灰烬路线 - 动态线段 - 布线模型在一起

时间:2013-07-30 16:14:56

标签: ember.js ember-data

我想使用ember路线和模型来构建一些“雄心勃勃”的网络应用程序。 这就是我想要做的。我有一本书和作者模型。在显示作者资源列表的同时,如果用户单击作者,则通过嵌套路径显示该作者的所有书籍。因此,我想将作者ID(或名称)视为传递给图书模型的动态细分。但似乎存在一个限制,即dynamic segments必须是当前模型的primaryKey,不能使用其他模型属性或模型。

我是否需要编写自定义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'

预订模型

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'

1 个答案:

答案 0 :(得分:0)

前段时间我回答了一个有点类似的question(在guides中也有描述)如何将模型与belongsTo之类的关联联系起来(当模型记录属于另一个模型)或hasMany(当模型记录具有与其关联的另一个模型的许多记录时)以及如何在同一路径中使用多个模型。 / p>

通过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模型中定义的属性,例如dobname ..等等

     _____________________              ______________________
    |       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模型的实例,这些实例可通过模型声明中的关联获得。