1

我很难获得在 BackboneJS 中工作的具有自动递增“订单”属性的模型。

由于某种原因,每个订单都设置为1. 函数中集合的长度nextOrder始终为0

Options = _.extend(Options, {
    Models: {
        Profile: Backbone.Model.extend({
            defaults: function() {
                console.log("Defaults");
                return {
                    title: "New Profile",
                    order: Profiles.nextOrder(),
                    active: false
                };
            },
            url: "/youdontcare"
        })
});

Options = _.extend(Options, {
    Collections: {
        ProfileList: Backbone.Collection.extend({
            model: Options.Models.Profile,
            comparator: function(profile) {
                console.log("Comparator");
                return profile.get('order');
            },
            nextOrder: function() {
                console.log("nextOrder...");
                console.log(this.length);
                if (!this.length) return 1;
                return this.last().get('order') + 1;
            },
            url: "/youdontcare"
        })
});

Options = _.extend(Options, {
    Views: {
        ProfileView: Backbone.View.extend({
            tagName: "li",
            template: _.template($('#profile-template').html()),
            render: function() {
                console.log("Render Profile");
                this.$el.html(this.template(this.model.toJSON()));
                return this;
            }
        }),
        ProfileListView: Backbone.View.extend({
            el: $("#auth_env_list"),
            initialize: function() {
                Profiles = new Options.Collections.ProfileList;
                console.log("INIT LIST");

                this.listenTo(Profiles, 'add', this.addOne);
                this.listenTo(Profiles, 'reset', this.addAll);
                this.listenTo(Profiles, 'all', this.render);

                // Suppresses 'add' events with {reset: true} and prevents the app view 
                // from being re-rendered for every model. Only renders when the 'reset'
                // event is triggered at the end of the fetch.
                console.log("Fetching");
                Profiles.fetch({ reset: true });
                console.log("Done fetching");
            },
            addOne: function (profile) {
                console.log("addOne");
                var view = new Options.Views.ProfileView({ model: profile });
                this.$el.append(view.render().el);
            },
            addAll: function () {
                console.log("addAll");
                this.$el.html('');
                Profiles.each(this.addOne, this);
            },
            render: function() {
                console.log("RENDER PROFILE LIST VIEW");

                if (Profiles.length)
                {

                }
            }
        })
});

我可以看到集合实例中的nextOrder函数被调用了为集合获取的每个元素的适当次数......但是它尝试计算的集合的长度总是返回!ProfilesOptions.Collections.ProfileListthis.length0

带有 5 个“配置文件”元素的控制台输出:

INIT LIST
Fetching 
RENDER PROFILE LIST VIEW
Done fetching
Defaults
nextOrder... 
0
Defaults
nextOrder...
0
Defaults
nextOrder...
0
Defaults 
nextOrder...
0
Defaults
nextOrder...
0
Comparator 
addAll
addOne
Render Profile
addOne
Render Profile
addOne
Render Profile
addOne
Render Profile
addOne
Render Profile
RENDER PROFILE LIST VIEW
RENDER PROFILE LIST VIEW

有没有更好的方法可以为这些分配一个自动递增的客户端 ID?我想要这样做的唯一原因是将它们显示在编号列表中。

4

1 回答 1

2

通过模型调用集合,您有点创建循环引用,如果您要在不同的集合上重用代码,它的效率不是很高。您可能会得到 0 回来,因为它不是指实际的集合实例。完成您想要的更好的方法是让集合在将新模型添加到集合时分配订单号:

// 1st, get rid of the adding an order in your model
// in your collection, add something like the following
initialize: function() {
  this.on('add', this.addOrderID);
  this.on('reset', this.addOrderIDs);
},
addOrderID: function(model) {
  var order = this.length;
  model.set({'order': order});
},
addOrderIDs: function() {
  var order = this.length;
  this.models.forEach( function(model) {
    model.set({'order': order});
    order++;
  }, this);
}

我认为这应该完成你正在寻找的东西。

于 2013-10-01T16:55:46.830 回答