私は、バックボーンズ・ジャズのものを書いて、どこでもっとうまく理解できるようにしています。私はサイトを持っていて、ページコンテンツを含むコレクションをロードしています。backbone.js初心者コレクション
JSONデータは、私のルータ上の(PID、名前、タイトル、内容)に戻ってくるデフォルトは
defaultRoute: function (actions)
{
this.showInfo('food');
},
showInfo: function (id)
{
var view = new ContentView({ model: this._items.at(id) });
$(".active").removeClass("active");
$("#" + id).addClass("active");
view.render();
}
である私は、この「新ContentView({モデルにIDの代わりに0を置く場合:この._items.atは(0)})」私は、コレクション内の最初の項目を取得し、私はビューでこれを行う場合:
var ContentView = Backbone.View.extend({
el: $('#content'),
render: function()
{
this.el.empty();
$(this.el).append(this.model.attributes.content);
return this;
}
});
私は、コンテンツが完全に表示されますが、もちろん、コンテンツIではないかもしれない取得します欲しい
名前== "food"に基づいてコレクションから選択することは可能ですか?これは愚かな質問のように思えるが、私はすべての単純
確認イム不足しているものを見て、イムの上にクロールしている場合、私はID番号にコンテンツをマッピングする必要がありますする必要はいけないことはデシベル
申し訳への格納の目的に反しそれは
var NavigationRouter = Backbone.Router.extend({
_data: null,
_items: null,
_view: null,
routes: {
"p/:id": "showInfo",
"*actions": "defaultRoute"
},
initialize: function (options)
{
var _this = this;
$.ajax({
url: "page_data.php",
dataType: 'json',
data: {},
async: false,
success: function (data)
{
_this._data = data;
_this._items = new ItemCollection(data);
_this._view.render();
Backbone.history.loadUrl();
}
});
return this;
},
defaultRoute: function (actions)
{
this.showInfo('home');
},
showInfo: function (id)
{
var view = new ContentView({ model: this._items.at(id) });
$(".active").removeClass("active");
$("#l_" + id).parent().addClass("active");
view.render();
}
});
だから私は作成する必要があります。新しいvar homeModelなど各ページのために私はこのshow.initがNavigationRouterの外で定義されていないので、show infoの中でルーティングするつもりです – Ehask
アンダースコアの 'find'関数を使って新しいモデルを作成していません。 'this._items'に既に存在するモデルを見つけ、そのモデルへの参照を保存するだけです。次に、それを使って、既存のビューに渡したり、新しいビューに渡したりすることができます。 – satchmorun
ありがとう!! – Ehask