Backbone.js model inheritance

I have a basic model

var app = app || {};

app.Era = Backbone.Model.extend({

    defaults: {
        from: Number.NEGATIVE_INFINITY,
        until: Number.POSITIVE_INFINITY,
        stash: {
            from: null,
            until: null
        },
        _enabled: true
    },

    toggle: function(){
        if(this.get('_enabled')){
            this.disable();
        }else{
            this.enable();
        }

        this.save();
    },

    enable: function(){
        this.from = this.stash.from;
        this.until = this.stash.until;

        this.stash.from = null; // strictly speaking unnecssary
        this.stash.until = null;

        this._enabled = true;
    },

    disable: function(){
        this.stash.from = this.from;
        this.stash.until = this.until;

        this.from = null;
        this.until = null;

        this._enabled = false;
    },

    enabled: function(){
        return this._enabled;
    },

});

      

which i am trying to do like this

Name = app.Era.extend({ defaults: { value: '' } });

      

It seems to work, I don't see any errors in the console. I can even create a new name, but when I try to create a new name, I get an error:

> era = new app.Era()
child
> era.get('from')
-Infinity
> Name = app.Era.extend({ defaults: { value: '' } })
function (){ return parent.apply(this, arguments); }
> name = new Name()
child
> name.get('value')
TypeError: Object [object Object] has no method 'get'

      

I would appreciate your feedback!

+3


source to share


2 answers


You cannot use name

in this context, you are working in window.name

. If you start from scratch with no lowercase variable name, you should work just fine. In addition, fencliff provides a good way to use the default inheritance.



+3


source


Edit: This might be a very good answer, but not for this specific question ... The correct answer was provided by Brian Clark. I'll leave this here, even though while I misunderstood your question, it describes a solution to a common problem and might help you anyway.

Base inheritance works by modifying the prototype of the constructor function. When you expand the model and override a property defaults

, it does not merge the property Name.defaults

into Era.defaults

, it just hides it.

Let's consider a simplified example:



var Era = Backbone.Model.extend({ name: "Era" });
var Name = Era.extend({ name: "Name" });
console.log(new Name().name); //-> "name", makes sense, right?

      

To combine a derived class defaults

with a superclass, you can use what Model.defaults

can be defined as a function
as well as an object literal.

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

var Name = Era.extend({ 
  //define defaults as a function
  defaults: function() {
    //copy the superclass defaults into a new object, and
    //then extend that with the new defaults
    return _.extend({}, Era.prototype.defaults, {
      value:''
    });
  }
});

      

+10


source







All Articles