在 Backbone View 中触发 el 事件
我有一个创建并填充选择列表的视图。我想在 Change 事件上绑定一个事件(当用户选择不同的选项时。
它只是输出与此接近的内容:
<select>
<option value="id">ID Name</option
</select>
视图:
App.Views.SessionList = Backbone.View.extend({
tagName: 'select',
id: 'sessionList',
events: {
'change': 'selectionChanged'
},
initialize: function () {
// Load Collection
_.bindAll(this, 'selectionChanged');
this.template = _.template($('#optionsTemplate').html());
this.Sessiontemplate = _.template($('#session_template').html());
this.SelectedSessiontemplate = _.template($('#selected_session_template').html());
// Create Collection
this.SessionList = new App.Collections.SessionList();
// Set Bindings
this.SessionList.bind('add', this.addSession, this);
this.SessionList.bind('reset', this.addSessions, this);
this.render();
this.SessionList.fetch();
},
render: function () {
return this;
},
addSession: function (item, num) {
if (num === 0) {
$(this.el).append(this.SelectedSessiontemplate(item.toJSON()));
console.log("Selected");
}
else {
$(this.el).append(this.Sessiontemplate(item.toJSON()));
}
},
addSessions: function () {
var self = this;
// Add all Rows
for (var i = 0; i < self.SessionList.models.length; i++) {
this.addSession(self.SessionList.models[i], i);
}
},
selectionChanged: function (e) {
var field = $(e.currentTarget);
var value = $("option:selected", field).val();
}
});
会话模板只是简单:
<option value="{{Id}}">{{Name}}</option>
该事件永远不会被触发,而且似乎它不是'我想在选择列表发生变化时触发它,
我最初认为我可能遇到类似的问题: backbone.js 事件和 el,但是在这种情况下不起作用。
I have a view that creates and populates a Selectlist. I want to bind an event on the Change event (When the user selects a different Option.
It simply outputs something close to this:
<select>
<option value="id">ID Name</option
</select>
The View:
App.Views.SessionList = Backbone.View.extend({
tagName: 'select',
id: 'sessionList',
events: {
'change': 'selectionChanged'
},
initialize: function () {
// Load Collection
_.bindAll(this, 'selectionChanged');
this.template = _.template($('#optionsTemplate').html());
this.Sessiontemplate = _.template($('#session_template').html());
this.SelectedSessiontemplate = _.template($('#selected_session_template').html());
// Create Collection
this.SessionList = new App.Collections.SessionList();
// Set Bindings
this.SessionList.bind('add', this.addSession, this);
this.SessionList.bind('reset', this.addSessions, this);
this.render();
this.SessionList.fetch();
},
render: function () {
return this;
},
addSession: function (item, num) {
if (num === 0) {
$(this.el).append(this.SelectedSessiontemplate(item.toJSON()));
console.log("Selected");
}
else {
$(this.el).append(this.Sessiontemplate(item.toJSON()));
}
},
addSessions: function () {
var self = this;
// Add all Rows
for (var i = 0; i < self.SessionList.models.length; i++) {
this.addSession(self.SessionList.models[i], i);
}
},
selectionChanged: function (e) {
var field = $(e.currentTarget);
var value = $("option:selected", field).val();
}
});
The Session Template is just simply:
<option value="{{Id}}">{{Name}}</option>
The event never gets triggered, and It seems that it isn't getting binded correctly. I want to trigger it on the change of the select list.
I originally thought I may be having an issue similar to:
backbone.js events and el, however it doesn't work in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接调试您的问题很困难,因为我们没有所有信息(
SessionList
看起来像什么......模板等)。但是,我已将您的示例与事件确实有效的一些代码配对。希望你能从那里建立它?如果您想到达一个对您来说失败的地方,您可以fork jsFiddle,我们可以尝试进一步提供帮助。
有了该代码,每次选择一个项目时,您都会获得一个控制台日志,其中包含该值。
既然你没有明白这一点,我猜你看到了异常或类似的情况?你的调试器会给你任何提示吗?
祝你好运!
It is hard to debug your problem directly, because we don't have all of the information (what does
SessionList
look like... the templates, etc).BUT, I have paired down your example to some code where the event does, indeed work. Hopefully, you can build it up from there? You can fork the jsFiddle if you want to get to a place where it fails for you and we can try to help further.
Given that code, you will get a console log with the value every time you select an item.
Since you aren't getting that, I am guessing you are seeing an exception or something like that? Does your debugger give you any hints?
Good luck!