maximus
maximus

Reputation: 4302

backbone.js for connecting model with another model

I know that in backbone.js you can connect View with Model. Where in Model you can update it's information by calling fetch etc. But in my case, I have several Views which can use common data. How is it possible in backbone.js to connect Model to some "Super" Model? So that I can have only one copy of data, but used at different places. And if one of the models updates the data, the "Super" Model will get that update and if needed make save to server.

If there are other than backbone.js frameworks which are better for such things, please suggest them.

Upvotes: 1

Views: 236

Answers (1)

Sachin Jain
Sachin Jain

Reputation: 21842

Maximus, You can define a parentModel yourself where in you can keep your common data and behavior. For example:

var PersonModel = Backbone.Model.extend({
    // Common Data
    defaults: {
        'company' : 'SO'
    },
    // Common behavior
    getCompany: function() {
        return this.get('company');
    }
});

var EmployeeModel = PersonModel.extend({
    // Override PersonModel behavior here and define new
});

Upvotes: 1

Related Questions