主干自定义事件:获取调用者对象
只是一些琐事; 我正在循环中创建视图和模型,然后尝试将自定义事件绑定到它们。到目前为止一切都很顺利,但是当我想找出对象触发的事件时呢?我知道还有其他冗长的方法可以将事件单独分配给系列中的每个事件,但我想我正在寻找一种更有效的方法:
initialize: function(args)
{
this.views = [];
this.models = [];
for(var i =0;i<args.items.length;i++)
{
this.models[i] = new BasicContentModel({url:args.items[i].urlRoot+args.items[i].url});
this.views[i] = new BasicContentView({model:this.models[i],id:i,className:"tab-"+i});
this.views[i].bind("updated",this.update,this) //<-- heres the bind
}
// blah blah
},
update: function(args)
{
console.log("updating:",args) //<-- trying to get which one called the update
}
这有意义吗?基本上我想记录哪个视图刚刚更新(向上冒泡之类的事情)
Just a bit of trivia;
I'm creating views and models in a loop then trying to bind custom events to them. All goo so far but what about when I want to find out with object fired the event. I know there are other longer winded ways to individually assign events to each one in series but I guess I'm looking for a more efficient way:
initialize: function(args)
{
this.views = [];
this.models = [];
for(var i =0;i<args.items.length;i++)
{
this.models[i] = new BasicContentModel({url:args.items[i].urlRoot+args.items[i].url});
this.views[i] = new BasicContentView({model:this.models[i],id:i,className:"tab-"+i});
this.views[i].bind("updated",this.update,this) //<-- heres the bind
}
// blah blah
},
update: function(args)
{
console.log("updating:",args) //<-- trying to get which one called the update
}
Does this make sense? basically I want to log which view just got updated (bubbling upward sort of thing)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,由于缺乏要点,我认为我无法删除我的问题,但我刚刚在其他地方找到了答案,并且认为将来其他人可能会发现这很有用:
当您触发事件时,应将对象作为第二个参数包含在内 到触发方法
像这样从 BasicContentView
Sorry, I dont think I can delete my question due to lack of points but I've just found the answer elsewhere and thought maybe someone else in future might find this useful:
When you trigger the event the object should be included as the 2nd parameter to the trigger method like so
From BasicContentView
集合是执行此类操作的完美场所,它会是这样的:
A collection is the perfect place to do this type of thing, it would be something like this: