骨干视图事件未触发 - 不确定原因

时间:2012-06-26 16:56:15

标签: javascript backbone.js backbone-events backbone-views

我正在尝试触发click事件,但它无效。也许有人可以看到我无法做到的事情。

ConnectionView = GlobalView.extend({
    tagName: 'div',

    events: {
        "click .social-links": "check"
    },

    initialize: function() {
        this.render();

        this.model.bind("change", this.render);
    },

    render: function() {
        // Compile the template using underscore
        var template = _.template($("#connection-template").html(), this.model.toJSON());

        // Load the compiled HTML into the Backbone "el"
        $(this.el).html(template);        
    },

    check: function(e) {
        e.preventDefault();

        alert("woot");
    }
});

这是它正在拉动的模板:

<script id="connection-template" type="text/template">
    <a id="link-<%= alt %>" class="social-links" href="<%= url.replace('||state||', state).replace('||timestamp||', time) %>">add <%= name %></a>
</script>

以下是将ConnectionView放入DOM的视图:

ConnectionsView = GlobalView.extend({
    tagName: 'div',

    initialize: function(){
        this.collection.bind("reset", this.render, this);
    },

    render: function(){        
        var $connections = $("<div class='external-accounts'>");

        this.collection.each(function (model) {            
            var conn = new ConnectionView({model: model});
            $connections.append(conn.el);
        });

        // Compile the template using underscore
        var template = _.template($("#connections-template").html());
        // Load the compiled HTML into the Backbone "el"
        $(this.el).html(template({
            connectionsList: $connections.outer()
        }));
    },

    destroy: function() {
        this.constructor.__super__.destroy.apply(this);

    }
});

1 个答案:

答案 0 :(得分:8)

您的问题是如何填写ConnectionsView el。你这样做了:

// Load the compiled HTML into the Backbone "el"
$(this.el).html(template({
    connectionsList: $connections.outer()
}));

我不知道.outer()究竟是什么,但这并不重要。重要的是,所有内容都通过编译的Underscore template()函数,这意味着一切都将在进入页面的过程中转换为字符串。一旦$connections中的DOM元素出现在字符串中,事件绑定就会丢失,并且不再有任何作用。

考虑这个例子:

  

http://jsfiddle.net/ambiguous/4tjTm/

在那里我们这样做:

var $connections = $('<div>');
var conn = new ConnectionView({ model: m });
$connections.append(conn.el);

var template = _.template('<%= connectionsList %>');
$('#view-goes-here').append(template({
    connectionsList: $connections.html()
}));

将您的ConnectionView添加到该页面。事件在那里不起作用,因为template()返回一个字符串,并且该字符串被解析为最终在页面中的DOM元素。但是,如果我们将$connections直接添加到页面:

  

http://jsfiddle.net/ambiguous/AKkCJ/

使用类似的东西:

var $connections = $('<div>');
var conn = new ConnectionView({ model: m });
$connections.append(conn.el);
$('#view-goes-here').append($connections);

事件按预期工作。

所以现在我们知道出了什么问题。但是我们该如何解决呢?我们需要做的就是调整您的ConnectionsView,直接将$connections添加到DOM,如下所示:

render: function() {
    // Move <div class='external-accounts'> into your #connections-template.
    var template = _.template($("#connections-template").html());
    this.$el.html(template());
    var $external = this.$el.find('.external-accounts');
    this.collection.each(function (model) {
        var conn = new ConnectionView({model: model});
        $external.append(conn.el);
    });
    return this; // This is conventional
}

<div class="external-accounts">推入ConnectionsView模板,然后将ConnectionView直接插入其中。这种方式你总是使用DOM元素,字符串不知道事件但DOM元素。

相关问题