backbone.js-使用事件调度器var dispatcher=\ ux.clone(backbone.Events)
信中说: 要制作一个方便的事件调度器,可以在应用程序的不同区域之间协调事件:backbone.js-使用事件调度器var dispatcher=\ ux.clone(backbone.Events),events,backbone.js,dispatcher,Events,Backbone.js,Dispatcher,信中说: 要制作一个方便的事件调度器,可以在应用程序的不同区域之间协调事件:var dispatcher=\uu.clone(Backbone.events) 有人能解释一下如何实现调度器从一个视图到另一个视图的通信吗?我必须将代码放在我的应用程序中的何处?这是一篇关于使用应用程序的好文章 有人能解释一下如何实现调度器从一个视图到另一个视图的通信吗?我必须将代码放在我的应用程序中的什么位置 您可能会有某种类型的应用程序控制器对象,它将控制应用程序的流程、创建视图、模型等。这也是事件聚合器的好地方
var dispatcher=\uu.clone(Backbone.events)
有人能解释一下如何实现调度器从一个视图到另一个视图的通信吗?我必须将代码放在我的应用程序中的何处?这是一篇关于使用应用程序的好文章 有人能解释一下如何实现调度器从一个视图到另一个视图的通信吗?我必须将代码放在我的应用程序中的什么位置 您可能会有某种类型的应用程序控制器对象,它将控制应用程序的流程、创建视图、模型等。这也是事件聚合器的好地方
从我的观点来看,我认为这篇文章对此解释得很好。最近,我需要一个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){
}
})
))();
});
下面是EventDispatcher和一些示例事件。事件本身在模板对象中预定义。您的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;
}
});
//模块事件调度器
定义(['backbone',下划线]],函数(backbone,u2;){
var实例;
函数getInstance(){
如果(!实例){
instance=createInstance();
}
返回实例;
}
返回getInstance();
函数createInstance(){
//ide的虚拟函数将被覆盖
函数e(eventContext,回调){}
var eventHandler={},
//请随意将模板放在另一个模块中
//或者甚至更多,将每个区域拆分为一个
模板={
服务器:{
连接:e,
断开连接:e,
登入:e,,
注销:e
},
全球:{
windowResize:e,
游戏开始:e
},
另一方面:{
希德尔:e
}
};
//创建事件
_.每个(模板、功能(事件、类别){
var handler=eventHandler[category]={.extend({},主干.Events);
变量类别事件={
//关闭侦听器。对于给定的\u或回调或两者:
//off()完成类别及其所有事件的清除。
//关闭(回调)关闭所有给定回调,无论这是什么
//off(null,this)在给定的情况下关闭所有,无论回调是什么
//关闭(回调,此)关闭所有给定回调和此
关闭:函数(回调,\u this){
如果(!callback&&&u this){
handler.off();
}否则{
_.每个(模板[类别],功能(v,k){
k!=“off”和&template[category][k].off(回调,\u this);
});
}
}
};
events.all=e;
_.每个(事件、功能(值、事件){
//在中创建新的侦听器
//例如:template.global.onSomething(this,fn);
categoryEvents[event]=函数(\u this,回调){
_listenTo(处理程序、事件、回调);
};
//在中仅为一个触发器创建新的侦听器
//例如:template.global.onSomething(this,fn);
categoryEvents[event].once=函数(\u this,回调){
_this.listenToOnce(处理程序、事件、回调);
};
//触发侦听器
//例如:template.global.onSomething.trigger();
categoryEvents[event].trigger=函数(debugData){
console.log('**Event**'+category+'.+事件,debugData?debugData:'');
触发器(事件);
};
//关闭侦听器。对于给定的\u或回调或两者:
//off()完成category.event的清除
//关闭(回调)关闭所有给定回调,无论这是什么
//off(null,this)在给定的情况下关闭所有,无论回调是什么
//关闭(回调,此)关闭所有给定回调和此
//例如:template.global.onSomething.off(fn,this);
categoryEvents[event].off=函数(回调,\u this){
关闭(事件,回调,_this);
}
});
模板[类别]=类别事件;
});
返回模板;
}
});
主干事件系统的行为不受任何影响,可以正常使用