0

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

Sonny Chivas
  • 17
  • 10
  • What you mean by mainModel? In your diagram mainModel should be a collection if you using backbone approach. Could you describe your issue more in details? – vvahans Oct 02 '14 at 11:26
  • Ok, it isn't a collection. I used this to control and communication betwen the another model. – Sonny Chivas Oct 02 '14 at 11:31
  • As I understand you need event aggregator. This is the best way for communication between application's parts. Look at [this](https://github.com/marionettejs/backbone.wreqr) lib, it might be helpful. Marionette uses it. – vvahans Oct 02 '14 at 11:39
  • That is interesting, is like a global object to de communication, is it not?. I need to see if I can use in my app. Thank. – Sonny Chivas Oct 02 '14 at 12:33
  • Can I used the event aggregator without Marionette? – Sonny Chivas Oct 02 '14 at 13:00
  • Yes you can. Read documentation and try to build some working example. – vvahans Oct 02 '14 at 13:03
  • You can extend Backbone Events and build a very lightweight version yourself: http://backbonejs.org/#Events – kinakuta Oct 02 '14 at 15:49
  • I have a similar problem, but a different question here: http://stackoverflow.com/questions/26172757/how-to-organize-backbone-collection-with-a-specific-selection – Andrew Oct 03 '14 at 04:37

1 Answers1

1

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() {}
});
jcreamer898
  • 8,109
  • 5
  • 41
  • 56