我有一個集合,它看起來像這樣Backbone.js的路由取決於型號值
[
{
"year": 1868,
....
]
},
{
"year": 1872,
....
},
{
...
}
]
有沒有一種方法來設置是路由與'/year/:year': 'year'
或'/(:year)': 'year'
?
我已經嘗試在主應用視圖中製作查找表,該查看錶將年份索引傳遞給模型視圖。我曾嘗試使用_.map,_.each,_.pluck和_.where,但我想我必須做錯了什麼。
Here是它的外觀的非骨幹視圖。所以,導航到/(:年)將直接進入那年,相當於一個模型索引
編輯:澄清,基本上我希望用戶能夠去/year/:year
,但:year
值對應於一個特定的模型(見上文)。在這種情況下去/year/1868
,將呈現上述集合中的第一個模型。
編輯#2:這是我的應用程序的樣子。
這是路由器 VAR路由器= Backbone.Router.extend({ 路線:{ '': '根', '年(/:年)': '年' },
root: function() {
new App();
},
year: function(year) {
new App({
year: year
});
}
});
因爲它們非必要的調用該文件
define(['backbone', 'assets/js/collections/elections.js', 'assets/js/views/election.js', 'jqueryui'], function(Backbone, Elections, ElectionView, simpleSlider) {
var AppView = Backbone.View.extend({
current_election_index: 0,
active_btn_class: 'dark_blue_bg',
wiki_base: 'http://en.wikipedia.org/wiki/United_States_presidential_election,_',
started: 0,
el: 'body',
playback: {
id: '',
duration: 1750,
status: false
},
initialize: function() {
elections = new Elections();
_.bindAll(this, 'render');
this.listenTo(elections, 'reset', this.render);
elections.fetch();
this.remove_loader();
},
render: function() {
if (this.started === 0) {
this.election_year();
}
var view = new ElectionView({
model: elections.at(this.current_election_index),
election_index: this.current_election_index
});
this._make_slider();
this.update_ui();
return this;
},
我拿出一些方法。
你可以嘗試澄清你的問題?從你的解釋中解析有點困難。 – jevakallio 2013-02-27 14:34:51