【发布时间】:2011-04-28 23:37:37
【问题描述】:
我正在尝试使用 Backbone JS,到目前为止,一切似乎都很有意义并且工作顺利。
但是使用下面的代码,我的自定义事件似乎没有触发。以下代码中的任何内容都可以说明为什么会这样?我需要在视图中“初始化”任何东西吗?代码/结构上的任何其他指针也很酷。以下是我的完整 JS/HTML。
JS
var Todo = Backbone.Model.extend({});
var TodoCollection = Backbone.Collection.extend({
model: Todo,
url: '/Home/Todos'
});
var AppView = Backbone.View.extend({
// where it should listen, required?
el: $(".content"),
events: {
"keypress #new-todo": "enter"
},
initialize: function () {
// _.bindAll(this, "render", "createOnEnter");
// this.collection.bind("all", this.render);
},
hi: function () {
alert('ohai');
},
render: function () {
var lis = '';
$.each(this.collection.models, function () {
lis += '<li>' + this.get('Text') + '</li>';
});
$('#todo-list').append(lis);
return this.el;
},
enter: function (e) {
alert('hi');
}
});
var TodoController = Backbone.Controller.extend({
routes: {
"": "todos"
},
initialize: function (options) { },
todos: function () {
var todolist = new TodoCollection();
todolist.fetch({
success: function (data) {
var appview = new AppView({ collection: data });
appview.render();
}
});
}
});
$(function () {
window.app = new TodoController();
Backbone.history.start();
});
HTML
<div id="todoapp">
<div class="content">
<input id="new-todo" placeholder="What needs to be done?" type="text" />
<div id="todos">
<ul id="todo-list">
</ul>
</div>
<a href="#">say hey</a>
</div>
</div>
【问题讨论】:
标签: jquery backbone.js