在backbone.js
documentation它說:Backbone.js的 - 與事件調度VAR調度= _.clone工作(Backbone.Events)
爲了一個方便的事件調度,可以應用程序的不同區域之間協調事件:var dispatcher = _.clone(Backbone.Events)
任何人都可以解釋如何實現調度員從一個視圖進行通信到另一個?我該在哪裏將代碼放入我的應用程序中?
在backbone.js
documentation它說:Backbone.js的 - 與事件調度VAR調度= _.clone工作(Backbone.Events)
爲了一個方便的事件調度,可以應用程序的不同區域之間協調事件:var dispatcher = _.clone(Backbone.Events)
任何人都可以解釋如何實現調度員從一個視圖進行通信到另一個?我該在哪裏將代碼放入我的應用程序中?
這是一篇關於使用event aggregator的好文章。
任何人都可以解釋如何實現調度員從一個視圖進行通信到另一個?我該在哪裏將代碼放入我的應用程序中?
您可能會擁有某種App Controller對象,它將控制應用程序的流程,創建視圖,模型等。這也是事件聚合器的好地方。
從我的角度來看,我認爲這篇文章解釋得非常好。
最近,我需要一個EventDispatcher來處理大量的事件,而不會丟失他們的名字和他們的行爲軌道。
也許它也可以幫助你。
這裏一個簡單的例子查看:
define(['backbone', 'underscore', 'eventDispatcher'],
function(Backbone, _, dispatcher){
new (Backbone.View.extend(_.extend({
el: $('#anyViewOrWhatever'),
initialize: function() {
window.addEventListener('resize', function() {
// trigger event
dispatcher.global.windowResize.trigger();
});
// create listener
dispatcher.server.connect(this, this.doSomething);
// listen only once
dispatcher.server.connect.once(this, this.doSomething);
// remove listener:
dispatcher.server.connect.off(this, this.doSomething);
// remove all listener dispatcher.server.connect from this:
dispatcher.server.connect.off(null, this);
// remove all listener dispatcher.server.connect with this method:
dispatcher.server.connect.off(this.doSomething);
// remove all listener dispatcher.server.connect no matter what and where:
dispatcher.server.connect.off();
// do the same with a whole category
dispatcher.server.off(/* ... */);
// listen to all server events
dispatcher.server.all(this, this.eventWatcher);
},
doSomething: function(){
},
eventWatcher: function(eventName){
}
})
))();
});
在這裏,此事件的一些例子事件。事件本身是在模板Object中預定義的。您的IDE應該識別它們並引導您瀏覽列表。
正如你所看到的,分派器自己運行。只有您的視圖或任何需要底層事件方法的基礎。
// module eventDispatcher
define(['backbone', 'underscore'], function (Backbone, _) {
var instance;
function getInstance() {
if (!instance) {
instance = createInstance();
}
return instance;
}
return getInstance();
function createInstance() {
// dummy function for your ide, will be overwritten
function e (eventContext, callback) {}
var eventHandler = {},
// feel free to put the template in another module
// or even more split them in one for each area
template = {
server: {
connect: e,
disconnect: e,
login: e,
logout: e
},
global: {
windowResize: e,
gameStart: e
},
someOtherArea: {
hideAll: e
}
};
// Create Events
_.each(template, function (events, category) {
var handler = eventHandler[category] = _.extend({}, Backbone.Events);
var categoryEvents = {
// turn off listener from <category>.<**all events**> with given _this or callback or both:
// off() complete purge of category and all its events.
// off(callback) turn off all with given callback, no matter what this is
// off(null, this) turn off all with given this, no matter what callback is
// off(callback, this) turn off all with given callback and this
off: function (callback, _this) {
if(!callback && _this){
handler.off();
}else{
_.each(template[category], function(v, k){
k != 'off' && template[category][k].off(callback, _this);
});
}
}
};
events.all = e;
_.each(events, function (value, event) {
// create new Listener <event> in <category>
// e.g.: template.global.onSomething(this, fn);
categoryEvents[event] = function (_this, callback) {
_this.listenTo(handler, event, callback);
};
// create new Listener <event> in <category> for only one trigger
// e.g.: template.global.onSomething(this, fn);
categoryEvents[event].once = function (_this, callback) {
_this.listenToOnce(handler, event, callback);
};
// trigger listener
// e.g.: template.global.onSomething.trigger();
categoryEvents[event].trigger = function (debugData) {
console.log('**Event** ' + category + '.' + event, debugData ? debugData : '');
handler.trigger(event);
};
// turn off listener from <category>.<event> with given _this or callback or both:
// off() complete purge of category.event
// off(callback) turn off all with given callback, no matter what this is
// off(null, this) turn off all with given this, no matter what callback is
// off(callback, this) turn off all with given callback and this
// e.g.: template.global.onSomething.off(fn, this);
categoryEvents[event].off = function (callback, _this) {
handler.off(event, callback, _this);
}
});
template[category] = categoryEvents;
});
return template;
}
});
骨幹事件系統的行爲不受任何影響,可以用作正常使用。