好的,所以我在獲取事件綁定與Coffeescript和Backbone一起工作時遇到了一些困難。我有一種感覺,它與我如何初始化一切有關;我覺得事件代表團甚至沒有運行。似乎無法正確綁定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
。我的破解(在代碼中)只是通過initialize
函數中的jQuery進行綁定。
任何想法是怎麼回事?我希望這是一個簡單的錯字或錯誤的初始化。
你,先生,是CoffeeScript的堆棧溢出問題的蝙蝠俠!謝謝! – CamelBlues 2013-03-24 20:55:30