私はバックボーンアプリを作成しています。すべてが機能しているように見えますが、このアプローチで機能とビューを追加し続けると、すぐにリファクタリングする必要があるかもしれません。バックエンドでParseを使用しています。つまり、オブジェクトParse
は。と同等Backbone.Model
です。モデルはモバイル向けに最適化されているため、モデルを制御できません。ここでは、3つの異なるモデルの特定のキー値のみを表示する必要があります。
「間違った」ものは何もありませんが(それは機能します)、私が「より大きな」間違いをし始めるまでは時間の問題です。
これが私のRouter
:
App.Controllers.Documents = Backbone.Router.extend({
routes:{
"story/:id" : "fullStory",
"" : "index"
},
// ...
fullStory: function(id){
var self = this;
var query = new Parse.Query(Steps);
query.equalTo("story", id)
var steps = query.collection();
steps.fetch({
success: function() {
new App.Views.Steps({ collection: steps });
var title = new Story({ objectId: id });
title.fetch({
success: function(model, resp) {
new App.Views.Title({ model: title});
var idUser = title.toJSON().idUser;
var user = new Parse.User({ objectId: idUser });
user.fetch({
success: function(){
// I just need the username key here...
new App.Views.Username({model:user,el:$("#user")});
},
error: function(){
new Error({ message: 'Could not find the user you requested.' });
}
})
},
error: function() {
new Error({ message: 'Could not find that story.' });
}
})
},
error: function() {
new Error({ message: "Error loading story." });
}
});
}
Parseでオブジェクトが作成された方法を考えると、 3つの異なるオブジェクトにアクセスする必要があるためView
、「パーツ」でレンダリングしています。FullStory
View
この関数にこれらの「サブビュー」を内部に含めてレンダリングしたいのですParse.Object
が、他のオブジェクトへの参照が必要なため、どのように実行するのかわかりません->var user = new Parse.User({ objectId: idUser });
ここidUser
で、は別のオブジェクトからのキーです。
最後に私のViews
:
App.Views.Steps = Backbone.View.extend({
tagName : "ul",
className: "steps",
initialize: function() {
this.render();
},
render: function() {
this.$el.html(_.template($("#story_steps").html())({ collection: this.collection }));
$('#steps_wrapper').html(this.el);
},
});
App.Views.Title = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
this.$el.html(_.template($("#story_title").html())({ model: this.model }));
$('#title').html(this.el);
}
});
App.Views.Username = Backbone.View.extend({
initialize: function() {
this.render();
},
template: _.template('<%= name %>'),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
}
});