私はいくつかのバックボーンモデルをコレクションビューでレンダリングし、そのモデルのビューをレンダリングするルートも持っています。だから、ここに来ビュー移動中のモデルメソッドのエラー
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>
Collectionビュー:
上記のコードは完璧に動作し、私は必要なものを正確に行い
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サーバーからフェッチされ、各mo delはコレクションビュー内に表示されます。ただし、上記のテンプレートでリンクをナビゲートしようとすると、問題が発生します。ここでは、ルータが来る:
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
メソッドが無効であると思われますが、正しい方法で実際に動作させる方法が得られません。
もう一度ありがとうEmile!奇妙なものですが、テンプレートは上記のものとほぼ同じですが、「職業は定義されていません」というエラーが返されます – AlexNikolaev94
@ AlexNikolaev94私はそれがうまくいく方法を提供するために答えを更新しましたが、むしろルーティングやビューのための良いパターンを示すオンラインをチェックします。 –
ありがとう、エミール!それはうまくいきましたが、以前の質問で言及したものと同じエラーを返します: 'A" url "プロパティまたは関数を指定する必要があります。コレクションから受け取ったデータを取得する方法を把握する必要があります – AlexNikolaev94