2013-01-16 20 views
4

我正在用ember.js創建一個應用程序。我開始使用PRE.2,但最終使用了ember-data v11,因此升級到掌握適當的餘燼。這意味着必須改變到新的v2路由器接口(作爲一個便箋,我認爲是非常好,所以謝謝你。)Ember.js路由器v2 App.Router.map在文檔中不一致?

我有幾個問題試圖找出它是如何工作的,我「M引導深,但有一對夫婦不一致的我不能完全避開我的頭:

1)

似乎有用於配置路由映射兩種不同的約定:

在'模板'部分中,使用匹配()。到()接口

App.Router.map(function(match) { 
    match('/').to('index'); 
    match('/posts').to('posts'); 
    match('/posts/:post_id').to('post'); 
}); 

(此方法也是在湯姆·戴爾的gist使用)在「路由」部分,資源/路由接口用於:

App.Router.map(function() { 
    this.resource('posts', function() { 
    this.route('new'); 
    }); 
}); 

這指出,「資源」應該用於名詞路線,而「路線」用於動詞路線。

那麼「重定向到一個不同的URL」部分中,這個名詞/動詞約定不遵循:

App.Router.map(function(match) { 
    this.resource('topCharts', function() { 
    this.route('choose', { path: '/' }); 
    this.route('albums'); 
    this.route('songs'); 
    this.route('artists'); 
    this.route('playlists'); 
    }); 
}); 

我的第一個問題是:

展望未來,什麼是正確的大會創建路線?

我的第二個問題,從後面上,並且更貼近我的應用程序:

如何從頂層「資源」的路線鏈接到一個嵌套的「路線」的路線,並通過適當的模型?

(在'Templates'文檔的'Links'部分有一個例子,但它與match()。to()接口有關,我特別使用資源/路線接口)

這裏是我的例子:

我已經創建了一個基於流的一個簡單的網站結構,數據流由細節,一組帖子,手柄和歷史。我的路由設置像這樣:

App.Router.map(function() { 
    this.resource('streams'); 
    this.resource('stream', { path: '/stream/:stream_id' }, function(){ 
    this.route('details'); 
    this.route('posts'); 
    this.route('handles'); 
    this.route('history'); 
    }); 
}); 

我流的路線是這樣的:

App.StreamsRoute = Ember.Route.extend({ 
    model: function() { 
    return App.Stream.find(); 
    }, 
    setupController: function(controller, model) { 
    controller.set('content', model); 
    } 
}); 

和模板:

<script type="text/x-handlebars" data-template-name="streams"> 
    <ul> 
    {{#each stream in controller}} 
    <li>{{#linkTo "stream" stream}} {{stream.title}} {{/linkTo}}</li> 
    {{/each}} 
    </ul> 
</script> 

我流(單數)路線:

<script type="text/x-handlebars" data-template-name="stream"> 
    <nav> 
    {{#linkTo "stream.details" }}Details{{/linkTo}} 
    </nav> 
    {{outlet}} 
</script> 

現在,我想鏈接到我的子路徑「詳細信息」,但我不知道該怎麼在linkTo地方,讓我的模型(這是一個流)向下傳遞到這個子路線:

App.StreamDetailsRoute = Ember.Route.extend({ }); 

我的「詳細信息」模板只是顯示流對象的一些屬性。

<script type="text/x-handlebars" data-template-name="stream/details"> 
    <h2>Stream Details</h2> 
    <p>Id: {{id}}</p> 
    <p>Title: {{title}}</p> 
</script> 

我也想鏈接到帖子,歷史和處理子路線,並能夠顯示這些聚合基於流模型。我不確定如何做到這一點。我假設我需要使用setupController來獲取要顯示的項目,我只是不知道如何將流對象放到這些子路徑中。

回答

2

展望未來,創建路線的恰當慣例是什麼?

http://emberjs.com/guides/routing/defining-your-routes/

描述如何從頂層「資源」的路線鏈接到一個嵌套的「路線」的路線,並通過適當的模型資源/路由的方法呢?

指定路由的名稱作爲第一個參數,然後指定所需的任何上下文。因此,在您的示例中,當從流模板創建到「stream.details」的鏈接時,您需要指定this作爲上下文。

{{#linkTo "stream.details" this}}Details{{/linkTo}} 

http://emberjs.com/guides/templates/links/中描述的方法仍涵蓋基礎知識。

如果有疑問,我建議檢查link_helper的測試用例。例如:https://github.com/emberjs/ember.js/blob/master/packages/ember/tests/helpers/link_to_test.js#L249

+0

啊,太簡單了,謝謝! – Bueller