Search code examples
javascriptbackbone.jsmarionette

Marionette - modelEvents not working


I'm trying to use the modelEvents part of the ItemView, but for some reasons, it does not work.

I set up a simple test case, where I have a CollectionView with 4 ItemViews. When I click on one of the ItemView, then I change the first ItemView's model within the CollectionView (this.collection.models[0].attributes = {...}). Even though the model's content changed, the change event does not fire.

Inside the ItemView I tried using the modelEvents with different options (modelEvents: {"change": "functionToCall"} ; modelEvents: {"change:attributes": "functionToCall"}), tried to use a simple listenTo, in the initialization function:

this.listenTo(this.model, "change", function(){
    this.functionToCall();
});

or

this.listenTo(this.model.attributes, "change", function(){
    this.functionToCall();
});

but none of them works.

It's not the first time that such hashes don't really want to work (such as the ui: {} and a few others) and I don't know what the problem is (I'm using Marionette 2.4.3 and jQuery 2.1.4 (just because Marionette needs it)).


Solution

  • Based on the documentation found in Backbone (http://backbonejs.org/#Model-attributes), I believe that the issue may be related to you updating the model via the attributes property directly. The documentation clearly states the models should be updated using model.set(..) - and not doing so will not fire the events associated with changing model data.