jaxkodex
jaxkodex

Reputation: 504

Marionette modelEvents not firing

I am trying to select a element on click from composite view but I can not make the itemView model event to work.

Here is some part of mi code

var resultItemView = Marionette.ItemView.extend({
    template: _.template('<b><%=institucionEducativaNombre%></b>'),
    events: {
        'click': 'onClick'
    },
    onClick: function (evt) {
        console.log(this.model.toJSON());
        this.model.set('institucionEducativaNombre', 'asdf');
        console.log(this.model.toJSON());
    }
});
var resultView = Marionette.CompositeView.extend({
    template: _.template(''),
    childView: resultItemView,
    modelEvents: {
        'change': 'pickSchool'
    },
    collectionEvents: {
        'add': 'added'
    },
    pickSchool: function () {
        console.log('This should be triggered');
        console.log(evt, args);
    },
    added: function (evt) {
        console.log('new added');
    }
});

Thanks in advance.

Upvotes: 0

Views: 963

Answers (1)

jaxkodex
jaxkodex

Reputation: 504

Never mind, It seems that the modelEvents attribute is used to bind events in the model of the view not the models in the collection that it holds.

https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view.js#L116

With this, I changed the event to listen and it worked as I wanted.

var resultItemView = Marionette.ItemView.extend({
    template: _.template('<b><%=institucionEducativaNombre%></b>'),
    events: {
        'click': 'onClick'
    },
    onClick: function (evt) {
        this.model.collection.trigger('select', this.model);
    }
});
var resultView = Marionette.CompositeView.extend({
    template: _.template(''),
    childView: resultItemView,
    collectionEvents: {
        'add': 'added',
        'select': 'pickSchool'
    },
    pickSchool: function (model) {
        console.log('This should be triggered');
        console.log(model.toJSON());
    },
    added: function (evt) {
        console.log('new added');
    }
});

Upvotes: 2

Related Questions