尝试导航时模型方法错误
Model method error while trying to navigate
我有几个骨干模型呈现在一个集合视图,我也有一个路由,应该呈现该模型的视图。这里是视图
resume.js
// this renders a single model for a collection view
var ResumeView = Backbone.View.extend({
model: new Resume(),
initialize: function () {
this.template = _.template($('#resume').html());
},
render: function () {
this.$el.html(this.template(this.model.toJSON));
return this;
}
});
#resume template
<section id="resume">
<h1><%= profession %></h1>
<!-- !!!!! The link for a router which should navigate to ShowResume view -->
<a href="#resumes/<%= id %>">View Details</a>
</section>
集合视图:
var ResumeList = Backbone.View.extend({
initialize: function (options) {
this.collection = options.collection;
this.collection.on('add', this.render, this);
// Getting the data from JSON-server
this.collection.fetch({
success: function (res) {
_.each(res.toJSON(), function (item) {
console.log("GET a model with " + item.id);
});
},
error: function () {
console.log("Failed to GET");
}
});
},
render: function () {
var self = this;
this.$el.html('');
_.each(this.collection.toArray(), function (cv) {
self.$el.append((new ResumeView({model: cv})).render().$el);
});
return this;
}
});
上面的代码工作得很完美,并且完全满足了我的需求——从本地json服务器获取模型数组,每个模型都显示在集合视图中。然而,当我试图浏览上面模板中的链接时,麻烦就来了。路由器来了:
var AppRouter = Backbone.Router.extend({
routes: {
'': home,
'resumes/:id': 'showResume'
},
initialize: function (options) {
// layout is set in main.js
this.layout = options.layout
},
home: function () {
this.layout.render(new ResumeList({collection: resumes}));
},
showResume: function (cv) {
this.layout.render(new ShowResume({model: cv}));
}
});
最后是ShowResume
视图:
var ShowResume = Backbone.View.extend({
initialize: function (options) {
this.model = options.model;
this.template = _.template($('#full-resume').html());
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
}
});
我没有为这个视图提供模板,因为它相当大,但错误如下:每当我尝试导航到一个链接时,视图试图渲染,但返回我以下错误:Uncaught TypeError: this.model.toJSON is not a function.
我怀疑我的showResume
方法在路由器是无效的,但我实际上不能得到如何使它以正确的方式工作。
您正在传递url 'resumes/:id'
的字符串id
作为视图的模型。
这应该能解决问题。
showResume: function (id) {
this.layout.render(new ShowResume({
model: new Backbone.Model({
id: id,
profession: "teacher" // you can pass data like this
})
}));
}
但是你应该从控制器中获取数据并在视图中做出相应的反应。
var AppRouter = Backbone.Router.extend({
routes: {
'*otherwise': 'home', // notice the catch all
'resumes/:id': 'showResume'
},
initialize: function(options) {
// layout is set in main.js
this.layout = options.layout
},
home: function() {
this.layout.render(new ResumeList({ collection: resumes }));
},
showResume: function(id) {
// lazily create the view and keep it
if (!this.showResume) {
this.showResume = new ShowResume({ model: new Backbone.Model() });
}
// use the view's model and fetch
this.showResume.model.set('id', id).fetch({
context: this,
success: function(){
this.layout.render(this.showResume);
}
})
}
});
此外,this.model = options.model;
是不必要的,因为Backbone自动拾取model
, collection
, el
, id
, className
, tagName
, attributes
和events
,用它们扩展视图。
相关文章:
- jQuery AJAX Post方法错误(语法错误)
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 谷歌地图javascript多个方法错误
- 检查对话框是否为 Open 会引发“初始化前无法在对话框上调用方法”错误
- 当输入值已更改时,html() 方法错误
- 对象不支持 IE7 jQuery 中的此属性或方法错误
- jQuery.validation 在生日年龄验证中添加方法错误
- 木偶/骨干应用程序中没有方法错误
- 使用此 javascript 在 IE7 中收到“对象不支持此属性或方法”错误,以及选项卡未正确定位的问题
- “初始化前无法在工具提示上调用方法”错误,当我添加 jquery-ui 库时
- 谷歌图表绘制()方法错误类型时给定数据表
- Javascript:随机“对象没有方法”错误,即使使用 jquery.getScript()
- 在对象中调用对象方法 - 错误:无法读取未定义的属性
- 调用原型的重写方法,然后调用下一个原型的重写方法错误
- 方法错误:未定义方法名称
- “对象 [对象对象] 没有方法”错误在线,但不在本地主机上!为什么
- 对象在没有冲突后没有方法错误
- AJAX POST请求上没有方法错误轨道
- “
没有方法”错误,用于似乎存在的对象 - 对象在 JavaScript 中没有方法错误