主干自定义事件:获取调用者对象

发布于 2024-12-22 11:32:08 字数 789 浏览 0 评论 0原文

只是一些琐事; 我正在循环中创建视图和模型,然后尝试将自定义事件绑定到它们。到目前为止一切都很顺利,但是当我想找出对象触发的事件时呢?我知道还有其他冗长的方法可以将事件单独分配给系列中的每个事件,但我想我正在寻找一种更有效的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-12-29 11:32:08

抱歉,由于缺乏要点,我认为我无法删除我的问题,但我刚刚在其他地方找到了答案,并且认为将来其他人可能会发现这很有用:

当您触发事件时,应将对象作为第二个参数包含在内 到触发方法

像这样从 BasicContentView

this.trigger("updated",this)

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

this.trigger("updated",this)
吻泪 2024-12-29 11:32:08

集合是执行此类操作的完美场所,它会是这样的:

var myCollectionView = Backbone.View.extend({

  initialize: function() {
    this.models = [];
    this.collection.add(this.models);
    this.collection.bind("add", this.addOne, this);
  }

  addOne: function(model_instance) {
    var basicContentView = new BasicContentView({model:model_instance});
    this.$('ul').append(basicContentView); // if your basicContentView is an li for example.
  }

});

A collection is the perfect place to do this type of thing, it would be something like this:

var myCollectionView = Backbone.View.extend({

  initialize: function() {
    this.models = [];
    this.collection.add(this.models);
    this.collection.bind("add", this.addOne, this);
  }

  addOne: function(model_instance) {
    var basicContentView = new BasicContentView({model:model_instance});
    this.$('ul').append(basicContentView); // if your basicContentView is an li for example.
  }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文