骨干网/下划线UNIQUEID()奇数 [英] Backbone/Underscore uniqueId() Odd Numbers

查看:106
本文介绍了骨干网/下划线UNIQUEID()奇数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的骨干网和下划线,并有这不是一个真正的问题的问题之一 - 只是缠着我出于好奇

我建立了一个非常简单的应用程序,让您集合中添加和删除模型,使他们在浏览器中。它也有能力的console.log 集合(这样我就可以看到我的集合)。

下面是奇怪的事情:生成的ID的存在是 1,3,5 ... 等。有没有具体到我的code或某事的原因做BB /美?

下面是一个工作小提琴: http://jsfiddle.net/ptagp/

而code:

 应用=(函数(){VAR AppModel = Backbone.Model.extend({    默认值:{
        ID:空,
        项目:空
    }});VAR AppCollection = Backbone.Collection.extend({    型号:AppModel});VAR APPVIEW = Backbone.View.extend({    EL:$('#应用程序),    新野:$('#新项目),    初始化:功能(){
        this.el = $(this.el);
    },    事件:{
        点击#附加新的':'的addItem,
        点击卸下摆臂项':'的removeItem,
        点击打印#收集':'printCollection
    },    模板:$('#项目模板)HTML()。    渲染:函数(模型){
        VAR TEMPL = _.template(this.template);
        this.el.append(TEMPL({
            ID:model.get(ID),
            项目:model.get('项目')
        }));
    },    的addItem:功能(){
        newModel,并向VAR =新AppModel({
            ID:_.uniqueId()
            项目:this.newfield.val()
        });
        this.collection.add(newModel,并向);
        this.render(newModel,并向);
    },    的removeItem:功能(E){
        VAR ID = $这个(e.currentTarget).parent('格')的数据(ID)。
        VAR模型= this.collection.get(ID);
        this.collection.remove(模型);
        $(e.target).parent('格')删除()。
    },    printCollection:功能(){
        this.collection.each(函数(模型){
            的console.log(model.get('身份证')+':'+ model.get('项目'));
        });
    }});返回{
    启动:功能(){
        新APPVIEW({
            集合:新AppCollection()
        });
    }
};});$(函数(){新的App()开始();});


解决方案

如果您在Backbone.js的源$ C ​​$看看c您会发现_.uniqueId用于设置模型的 CID
<一href=\"https://github.com/documentcloud/backbone/blob/master/backbone.js#L194\">https://github.com/documentcloud/backbone/blob/master/backbone.js#L194

这意味着每次创建一个模型实例, _。UNIQUEID()被调用。
这就是导致它增加两倍。

I'm relatively new to Backbone and Underscore and have one of those questions that's not really an issue - just bugging me out of curiosity.

I built a very simple app that allows you to add and remove models within a collection and renders them in the browser. It also has the ability to console.log the collection (so I can see my collection).

Here's the weird thing: the ID's being generated are 1,3,5... and so on. Is there a reason specific to my code, or something to do with BB/US?

Here's a working Fiddle: http://jsfiddle.net/ptagp/

And the code:

App = (function(){

var AppModel = Backbone.Model.extend({

    defaults: {
        id: null,
        item: null
    }

});

var AppCollection = Backbone.Collection.extend({

    model: AppModel

});

var AppView = Backbone.View.extend({

    el: $('#app'),

    newfield: $('#new-item'),

    initialize: function(){
        this.el = $(this.el);
    },

    events: {
        'click #add-new': 'addItem',
        'click .remove-item': 'removeItem',
        'click #print-collection': 'printCollection'
    },

    template: $('#item-template').html(),

    render: function(model){
        var templ = _.template(this.template);
        this.el.append(templ({
            id: model.get('id'),
            item: model.get('item')
        }));
    },

    addItem: function(){
        var NewModel = new AppModel({
            id: _.uniqueId(),
            item: this.newfield.val()
        });
        this.collection.add(NewModel);
        this.render(NewModel);  
    },

    removeItem: function(e){
        var id = this.$(e.currentTarget).parent('div').data('id');
        var model = this.collection.get(id);
        this.collection.remove(model);
        $(e.target).parent('div').remove();
    },

    printCollection: function(){
        this.collection.each(function(model){
            console.log(model.get('id')+': '+model.get('item'));
        });
    }

});

return {
    start: function(){
        new AppView({
            collection: new AppCollection()
        });
    }
};

});

$(function(){ new App().start(); });

解决方案

if you look in the backbone.js source code you'll notice that _.uniqueId is used to set a model's cid: https://github.com/documentcloud/backbone/blob/master/backbone.js#L194

that means that every time you create a model instance, _.uniqueId() is invoked. that's what causing it to increment twice.

这篇关于骨干网/下划线UNIQUEID()奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆