Javascript Backbone.js 模型以查看连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6697483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone.js Model to View Connection
提问by felix
I am a Backbone.js newbie. I was just playing around with it. I would like to know whether the model is related with the View. In the provided todosexample, I see in the addOne method, a new View is created and associated with the newly created model and appended.
我是 Backbone.js 新手。我只是在玩弄它。我想知道模型是否与视图相关。在提供的todos示例中,我在 addOne 方法中看到,一个新的 View 被创建并与新创建的模型相关联并附加。
window.AppView = Backbone.View.extend({
// view code
addOne: function(todo) {
var view = new TodoView({model: todo});
this.$("#todo-list").append(view.render().el);
}
}
When I tried to do a similar thing, I got an error saying "bind method cannot be found on undefined".
当我尝试做类似的事情时,我收到一条错误消息,提示“无法在未定义的情况下找到绑定方法”。
window.TodoView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render', 'close');
this.model.bind('change', this.render); // I got the error at this place.
this.model.view = this;
}
});
In order to solve this, I got to pass the newly created model as a param to the view constructor and I got to do this.model = task
inorder to associate it.
为了解决这个问题,我必须将新创建的模型作为参数传递给视图构造函数,我必须这样做this.model = task
才能关联它。
window.TodoView = Backbone.View.extend({
initialize: function(task) {
_.bindAll(this, 'render', 'close');
this.model = task
this.model.bind('change', this.render);// now there is no error
this.model.view = this;
}
});
window.AppView = Backbone.View.extend({
insertTask:function(){
var newTask, newTaskView;
newTask = new Task(JSON.parse(xhr));
Tasks.create(newTask);
newTaskView = new TaskView({ model: newTask });
$("#todo_list").append(newTaskView.render().el);
this.input.val('');
}
});
});
But the todos example, do not have something like that. How is the new model associated with the new view implicitly in the todos example?
但是 todos 示例,没有类似的东西。在 todos 示例中,新模型如何与新视图隐式关联?
Thanks
谢谢
回答by Factor Mystic
It's not implicit at all, it's explicit in this line right here:
它根本不是隐含的,它在这里的这一行中是明确的:
var view = new TodoView({model: todo});
This is creating a new TodoView
view and setting its model
property to the addOne
function's only parameter (todo
, which is a model).
这是创建一个新TodoView
视图并将其model
属性设置为addOne
函数的唯一参数(todo
,这是一个模型)。
Whenever a new model is added to the Todos
collection, the addOne
method is called with the new model as the parameter.
每当将新模型添加到Todos
集合中时,addOne
都会使用新模型作为参数调用该方法。
Todos.bind('add', this.addOne);
Then, in addOne
, a new view is created for that model and the relationship is explicity set, via {model: todo}
. This is what you're missing from your version of the code, I suspect.
然后,在 中addOne
,为该模型创建一个新视图,并通过 显式设置关系{model: todo}
。我怀疑这就是您的代码版本中缺少的内容。
What you appear to be attempting to do is link up the view and the model in the view's init function, and that's fine, but you're on your own if you do that- which means you need to set up the model <-> view relationship yourself (which you have solved by passing the model as a parameter to the view init function).
你似乎试图做的是在视图的 init 函数中链接视图和模型,这很好,但是如果你这样做,你就靠自己了 - 这意味着你需要设置模型 <->自己查看关系(您已经通过将模型作为参数传递给视图初始化函数来解决这个问题)。