Appcelerator - 如果 child 视图发生某些事情,通知 parent 视图

Appcelerator - Notify parent view if something happen on child view

我应该如何获取有关 child 视图在 parent 视图上的事件的信息?

例如:

我将参数传递给 child (Alloy.createController('myChildView', { info: test }).getView())。然后我使用它并将全局变量从 false 设置为 true (Alloy.Globals.childPrecessed = true)。在那之后,我可以在这个视图上花费任何时间,但是当我点击一个触发隐藏事件的按钮时,我应该处理来自 parent 视图的信息。

我的第一个想法是触发 az appwide 事件 (myChildHide),然后在 parent 视图中收听它。如果我抓住它,然后我处理信息,并销毁侦听器...

这是最好的方法吗?我不确定...

有没有更好的解决方案?

谢谢!

我是事件监听器的粉丝,所以我认为你的方法很好。

我通常做的是在我需要事件监听器生效之前启动事件监听器,即在打开子进程的方法中 window。但首先我使用 backbone 事件进行简单的事件触发。参见 Fokke Zandbergen's article for further info。所以假设你已经设置了一个 "dispatcher" 那么我会做这样的事情:

function openChild(){
   dispatcher.on("child-calling", doChildsWork);
   // ... open child view
}

然后在 doChildsWork 中,一旦调用,我将禁用事件处理程序:

function doChildsWork(args){
   dispatcher.off("child-calling");
   // ... do work initiated by child view using args...
}

最后在子视图中(假设您已经设置了 "dispatcher"),您将执行如下操作:

function doChildsWork(){
   // ... Tell parent to do some work
   dispatcher.trigger("child-calling",{test:true});
   // ... continue whatever is going on in child
}

我经常使用这种方法 - 而且效果很好:-)

/约翰