主干 js - 更改模型属性

Backbone js - change model attributes

本文关键字:模型 属性 js 主干      更新时间:2023-09-26

我有一个骨干模型:

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});
   }
});

如何更改高中"大学"字段中的占位符文本?

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

相反,你能做这样的事情吗:

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); 
   }
});