【发布时间】:2016-02-08 21:51:48
【问题描述】:
我在视图中添加了一个模型属性,如下所示:
app.ActorView = Backbone.View.extend({
modelImg:'', ....
我跳到渲染部分,因为其他一切都很好:
render: function () {this.$el.html(this.template({
modImg: this.modelImg.toJSON(),
mod: this.model.toJSON(),
movies: this.collection.toJSON()}
视图中的每个模型(模型、集合和 modelimg)都在我的项目的根部分中正确获取:
modActor.fetch().done(function () {
modActorMovies.fetch().done(function () {
modImgActor.fetch().done(function () {
var actorView = new app.ActorView({
modelImg: modImgActor,<--problematic model
model: modActor,
collection: modActorMovies
});
我的 modImgActor 定义如下:
app.ActorImg= Backbone.Model.extend({
url: "http://umovie.herokuapp.com/unsecure/actors/272994458/movies",
parse: function (response) {
return response.results[0];
}
});
问题是当我在 modelImg 上使用 toJson() 函数时。出现如下错误:this.modelImg.toJSON is not a function
模型是如何用它的 url 定义的?
【问题讨论】:
-
您在视图中添加了一个 modelImg 属性,但我没有看到将模型 (modImgActor) 分配给该属性。字符串没有方法 toJSON()。只有一些选项,如模型将由构造函数自动分配)。扩展视图时可以添加模型而不是字符串。
-
如果
toJSON不是函数,那么this.modelImg不是模型,因为all backbone models havetoJSONmethod。
标签: javascript backbone.js view model