javascript - backbone.js "order"属性像 TodoMVC 不递增

标签 javascript jquery backbone.js backbone-views todomvc

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

出于某种原因,每个订单都设置为 1nextOrder 函数中集合的长度始终为 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)
                {

                }
            }
        })
});

我可以看到 Options.Collections.ProfileList 集合的 Profiles 实例中的 nextOrder 函数被调用了适当的次数对于为集合获取的每个元素...但是它尝试使用 this.length 计算的集合的长度总是返回 0!

带有 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?我想要这样做的唯一原因是将它们显示在编号列表中。

最佳答案

通过模型调用集合,您创建了一个循环引用,如果您要在不同的集合上重用代码,效率会很低。返回 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);
}

我认为这应该可以满足您的需求。

关于javascript - backbone.js "order"属性像 TodoMVC 不递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19120532/

相关文章:

javascript - 通知声音不起作用

javascript - jquery - 当一系列 promise 成功时运行回调

javascript - 单击 tpl 链接时,ExtJS 4.2 自定义组件触发事件

javascript - jQuery .load 未在 Cocoa WebView 中加载本地 html 文件

javascript - WordPress的。添加后html代码消失

javascript - 主干事件委托(delegate) eventName 问题

javascript - 面板内的 jQuery Accordion 对齐

jquery:未捕获类型错误:$(...).error 不是函数

javascript - 如何仅使用 Backbone 中的 View ?

javascript - 访问主干模型的属性