2014-10-09 24 views
0

我是backbone.js的新手,我正在試着學習它。在下面的代碼中,我希望我的集合名爲「JokesCollection」,只接受類「Joke」的添加模型。我該如何做到這一點?將「收集」屬性「模型」設置爲某個模型時,是不是隻接受該模型類並確保同質性的集合?不要這樣縫。當我將「JokesCollection」類中的屬性「model」分配給「Joke」時,它仍然接受增加「Persson」類的模型,這不是我想要的。我只希望它接受添加類「笑話」的模型。Backbone.js如何讓收藏只接受一類模型

Joke = Backbone.Model.extend ({ 
    initialize: function(){ 
     console.log("Joke was created"); 
    }, 
    defaults: { 
     joke : "", 
     date : "0", 
    } 
}); 

JokesCollection = Backbone.Collection.extend({ 
    initialize: function(){ 
     console.log("JokesCollection was created"); 
    }, 
    model: Joke // <=== Isn´t this supposed to ensure that the collection only accepts models of class "Joke"? 
}); 

Person = Backbone.Model.extend ({ 
    initialize: function(){ 
     console.log("Person was created"); 
    }, 
    defaults: { 
     username: "default", 
     password: "default", 
     email: "default" 
    } 
}); 



var person1 = new Person({username:"masterMind"}); 
var joke1 = new Joke({joke:"Girls are cute and funny hahahaha"}); 

jokesCollection = new JokesCollection(); 
jokesCollection.add(joke1); 
jokesCollection.add(person1); // This adds a model of class "Person" to the collection. Witch is not what I want. It is not supposed to work! I want "jokesCollection" to only accept models of class "Joke". 

console.log(jokesCollection.length); // length gets increased by 1 after adding "person1" to "jokesCollection". Again, it is no supposed to work from my point of view. I want "jokesCollection" to only accept models of class "Joke". 
console.log(jokesCollection); 

回答

1

來自官方的文檔:

模式collection.model

覆蓋此屬性來指定模型類的集合 包含。如果已定義,則可以傳遞原始屬性對象(和數組) 以添加,創建和重置,並且這些屬性將轉換爲適當類型的 模型。

看起來將不得不重新編寫add方法是這樣的:

add: function(models, options) { 

    var modelClass = this.model; 
     isProperIns = this.models.every.(function(model){ 
      return model instanceof modelClass; 
     }); 

    if (!isProperIns) { 
     throw new Error("Some of models has unacceptable type") 
    } 

    return this.set(models, _.extend({merge: false}, options, addOptions)); 
} 
+0

如果您發現該答案有用,請將其標記爲正確 – Evgeniy 2014-11-24 10:44:34

1

一個Collectionmodel屬性的目的是限制哪些車型Collection可以接受。相反,該屬性定義Model類,當需要創建新的Model時,Collection將使用該類。例如,當你傳遞給JokesCollection.add對象文本的Model屬性(而不是一個實例Model),或當你fetch模型到JokesCollection,骨幹網將使用JokeModel實例化這些新增加的Collection

有兩種方法可確保您的JokesCollection僅填充Joke的實例。第一種方式是永遠不會直接添加Model實例的JokesCollection,而是兩種:

A)通過在JokesCollection

乙打電話fetch帶來新的Joke S IN從服務器)只添加「原始」 Model屬性爲JokesCollection;不添加實例化Model小號

不過,如果你擔心開發商不小心添加非JokeModelCollection,你的其他選項(如首先由@Evgeniy建議)是覆蓋你的JokesCollectionadd方法。不像@ Evgeniy的回答,雖然我不會推薦重寫Backbone的內部。相反,我會使用一個簡單的覆蓋,只是在可能的情況下調用基本骨幹方法:

add: function(models, options) { 
    if (models instanceof Joke) { 
     // Use the normal Backbone.Collection add method 
     return Backbone.Collection.prototype.add.call(this, models, options); 
    } 
    var allModelsAreJokes = _(models).all(function(model) { 
     return model instanceof Joke; 
    )); 
    if (allModelsAreJokes) { 
     // Use the normal Backbone.Collection add method 
     return Backbone.Collection.prototype.add.call(this, models, options); 
    } 
    // Handle the case where non-Jokes are passed in; either: 
    // A) convert whatever was passed in to be a Joke: 
    // var rawModels = _(models).isArray() ? _(models).invoke('toJSON') : model.toJSON(); 
    // return Backbone.Collection.prototype.add.call(this, rawModels, options); 
    // B) just don't add anything 
} 
相關問題