Backbone js - 更改模型属性

时间:2015-01-30 14:50:38

标签: javascript backbone.js

我有一个骨干模型:

App.Models.Education = Backbone.Model.extend({
  schema: {
    university: {
      type: 'Text',
      validators: ['required'],
      editorAttrs: {placeholder: 'Test placeholder'}
    },
   info: {type: 'Text'},
   desc: {type: 'Text'}
})

并扩展它:

App.Models.HighSchool = App.Models.Education.extend({
   initialize: function () {
      //code to change placeholder
      this.set({education_type: init_parameters.school});
   }
});

如何更改HighSchool“大学”字段中的占位符文本?

1 个答案:

答案 0 :(得分:1)

我不建议以这种方式设置你的模型。您应该尝试避免嵌套属性,因为您遇到了完全相同的问题。改变一个特定的领域变得很难。

相反,你可以这样做:

App.Models.Education = Backbone.Model.extend({
    defaults: { // backbone keyword to have default model attributes
        type: 'Text',
        validators: ['required'],
        editorAttrs: {placeholder: 'Test placeholder'
    }
});

App.Models.HighSchool = App.Models.Education.extend({
   initialize: function () {
       //code to change placeholder
       this.set('editorAttrs', {placeholder: 'new placeholder'});
       this.set('education_type', init_parameters.school); 
   }
});
相关问题