我对Backbone View的理解是每个需要显示模型数据的html元素都可以是一个View本身。backbone.js如何将el设置为特定的div ID
我希望创建视图链接到特定的div来显示模型数据。 我的问题是,如果我对el使用其他的“body”以外的代码,则代码不起作用。
下面的代码无法正常工作:
$(document).ready(function() {
var ToDo = Backbone.Model.extend({
defaults: { "date": "today",
"task": ""
},
initialize: function() {}
});
var ToDoList = Backbone.Collection.extend({
model: ToDo
});
var ToDoListView = Backbone.View.extend({
el: "#view1",
initialize: function(myTodoList) {
this.todolist = myTodoList;
this.todolist.bind('add', this.render, this);
},
render: function() {
text = this.todolist.toJSON();
string = JSON.stringify(text);
$(this.el).append(string);
return this;
},
events: {
"keypress #new-todo": "createOnEnter"
},
createOnEnter: function(e) {
if (e.keyCode != 13) return;
if (!$("#new-todo").val()) return;
this.todolist.add({"task": $("#new-todo").val()});
$("#new-todo").val('');
}
});
$("#new-todo").focus();
var todolist = new ToDoList();
var myToDoListView = new ToDoListView(todolist);
});
但是,如果我为el使用“body”,它会按我的意愿工作。
如何将el设置为特定的div?
尼斯之一。谢谢。 – tucson