使用時,我已經在骨幹網中定義像這樣一個模型,收集沒有定義:自定義字段中收集
$(document).ready(function() {
DeviceModel = Backbone.Model.extend({
urlRoot: '/ajax/mvcDevices',
validationRules: {
name: [{ rule: 'required'}],
mac: [
{ rule: 'required' },
{ rule: 'isMacAddress' }
],
speed: [{ rule: 'required'}]
},
preprocess: {
name: ['clean', 'trim'],
speed: ['clean', 'trim']
}
});
DeviceCollection = Backbone.Collection.extend({
url: '/ajax/mvcDevices',
Model: DeviceModel
});
});
然而,一個集合裏,這些模型時,列出的自定義字段都沒有定義的。我在這裏錯過了什麼?如果你創建一個new
運營商defaults
所有屬性將被複制到新的對象模型
var DeviceModel = Backbone.Model.extend({
urlRoot: '/ajax/mvcDevices',
defaults: {
validationRules: {
name: [{ rule: 'required'}],
mac: [
{ rule: 'required' },
{ rule: 'isMacAddress' }
],
speed: [{ rule: 'required'}]
},
preprocess: {
name: ['clean', 'trim'],
speed: ['clean', 'trim']
}
}
});
var DeviceCollection = Backbone.Collection.extend({
url: '/ajax/mvcDevices',
Model: DeviceModel
});
var collection = new DeviceCollection();
var model = new DeviceModel({id: 1});
collection.add(model);
console.log(collection.get(1).get('validationRules'));
console.log(collection.get(1).get('preprocess'));
從骨幹文檔,因此:
'Model:DeviceModel' =>'model:DeviceModel'? (小寫'm') – WiredPrairie 2013-03-26 12:09:17
@WiredPrairie(facepalm)就是這樣,一直注視着我的臉。非常感謝。 – Nidonocu 2013-03-26 13:48:46