2011-07-17 10 views
3

私はちょうどbackbone.jsで始まりました。そして、私はサーバーからデータを取得する際に問題があります。ここで私はサーバーからの応答です。Backbone.js:同じモデルをセットに2回追加できません

[{ 
    "list_name":"list1", 
    "list_id":"4", 
    "created":"2011-07-07 21:21:16", 
    "user_id":"123456" 
}, 
{ 
    "list_name":"list2", 
    "list_id":"3", 
    "created":"2011-07-07 21:19:51", 
    "user_key":"678901" 
}] 

ここに私のjavascriptのコードだ...

// Router 
App.Routers.AppRouter = Backbone.Router.extend({ 
    routes: { 
     '': 'index' 
    }, 
    initialize: function() { 
    }, 
    index: function() { 
     var listCollection = new App.Collections.ListCollection(); 
     listCollection.fetch({ 
      success: function() { 
       new App.Views.ListItemView({collection: listCollection}); 
      }, 
      error: function() { 
       alert("controller: error loading lists"); 
      } 
     }); 
    } 
}); 

// Models 
var List = Backbone.Model.extend({ 
    defaults: { 
     name: '', 
     id: '' 
    } 
}); 

App.Collections.ListStore = Backbone.Collection.extend({ 
    model: List, 
    url: '/lists' 
}); 

// Initiate Application 
var App = { 
    Collections: {}, 
    Routers: {}, 
    Views: {}, 
    init: function() { 
     var objAppRouter = new App.Routers.AppRouter(); 
     Backbone.history.start(); 
    } 
}; 

私はエラーが

if (already) throw new Error(["Can't add the same model to a set twice", already.id]); 
BACKBONE.JS

に、このライン上に「二度セットに同じモデルを追加することはできません」を取得します

注釈付きBackbone.jsをチェックアウトして、最初のモデルがコレクションに追加されたが、2番目のモデルでこのエラーが発生することがわかりました。なぜこうなった?サーバーサイドの応答で何かを変更する必要がありますか?

答えて

8

Listidにはdefaultsというプロパティがあり、デフォルトではそれぞれのインスタンスのIDが同じになります。また、バックボーンはそれを使って偽装を検出しています。あなたのデータがIDとしてlist_idを使用する場合、idAttribute: 'list_id'Listクラス定義の中に入れて、それをバックボーンに伝える必要があります。

私は、オブジェクト属性に型情報を重複させないことをお勧めします(この点についてはBackbone.jsに同意します)。一貫した属性名を持つことは、バックボーンが期待していることであり、扱いが容易です。したがって、list_idlist_nameの代わりに、すべてのクラスでidnameを使用してください。

+0

私は多くの問題を救った - ありがとう! –

1

同じ修正IDを持つモデルを追加するには、この修正プログラムを使用します。

追加、使用:collection.add(model,{unique: false})

var __hasProp = {}.hasOwnProperty, 
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; 

Backbone.Collection = (function(_super) { 

    __extends(Collection, _super); 

    function Collection() { 
     return Collection.__super__.constructor.apply(this, arguments); 
    } 

    Collection.prototype.add = function(models, options) { 
     var i, args, length, model, existing; 
     var at = options && options.at; 
     models = _.isArray(models) ? models.slice() : [models]; 

     // Begin by turning bare objects into model references, and preventing 
     // invalid models from being added. 
     for (i = 0, length = models.length; i < length; i++) { 
      if (models[i] = this._prepareModel(models[i], options)) continue; 
      throw new Error("Can't add an invalid model to a collection"); 
     } 

     for (i = models.length - 1; i >= 0; i--) { 
      model = models[i]; 
      existing = model.id != null && this._byId[model.id]; 

      // If a duplicate is found, splice it out and optionally merge it into 
      // the existing model. 
      if (options && options.unique) { 
       if (existing || this._byCid[model.cid]) { 
        if (options && options.merge && existing) { 
         existing.set(model, options); 
        } 
        models.splice(i, 1); 
        continue; 
       } 
      } 

      // Listen to added models' events, and index models for lookup by 
      // `id` and by `cid`. 
      model.on('all', this._onModelEvent, this); 
      this._byCid[model.cid] = model; 
      if (model.id != null) this._byId[model.id] = model; 
     } 

     // Update `length` and splice in new models. 
     this.length += models.length; 
     args = [at != null ? at : this.models.length, 0]; 
     Array.prototype.push.apply(args, models); 
     Array.prototype.splice.apply(this.models, args); 

     // Sort the collection if appropriate. 
     if (this.comparator && at == null) this.sort({silent: true}); 

     if (options && options.silent) return this; 

     // Trigger `add` events. 
     while (model = models.shift()) { 
      model.trigger('add', model, this, options); 
     } 

     return this; 
    }; 

    return Collection; 

})(Backbone.Collection); 
0

バックボーンは、あなたはBACKBONE.JSでそれを見ることができます... 1つのコレクションに同じモデルを挿入するために私たちを防ぐライン676は700

場合行目にあなたは本当にコレクションに同じモデルを挿入したいだけで、そこにコードを削除します。

if(existing = this.get(model)){//here 
      ... 
    } 
関連する問題