以下は、バックボーン アプリケーションで何が起こっているかの基本的な概要です。
- ルーターは新しいプロファイル ビューを作成し、ユーザー名を渡します。
- 新しいプロファイルが初期化されると、渡されたユーザー名で新しいユーザーが作成されます。
- その後、プロファイルはこのユーザーに関する詳細を取得します。
- ここで迷ってしまいます…
これらは私の質問です:
- 意図したとおりにバックボーンを使用していますか、それとも構造を再考する必要がありますか?
- プロファイル ビューには、
render
ハンドルバーを使用する機能があります。ビューをレンダリングするには、ユーザーの詳細をハンドルバーに渡す必要があります。これを行う最善の方法は何ですか? - 関数を実際にどこで呼び出す必要があり
render
ますか?
これが私のコードです(コメントされた質問に注意してください):
ルーター
var Router = Backbone.Router.extend({
routes: {
":username": "profile"
},
profile: function (username) {
var profile = new Profile({username: username});
}
});
モデル
var User = Backbone.Model.extend({
urlRoot: 'http://api.example.com/user'
});
意見
var Profile = Backbone.View.extend({
el: "#content section",
initialize: function(username) {
var user = new User({id: this.options.username});
user.fetch({
beforeSend: authenticate,
success: function() {
//*************************************************************
// How do I make this result available to the render function?
//*************************************************************
},
});
},
render: function() {
alert(JSON.stringify(this.context));
var source = $("#profile").html();
var template = Handlebars.compile(source);
//**********************************************************************
// How do I set context equal to the result of the initialize function?
//**********************************************************************
var html = template(context);
$("#content section").html(html);
}
});