user3279058
user3279058

Reputation: 559

model events are not working in Marionette

I am using Marionette.js for making an Application.

model event was not triggering If I change Attribute value in view.initialize function just like in the following way.

var ChartsView = Marionette.CompositeView.extend({
   initialize:function(){
      this.model.set({"currentJson":""},{silent:true});
      this.model.set({"currentJson":"test"});
   },
   modelEvents:{
    "change:currentJson":"settingOtherAttributesInSameModel"
  },
  settingOtherAttributesInSameModel:function(){
     console.log("This is Testing");
  }
});

while instance creation time, I am passing the model object just like in the following way.

chartsViewObj=new ChartsView({el:$('#plannerTablePlace'),model:chartsModelObj});

I don't know, why model event was not working.

I didn't find,where I wrong.

can anyone help me.

Upvotes: 2

Views: 920

Answers (1)

Rida BENHAMMANE
Rida BENHAMMANE

Reputation: 4129

Your problem is that the delegateEvents function that bind the view events is called after the initialize function. Thus when you do this :

initialize:function(){
  this.model.set({"currentJson":""},{silent:true});
  this.model.set({"currentJson":"test"});
}

The attribute is set and the change event is triggered but the view events are not yet bound.


So if you want the events to be triggered, do like this :

chartsViewObj=new ChartsView({el:$('#plannerTablePlace'),model:chartsModelObj});

chartsModelObj.set({"currentJson":""},{silent:true}); // this will not trigger the event because of 'silent:true'
chartsModelObj.set({"currentJson":"test"});

Upvotes: 1

Related Questions