【问题标题】:binding collection change to view render绑定集合更改以查看渲染
【发布时间】:2012-10-23 22:27:19
【问题描述】:

尽管 StackOverflow 和其他地方有很多关于同一主题的问题/答案,但我仍然不明白如何继续。我想更改我的收藏以在我的视图中触发渲染功能。 View 有一个集合,而不是一个模型——所以我看到的许多 model.bind 示例都不适用。显然 collection.bind 不是合法的绑定。这是我的视图代码。我应该在初始化中添加什么,以便在orderedPrefs(集合)发生更改时调用视图的渲染函数?

headerView = Backbone.View.extend({

        el: $('#' + targetdiv),
        collection: orderedPrefs,
        events: {
            "click .scheduleheader": "clicked"                               // dependency here on scheduler.js class naming .scheduleheader

        },
        initialize: function () {
            _.bindAll(this, "render");

        },
        render: function () {
            alert('render!!');
        },

..... .....

【问题讨论】:

    标签: jquery backbone.js


    【解决方案1】:

    这些应该在初始化函数中起作用:

    this.collection.on("add", this.render);
    this.collection.on("remove", this.render);
    this.collection.on("reset", this.render);
    

    如果他们不这样做,则附加到视图的集合有问题。您不应使用全局“orderedPrefs”。

    主干文档状态:

    创建新视图时,您传递的选项将作为 this.options 附加到视图,以供将来参考。有几个特殊选项,如果通过,将直接附加到视图:模型、集合、el、id、className、tagName 和属性。

    当实例化你的视图时,你需要像这样传递集合:

    new headerView({ collection: orderedPrefs });
    

    如果你想跟踪集合模型的变化,你应该在不同的视图中进行:

    var ModelView = Backbone.View.extend({
        initialize: function() {
            _.bindAll(this, "render");
            this.render();
            this.model.on("change",this.render);
        },
        render: function() {
            $(this.el).html("model view markup"); // you should use templating
            return this;
        }
    });
    
    var headerView = Backbone.View.extend({
        el: $('#' + targetdiv),
        initialize: function () {
            _.bindAll(this, "render");
            this.collection.on("add", this.render);
            this.collection.on("remove", this.render);
            this.collection.on("reset", this.render);
        },
        render: function () {
            var self = this;
            this.collection.each(function(collectionModel){
                var view = new ModelView({ model : collectionModel });
                $(self.el).append(view.el);
            });
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用collection.on 绑定到“添加”和“删除”事件。有关使用示例,请参阅 documentation under Add。

      this.collection.on("add", this.render);
      

      如果您使用的是 Backbone 0.9.0 或更高版本,那么您可以在同一语句中绑定多个事件:

      this.collection.on("add remove", this.render);
      

      另请注意,“绑定”应该与“开启”相同:

      为了清楚起见,bind 和 unbind 已重命名为 on 和 off,遵循 jQuery 的引导。旧名称也仍受支持。

      【讨论】:

      • 但是当我的集合已经加载并且我对底层模型进行了修改时怎么办 - 在这种情况下我似乎无法绑定到更改?看起来我需要绑定到底层模型才能捕捉到变化(如编辑)?
      • @goldfinger 不,您可以使用“更改”事件来通知集合中任何模型的更改。只是从这里的文档开始——措辞有点模棱两可,所以我不能 100% 确定:您可以绑定“更改”事件,以便在修改集合中的任何模型时得到通知
      • 仍然有一些我无法理解的错误。 this.collection.bind("change", this.render) 在 JavaScript 中给出“对象不支持属性或方法绑定”错误,这就是为什么在 OP 中我提到 collection.bind 似乎不是一个合法的绑定。我不确定我到底出了什么问题....
      • @goldfinger Collection 对象肯定支持 bind - 所以可能集合没有正确设置。 orderedPrefs 在您的示例中定义在哪里?
      猜你喜欢
      • 2021-08-14
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多