似乎无法在Coffeescript中正确绑定Backbone事件

时间:2013-03-24 20:34:08

标签: backbone.js coffeescript backbone-events event-binding

好的,所以我在使用Coffeescript和Backbone进行事件绑定时遇到了一些困难。我有一种感觉,它与我如何初始化一切有关;我觉得事件授权甚至没有被运行。

这是我的观看代码:

$ ->
  class AppName.TitleView extends Backbone.View
    template: JST['templates/title']
    collection: new AppName.Members
    events:
      "keypress #search"      : "search",
    initialize: =>
      $('#search').keypress(@search) #doing it manually as a hack
    search: ->
      console.log('search handler call')
    render: =>
      $('#app').html(@template)
      @delegateEvents() #this doesn't seem to do anything, nor does @delegateEvents(@events)
      @

在编译时,看起来像这样:

(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = Object.prototype.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

  $(function() {
    AppName.TitleView = (function(_super) {

      __extends(TitleView, _super);

      function TitleView() {
        this.render = __bind(this.render, this);
        this.initialize = __bind(this.initialize, this);
        TitleView.__super__.constructor.apply(this, arguments);
      }

      TitleView.prototype.template = JST['templates/title'];

      TitleView.prototype.collection = new AppName.Members;

      TitleView.prototype.events = {
        "keypress #search": "search",
      };

      TitleView.prototype.initialize = function() {
        return $('#search').keypress(this.search);
      };


      TitleView.prototype.search = function() {

        console.log('search handler call'););
        });
      };

      TitleView.prototype.render = function() {
        $('#app').html(this.template);
        this.delegateEvents();
        return this;
      };

      return TitleView;

    })(Backbone.View);
  });

}).call(this);

AppName.TitleView从我的路由器(主要由app.coffee启动)开始执行:

$ ->
  class AppName.Router extends Backbone.Router
    routes:
      ".*": "main"

    main: ->
      @titleView ||= new AppName.TitleView el: $('#app')[0]
      @titleView.render()

但是对于我的生活,我无法从Backbone Events绑定到#search。我的hack(在代码中)只是通过initialize函数中的jQuery进行绑定。

知道发生了什么事吗?我希望这是一个简单的拼写错误或初始化错误。

1 个答案:

答案 0 :(得分:5)

使用jQuery on的委托形式将视图事件绑定到视图el。这意味着您在视图的events对象中提及的所有内容必须位于视图的el内,否则将不会触发事件处理程序。

默认情况下,视图的el为空<div>,视图会在需要时创建<div>并将事件绑定到<div>。您可能会注意到您未在代码中的任何位置使用this.elthis.$el;你的事件被正确约束但是它们被绑定到你放在DOM中的任何东西,所以看起来事件不起作用。

出现两种直接可能性:

  1. 使用#app作为视图的el

    class AppName.TitleView extends Backbone.View
      #...
      el: '#app'
      #...
      render: =>
        @$el.html(@template)
        @
    
  2. 以通常的Backbone方式执行操作并为您的视图创建el

    class AppName.TitleView extends Backbone.View
      #...
      render: =>
        @$el.html(@template)
        @
    
    v = new AppName.TitleView
    $('#app').append(v.render().el)
    
  3. 我建议使用后者,因为它更容易管理,并且可以帮助您避免将多个视图附加到同一个DOM元素(以及倾向于来自它的僵尸)。

    此外,@template几乎总是一个函数,所以你想说:

    $('#app').html(@template())