Reputation: 43
How should I get info about child view's events on parent view?
For example:
I pass and argument to the child (Alloy.createController('myChildView', { info: test }).getView()). Then I work with it and set a global variable from false to true (Alloy.Globals.childPrecessed = true). After that, I can spent any time on this view, but when I click on a button which fires a hide event, I should process the info from parent view.
My first thought was I fire az appwide event (myChildHide), and listen for it in the parent view. If I catch it, then I process the info, and destroy the listener...
Is this the best method? I'm not sure...
Has anybody better solution for this?
Thanks!
Upvotes: 0
Views: 250
Reputation: 2807
I am a fan of event listeners, so I think your approach is a good one.
What I normally do is to initiate the event listener right before I need it to be effective, i.e. in the method opening the child window. But first I use backbone events for simple event triggering. See Fokke Zandbergen's article for further info. So assuming you have set up a "dispatcher" then I would do something like this:
function openChild(){
dispatcher.on("child-calling", doChildsWork);
// ... open child view
}
Then in the doChildsWork
I would disable the event handler once called:
function doChildsWork(args){
dispatcher.off("child-calling");
// ... do work initiated by child view using args...
}
And finally in the child view (assuming you have set up a "dispatcher") you would do something like this:
function doChildsWork(){
// ... Tell parent to do some work
dispatcher.trigger("child-calling",{test:true});
// ... continue whatever is going on in child
}
I use this approach a lot - and it works well :-)
/John
Upvotes: 2