Sonny Chivas
Sonny Chivas

Reputation: 17

Backbone good model structure

I would like to know your opinion about this structure. you have 3 or 4 part in the same page. And you used a main Model to control and communication between the another Model. I'm thinking that this is a good practice to reuse de code. What do you think about it?

     mainModel(comunication betwen submodel)
    /    |    \
 model  model  model

Thank you

Upvotes: 0

Views: 61

Answers (1)

jcreamer898
jcreamer898

Reputation: 8189

I like to create a mediator by extending Backbone.Events...

var App = {};
App.Events = _.extend({}, Backbone.Events);

var MainModel = Backbone.Model.extend({
    initialize: function() {
        App.Events.trigger("my.event");
    }
});

var ModelA = Backbone.Model.extend({
    initialize: function() {
        App.Events.on("my.event" this.onEvent, this);
    },
    onEvent: function() {}
});
var ModelB = Backbone.Model.extend({
    initialize: function() {
        App.Events.on("my.event" this.onEvent, this);
    },
    onEvent: function() {}
});

Upvotes: 1

Related Questions