【问题标题】:Setting defaults in subclass backbone在子类主干中设置默认值
【发布时间】:2014-12-26 18:41:45
【问题描述】:

我有一个超类

var mySuperClass = Backbone.Model.extend({
     defaults: { ... }
});

var mySubClass = mySuperClass.extend({
     defaults: _.extend(mySuperClass.prototype.defaults, { 
         childDefault1: ..
     }
})

它工作得很好,它会覆盖我需要覆盖的每个值,但是,如果我用options 创建一个new mySubClass 就像:new mySubClass({someDefault: value}) someDefault property 不会设置。 任何想法?

【问题讨论】:

    标签: javascript inheritance backbone.js underscore.js


    【解决方案1】:

    使用 _.defaults

    var SuperBook = Backbone.Model.extend({
        defaults: {
            author: "test",
            pages: 525
        }
    });
    
    var MiniBook = SuperBook.extend({
        defaults: {
            description: "mini book"            
        }
    });
    
    _.defaults(MiniBook.prototype.defaults, SuperBook.prototype.defaults); 
    
    
    var myModel = new MiniBook({ author: 'default author', description : 'default description' })
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2010-10-01
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多