【问题标题】:Loading collections into models in backbone.js将集合加载到backbone.js中的模型中
【发布时间】:2013-11-01 22:10:18
【问题描述】:

这是目标: 我有一个parent 模型。它位于名为parents 的集合中。在parent 中是children 的集合。因此,当我实例化 parents 集合时,我得到了 parent 模型,每个模型都包含 children 的集合。

我已经尝试了几种不同的方法,但我得到了不同的性能。有时children 根本不会出现。有时,它们会在parent 渲染循环的每次迭代中重复。我正在寻求有关执行此操作的最佳做​​法的帮助。

children 的列表不应该改变,每次都应该是同一个集合。我可以稍后即时过滤差异。

我已将其精简为尽可能简单地仅提取数据,没有包含其他额外内容以明确需要发生的事情。

我两次加载children 集合。 (嗯,很多次,但一次在parent 集合中,一次在每个parent 模型中)。这样我就可以添加一个新的“父级”并访问子级,这样我就可以在保存之前将它们添加到“父级”模型中。

问题:

  • 如何确保Children 已加载到Parent 只有一次?
  • 如何将一个Children 集合加载到Parents 收藏?
  • 这是正确的做法吗?

型号:

$(function() {
    window.Child = Backbone.Model.extend({});
    window.Children = Backbone.Collection.extend({
        model: Child
    });
    window.Parent = Backbone.Model.extend({
        initialize: function() {
            this.children = new Children();
            children.fetch();
        }
    });
    window.Parents = Backbone.Collection.extend({
        model: Parent
        initialize : function() {
            this.childrenAll = new Children();
            this.childrenAll.fetch();
        }
    });
    // instantiate the collections
    window.parents = new Parents();
});

我的观点:

$(function() {
    window.ChildView = Backbone.View.extend({
        className: "child-item",
        template: _.template($('#child-template').html()),
        initialize: function () {
            _.bindAll(this, 'render');
            this.model.bind('change', this.render);
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });
    window.ChildrenView = Backbone.View.extend({
        className: 'children',
        template: _.template($('#children-template').html()),
        initialize: function() {
            _.bindAll(this, 'render');
            this.collection.bind('reset', this.render);
        },
        render: function() {
            this.$el.html(this.template());
            this.collection.each(function(child) {
                var view = new ChildView({ model:child });
                $('.children-list').append(view.render().el);
            });
            return this;
        }
    });
    window.ParentView = Backbone.View.extend({
        className: "parent",
        template: _.template($('#parent-template').html()),
        initialize: function () {
            _.bindAll(this, 'render');
            this.model.bind('change', this.render);
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            this.children = new Children({ collection: children });
            $('.children-list').html(this.children.render().el);
            return this;
        }
    });
    window.ParentsView = Backbone.View.extend({
        el: "#app",
        template: _.template($('#parents-template').html()),
        initialize: function () {
            _.bindAll(this, 'render', 'add');
            this.collection.bind('reset', this.render);
        },
        render: function() {
            this.$el.html(this.template());
            this.childrenView = new ChildrenView({ collection: children });
            $('.children-new').append(this.childrenView.render().el);
            this.collection.each(function(parent){
                var view = new ParentView({ model: note });
                $('#items-list').prepend(view.render().el);
            });
            return this;
        }
    });
});

路由器:

$(function() {
    window.ParentsRouter = Backbone.Router.extend({
        routes: {
            '': 'list'
        },
        initialize: function() {
            // starts by assigning the collection to a variable so that it can load the collection
            this.parentsView = new ParentsView({
                collection: parents
            });
            parents.fetch({ reset: true });
        },
        list: function () {
            $('#app').empty();
            $('#app').append(this.parentsView.render().el);
        }
    });

    window.parentsRouter = new ParentsRouter();
    Backbone.history.start();
});

【问题讨论】:

  • 问题是什么?
  • 我不敢相信我添加了所有这些并且从未问过任何问题。问题已添加。

标签: javascript backbone.js backbone-views backbone-model backbone.js-collections


【解决方案1】:

事实证明,有几种不同的方法可以做到这一点。我选择在父模型的初始化方法中实例化和渲染子集合。

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    相关资源
    最近更新 更多