【问题标题】:View events not firing - backbone查看未触发的事件 - 主干
【发布时间】:2014-11-11 16:35:12
【问题描述】:

我现在正在构建一个视图,当我单击 .organisation 链接时,我想触发我的编辑事件,但是在单击此元素时,什么都没有触发,我不明白为什么。

这是构建我的视图的代码,

App.Views.groupsView = Backbone.View.extend({

el: '.app',

template: _.template( $('#tpl-groups-base').html() ),

events: {

},

initialize: function(options) {
    this.render();
},

render: function() {
    this.$el.html( this.template() );

    var orgTab = new App.Views.OrganisationsTab({
        collection : new App.Collections.Organisations
    });

},

});

App.Views.OrganisationsTab = Backbone.View.extend({

el : '#organisations',

initialize: function() {
    App.Collections.OrganisationCollection = this.collection;
    this.collection.fetch();

    this.collection.on('sync', this.render, this);
},

render: function() {
    this.addAll();
    return this;
},

addAll: function() {
    App.Collections.OrganisationCollection.each(this.addOne, this);
},

addOne: function(organisation) {

    var view = new App.Views.OrganisationView({
        model : organisation
    });

    this.$el.append( view.render().el );
}

});

App.Views.OrganisationView = Backbone.View.extend({

    tagName: 'a',
    className:'group group--panel col-sm-3 organisation',

    template : _.template( $('#tpl-single-group').html() ),

    events: {
        "click body" : "edit",
    },

    initialize: function() {
        this.listenTo(this.model, 'change', this.render);
        this.listenTo(this.model, 'destory', this.remove);
    },

    render: function() {

        this.$el.html( this.template({
            group: this.model.toJSON()
        }));

        return this;
    },

    edit: function(e) {
        e.preventDefault();
        console.log(this.model);
    }

});

为什么我无法点击在最终视图中创建的组织并触发我的编辑功能?

【问题讨论】:

  • 显然a 需要有一个 href 才能有点击事件。
  • 如果相信events标签中写的内容,a必须包含body标签。

标签: javascript backbone.js backbone-views


【解决方案1】:

如果你想将点击事件监听器附加到视图的 el 的子元素上

events: {
    "click .organisation" : "edit",
},

如果你想把它附加到el上

events: {
        "click" : "edit",
 },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多