Reputation: 137
Suppose we have something like this:
var parent = parentView; // A View
var child = Alloy.createController('ChildView',{});
parent.add(child);
And in the ChildView.js controller:
function closeView(){
// remove ourself from parent how?
}
Can you do this somehow? From the closeView() remove the child view from its parent?
The way i can solve it i guess is sending in the parent view in the {} options to the childview and keep a reference there {parent:parentView}. And then do a parent.remove()... in my closeView(). But is there some other way?
Upvotes: 0
Views: 2791
Reputation: 10064
Use events. All Alloy Controllers implement BackBone.Events
so when you create the child attach a close
event to it and have the parent remove the child.
The child should only be concerned with itself. knowing about how it interacts with its parent is leaking information and not following good SOLID design. Maintenance itself would be a nightmare not to mention the mental overhead to growk that kind of design.
var parent = parentView; // A View
var child = Alloy.createController('ChildView',{});
child.on('close', function () {
parent.remove(child.getView());
});
parent.add(child.getView());
function closeView(){
$.trigger('close');
}
Upvotes: 4
Reputation: 24815
You want to pass the reference to the parent to the child for this to work, as you mentioned
var child = Alloy.createController('ChildView',{parentView: parent});
Then in the childview:
function closeView(){
$.args.parentView.remove($.getView());
}
You can also do it the other way around by letting the parent take care of it:
in childview:
$.trigger('removeMe');
In the parent controller:
child.on('removeMe',function(){
parent.remove(child);
}
Upvotes: 0