Javascript 如何查找已对集合触发的事件
我有一个名为approvals的集合,其中有一个同步事件,并将集合的事件类型删除到renderRows。选中下面的代码我需要根据批准集合删除事件重置当前集合Javascript 如何查找已对集合触发的事件,javascript,backbone.js,marionette,Javascript,Backbone.js,Marionette,我有一个名为approvals的集合,其中有一个同步事件,并将集合的事件类型删除到renderRows。选中下面的代码我需要根据批准集合删除事件重置当前集合 this.approvals.on("sync delete", this.renderRows, this); function renderRows(model, e, event ) { //some code if (event.type == "delete") { this.colle
this.approvals.on("sync delete", this.renderRows, this);
function renderRows(model, e, event ) {
//some code
if (event.type == "delete") {
this.collection.reset();
}
}
但我得到的事件是
未定义的
。请告诉我如何获取集合的event.type事件名称不会传递,除非它是专门作为参数传递的,例如触发器('sync','sync')
。因此,您可以检查参数(因为在本例中,它们根据事件的不同而不同),但这不是一个好主意,因为它们可能会更改,这会使代码变得脆弱。您的最佳选择是简单地将其拆分:
this.listenTo(this.approvals, "sync", this.onApprovalSync);
this.listenTo(this.approvals, "delete", this.onApprovalDelete);
onApprovalSync: function() {
this.renderRows();
}
onApprovalDelete: function() {
this.collection.reset();
this.renderRows();
}
您还可以选择以下选项:
this.listenTo(this.approvals, 'sync', _.partial(this.renderData, 'sync'));
this.listenTo(this.approvals, 'delete', _.partial(this.renderData, 'delete'));
而renderData
(或者您想怎么称呼它)将获得一个额外的参数,您将通过部分
(curry)传递该参数
这是方法签名:
collection.trigger('sync',collection,resp,options)代码>删除看起来是一样的
下面是一个基本示例:(无法执行删除
,但我可以触发更改
,只需等待5秒钟)
var Model1=Backbone.Model.extend({
网址:'http://jsonplaceholder.typicode.com/posts/1'
});
var View1=Backbone.View.extend({
模板:u.template('-'),
初始化:函数(){
//尽快渲染某物
这个。render();
this.model=newmodel1();
this.listenTo(this.model,'sync',_u.partial(this.renderData,'sync');
this.listenTo(this.model,'change',_u.partial(this.renderData,'change');
this.model.fetch();
//测试它
setTimeout(u.bind(function(){this.model.set('body','it was changed')},this),5000);
},
//这是正常的同步/更改函数签名,只有一个额外的参数`eventName`
//那是“咖喱”
renderData:函数(事件名称、模型、响应、选项){
this.$el.html(this.template({
“eventName”:eventName,
“body”:model.get('body')
}));
归还这个;
},
render:function(){
这个.$el.html('这里没什么可看的');
归还这个;
}
});
新视图1({el:$('body')。追加($(''))});
在此处运行:据我所知,您希望为sync
和remove
事件提供一个公共处理程序,并希望确定哪个事件触发了处理程序
由于这些事件回调的签名不同:
删除:(型号、系列、选项)
和sync:(model\u或\u collection,resp,options)
我们可以通过检查传递给处理程序的参数类型来实现这一点,如下所示:
var-View=Backbone.View.extend({
初始化:函数(){
this.listenTo(this.collection,'sync',this.dosomething);
this.listenTo(this.collection,'remove',this.dosomething);
这个。render();
},
活动:{
'单击#删除':'triggerRemove',
'单击#同步':'触发同步',
},
render:function(){
这是.el.html('Trigger removetrigger sync')。appendTo('body');
归还这个;
},
triggerRemove:函数(){
var model=this.collection.add({
名称:“测试”
});
此.collection.remove(模型);
},
triggerSync:函数(){
this.collection.trigger('sync');
},
剂量测量:功能(模型){
if(arguments[1]instanceof Backbone.Collection)//第二个参数是集合
console.log('删除已触发')
其他的
console.log('同步已触发')
}
});
变量视图=新视图({
集合:新主干。集合([{
姓名:“你好”
}, {
名字:“hellow”
}])
});代码>
我想对这两个事件使用相同的函数,大多数代码都是通用的,需要使用事件类型进行检查。您可以将通用代码放在另一个方法中,唯一的其他方法是检查参数类型,但是delete
是自定义事件吗?因此,我们无法帮助您,因为我们不知道触发器签名是什么。delete
是自定义事件还是您打算使用remove
。。?我们可能会得出一个解决方案,即删除用于跟踪是否从集合中删除了任何内容,这不是自定义的。主干中没有delete
事件,因为我们想知道是否有任何答案有帮助。。。
renderData: function(eventName, collection, resp, options) {}
var Model1 = Backbone.Model.extend({
url: 'http://jsonplaceholder.typicode.com/posts/1'
});
var View1 = Backbone.View.extend({
template: _.template('<%= eventName %> - <%= body %>'),
initialize: function() {
// render something as soon as possible
this.render();
this.model = new Model1();
this.listenTo(this.model, 'sync', _.partial(this.renderData, 'sync'));
this.listenTo(this.model, 'change', _.partial(this.renderData, 'change'));
this.model.fetch();
// to test it
setTimeout(_.bind(function(){this.model.set('body', 'it was changed')}, this), 5000);
},
// this is the normal sync/change function signature only with one extra param `eventName`
// which is being `curry`'ed in
renderData: function(eventName, model, resp, options) {
this.$el.html(this.template({
'eventName': eventName,
'body': model.get('body')
}));
return this;
},
render: function() {
this.$el.html('nothing to see here');
return this;
}
});
new View1({el: $('body').append($('<div>'))});