2013-07-30 29 views
0

我想用餘燼路線和模型來構建一些「雄心勃勃」的網絡應用程序。 這就是我想要做的。我有一本書和作者模型。在顯示作者資源列表的同時,如果用戶點擊作者,則通過嵌套路線顯示該作者的所有書籍。所以我想將作者id(或名稱)作爲動態片段傳遞給書籍模型。但似乎有一個限制,dynamic segments必須是當前模型的primaryKey,不能使用其他模型屬性或模型。Ember路線 - 動態細分 - 將模型連接在一起

我是否需要編寫自定義的de/serializer?如果是這樣,一個適用於這種情況下的例子小提琴將不勝感激。

下面是一些示例代碼,描述了我想要做什麼,並在評論中出現了具體的問題。

Authors.hbs模板

 <div class="span3"> 
      <h4>Books</h4> 
      <ul class="unstyled"> 
       {{#each model}} 
        {{#linkTo 'authors.books' this}}<li> 
         {{name}} <small>{{date dob}}</small> <small> {{origin}}</small> 
        </li>{{/linkTo}} 
       {{/each}} 
      </ul> 
     </div> 
     <div class="span8"> 
      {{outlet}} 
     </div> 

initialize.coffee

@resource 'books', -> 
    @resource 'book', {path: ':cube_id'} 
@resource 'authors', -> 
    @resource 'AUTHOR', {path: ':AUTHOR_id'} 
    # QUESTION How to make the next line retrieve books-by-author from the book model? 
    @route 'books', {path: '/books/:author_id'} 

AuthorRoute

module.exports = App.AuthorsRoute = Em.Route.extend 
    model: -> 
     App.Author.find() 

BookRoute

module.exports = App.BooksRoute = Em.Route.extend 
    model: -> 
     App.Book.find() 
    # QUESTION How should the model be extended to allow for lookup by attribute? 
    # model: (params) -> 
    # App.Book.find { author: params.book_id } 

作者模型

module.exports = App.Author = DS.Model.extend 
    name: DS.attr 'string' 
    origin: DS.attr 'string' 
    dob: DS.attr 'date' 

Book模型

module.exports = App.Book = DS.Model.extend 
    name: DS.attr 'string' 
    author: DS.attr # QUESTION How to wire together models here? 
    #author: DS.belongsTo 'App.Author' 
    publisher: DS.attr 'string' 
    published: DS.attr 'date' 

回答

0

有時前,我已經回答了幾分相似question(和它也在guides描述)如何與協會,如belongsTo鏈接模型(當一個模型記錄屬於另一個模型)或hasMany(當一個模型記錄有很多與其相關的另一個模型的記錄)以及如何使用多個模型在相同的路線。

使用Ember-Data提供的關聯,您可以在模型中創建屬性,這些屬性是(a)相關記錄的集合或(b)相關記錄的父親的單個記錄。然後在視圖圖層中,只需按名稱引用這些屬性(集合或其他)(請參閱下面的建議模板)。

至於你的特定的應用程序,你如下建議可以寫你的模型:

module.exports = App.Author = DS.Model.extend 
    name: DS.attr 'string' 
    origin: DS.attr 'string' 
    dob: DS.attr 'date' 
    books: DS.hasMany 'App.Book' 

module.exports = App.Book = DS.Model.extend 
    name: DS.attr 'string' 
    author: DS.belongsTo 'App.Author' 
    publisher: DS.attr 'string' 
    published: DS.attr 'date' 

注意,現在這兩個模型彼此瞭解。這個模型聲明將給你一個類似於下面的表示的數據結構。因此,每次有Book的實例時,都可以使用其屬性author訪問Author模型中定義的屬性,例如dob,name ..等

 
    _____________________    ______________________ 
    |  Author  |   |   Book  |   
    |---------------------|   |----------------------| 
(PK)| + id: int   |◄-\ |--►►| + id: int   |(PK) 
    | + name: string  | \ | | + name: string  | 
    | + origin: string | \--Ԑ----| + author: App.Author |(FK) 
    | + dob: date   | / | + publisher: string | 
(FK)| + books: App.Book[] |-----/  | + published: date | 
    |_____________________|   |______________________| 

依託協會,你就可以參考Author.books在您的視圖層(迭代的集合,是你的控制器或模型的屬性時,在您的視圖中使用content.books),以獲得集合該作者的書籍並將其與{{#each}}塊一起使用。作爲一個例子,看看這fiddle,特別是在模板標籤/標籤其中我迭代通過與給定標籤相關聯的帖子列表。儘管樣本是用於「博客」的,但是同樣的概念可以用於您當前使用的任何實體類型。

所以你的情況,甚至在「authors.author」路線,模板可能看起來像以下:

<h3>Books written by <em>{{name}}</em></h3> 
<ul> 
    {{#each content.books}} 
     <li> 
      {{#linkTo books.book this}} 
       <i class="icon-book"></i> 
       <!-- consider a helper to display the year or format the date 
        instead of "publisehd" which will display an ugly date --> 
       {{this.name}} (this.published) 
      {{/linkTo}} 
     </li> 
    {{else}} 
    <li> 
     <div class="alert"> 
      <a class="close" data-dismiss="alert" href="#">&times;</a> 
      This author hasn't written any books that we know of.. seriously... 
     </div> 
    </li> 
    {{/each}} 
</ul> 

注意,{{#each}}助手是通過收集迭代,也就是實例Book模型可通過模型聲明中的關聯獲得。

+0

感謝您的徹底解答。我的問題確實在嵌入式路線的上下文中具體詢問了動態細分市場......您認爲您可以添加一些關於此的評論嗎? –

+0

@ ted.strauss由於某種原因,我沒有看到這個評論。我今天晚些時候會看看它 – MilkyWayJoe