backbone.js 事件和 el
好吧,我已经阅读了其他几个有关 Backbone 视图和事件未被解雇的问题,但遗憾的是我仍然没有得到它。我已经用 Backbone 搞乱了大约一天,所以我确信我错过了一些基本的东西。这是我正在使用的jsfiddle: http://jsfiddle.net/siyegen/e7sNN/3/
(function($) {
var GridView = Backbone.View.extend({
tagName: 'div',
className: 'grid-view',
initialize: function() {
_.bindAll(this, 'render', 'okay');
},
events: {
'click .grid-view': 'okay'
},
okay: function() {
alert('moo');
},
render: function() {
$(this.el).text('Some Cow');
return this;
}
});
var AppView = Backbone.View.extend({
el: $('body'),
initialize: function() {
_.bindAll(this, 'render', 'buildGrid');
this.render();
},
events: {
'click button#buildGrid': 'buildGrid'
},
render: function() {
$(this.el).append($('<div>').addClass('gridApp'));
$(this.el).append('<button id="buildGrid">Build</button>');
},
buildGrid: function() {
var gridView = new GridView();
this.$('.gridApp').html(gridView.render().el);
}
});
var appView = new AppView();
})(jQuery);
好的 GridView 上的
事件不会触发,我假设是因为第一次绑定事件时 div.grid-view
不存在。我应该如何处理动态构建在视图上的事件的绑定和触发? (另外,这是一个简短的例子,但是如果我做了任何不应该做的事情,请随意对我大喊大叫)
Okay, so I've read several other questions regarding Backbone views and events not being fired, however I'm still not getting it sadly. I been messing with Backbone for about a day, so I'm sure I'm missing something basic. Here's a jsfiddle with what I'm working with:
http://jsfiddle.net/siyegen/e7sNN/3/
(function($) {
var GridView = Backbone.View.extend({
tagName: 'div',
className: 'grid-view',
initialize: function() {
_.bindAll(this, 'render', 'okay');
},
events: {
'click .grid-view': 'okay'
},
okay: function() {
alert('moo');
},
render: function() {
$(this.el).text('Some Cow');
return this;
}
});
var AppView = Backbone.View.extend({
el: $('body'),
initialize: function() {
_.bindAll(this, 'render', 'buildGrid');
this.render();
},
events: {
'click button#buildGrid': 'buildGrid'
},
render: function() {
$(this.el).append($('<div>').addClass('gridApp'));
$(this.el).append('<button id="buildGrid">Build</button>');
},
buildGrid: function() {
var gridView = new GridView();
this.$('.gridApp').html(gridView.render().el);
}
});
var appView = new AppView();
})(jQuery);
The okay
event on the GridView does not fire, I'm assuming because div.grid-view
does not exist when the event is first bound. How should I handle the binding and firing of an event that's built on a view dynamically? (Also, it's a short example, but feel free to yell at me if I'm doing anything else that I shouldn't)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是 GridView 上的事件:
说:
事件与此 来自
backbone.js
的片段:所以
.grid-view
元素必须包含在 GridView 的this.el
中,并且您的this.el
是。如果您将
事件
更改为:您会听到牛的声音(或者在阅读警报后“在脑海中听到它们的声音”,具体取决于这个问题让您有多疯狂)。
固定小提琴: http://jsfiddle.net/ambigously/5dhDW/
Your problem is that the events on GridView:
say:
The events are bound with this snippet from
backbone.js
:So the
.grid-view
element has to be contained within your GridView'sthis.el
and yourthis.el
is<div class="grid-view">
. If you change yourevents
to this:you'll hear your cows (or "hear them in your mind" after reading the alert depending on how crazy this problem has made you).
Fixed fiddle: http://jsfiddle.net/ambiguous/5dhDW/