【问题标题】:Marionette modelEvents not firing木偶模型事件未触发
【发布时间】:2014-08-30 18:05:13
【问题描述】:

我正在尝试从复合视图中选择单击时的元素,但无法使 itemView 模型事件起作用。

这是mi代码的一部分

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');
    }
});

提前致谢。

【问题讨论】:

    标签: events backbone.js marionette


    【解决方案1】:

    没关系,modelEvents 属性似乎用于绑定视图模型中的事件,而不是它所拥有的集合中的模型。

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

    有了这个,我将事件更改为监听,它可以按我的意愿工作。

    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');
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 2013-07-15
      • 2014-08-15
      • 1970-01-01
      相关资源
      最近更新 更多