我正在使用名为RiotControl
的库,RiotControl
拥有Store
个实例的集合。它还会在每个添加到其上的商店中连接一些事件监听器:
https://github.com/jimsparkman/RiotControl/blob/master/demo/todo.tag#L31
var RiotControl = {
_stores: [],
addStore: function(store) {
this._stores.push(store)
}
}
['on','one','off','trigger'].forEach(function(api){
RiotControl[api] = function() {
var args = [].slice.call(arguments)
this._stores.forEach(function(el){
el[api].apply(null, args)
})
}
})
Store
观察并触发事件:
https://github.com/jimsparkman/RiotControl/blob/master/demo/todostore.js#L21
function TodoStore() {
...
self.on('todo_add', function(newTodo) {
self.todos.push(newTodo)
self.trigger('todos_changed', self.todos)
})
}
在上面的示例中,商店会自行触发todos_changed
个事件。但不知怎的,这个事件在RiotControl
对象上也是可观察的:
https://github.com/jimsparkman/RiotControl/blob/master/demo/todo.tag#L31
// Register a listener for store change events.
RiotControl.on('todos_changed', function(items) {
self.items = items
self.update()
})
我不清楚RiotControl
当Store
被RiotControl
发射时,该事件是如何被观察到的。据我所知,forEach
在TodoStore
循环中,会将触发的事件分发给它管理的每个商店,这就是self.on('todo_add')
可以观察{{1}的原因}}。我无法弄清楚self.trigger('todos_changed')
如何冒泡到RiotControl
,以便在那里可以观察到它。有人有任何想法吗?
答案 0 :(得分:0)
该事件可在RiotControl上观察并通过self
触发,因为它已通过RiotControl.addStore(todoStore)
添加到RiotControl,后者将其添加到_stores
然后applys
,无论调用什么稍后self
或RiotControl
。
(见https://github.com/jimsparkman/RiotControl/blob/master/demo/index.html#L31)