0

所以我有我正在构建的这个 ember 应用程序,到目前为止一切都很顺利。我有点困惑,不太明白我必须做什么,但我知道我想做什么。我被卡住的地方是:我想为一个用户显示所有的录音,每个录音会有 1 或 2 个过滤版本(以后可能会更多)。在显示所有用户记录的表中,我想为每个版本呈现一个链接。目前,我们通过在公共 id 前面添加一些内容来在 API 中形成这些链接以进行记录。

所以这里有一些伪代码来帮助说明我在问什么:

//models
App.User = DS.model.extend({
  first_name: DS.attr('string'),
  last_name: DS.attr('string')
  recordings: DS.hasMany(App.Recording)
});

App.Recording = DS.model.extend({
  name: DS.attr('string'),
  filter: DS.hasMany(App.Filter)
});

App.Filter = DS.model.extend({
  name: DS.attr('string'),
  linkPrefix: DS.attr('string')
});

//router
App.Router.map( function(){
  this.resource('user', {path: '/:user_id'}, function(){});
});

//User template

<script type="text/x-handlebars" data-template-name="user">
    <table class="table">
      <thead>
        <tr>
          <th>Recording Name</th>
          <th>Filters</th>
        </tr>
      </thead>
      <tbody>
        {{#each recording in recordings}}
        <tr>
          <td>{{name}}</td>
          <td>
            {{#each filter in recording}}
              //pseudocode here: {{#view should combine linkPrefix + recording_id contentBinding="recording" (maybe??)}}{{filter.name}}{{/view}}
              //also need this link to handle the click and set it to "read" once the link has been clicked
            {{/each}}
          </td>
        </tr>
        {{/each}}
      </tbody>
    </table>
  </div>
</script>

如果不明显,在“过滤器”的表格单元格中,我想显示“未过滤”和“增强”等名称。每个名称都应该链接到该记录的适当版本,现在只需要在新选项卡中打开它(我知道,呸)。

因此,在为此苦苦挣扎了一段时间后,我发现我无法弄清楚如何将父记录 id 和子链接前缀渲染到视图中;对我来说最大的问题是我无法让视图“了解”父记录和子过滤器。Filter.name 是小菜一碟,但我担心这不是“灰烬之道”。我的直觉告诉我有一种方法可以在不使用每个循环的情况下完成这部分。想法?

4

1 回答 1

1

Seems like what you need here is to add a few more properties to filter. First add a belongsTo relationship so that you can get to the recording, then a computed property that returns the filter url given the recording and linkPrefix.

App.Filter = DS.model.extend({
  name: DS.attr('string'),
  linkPrefix: DS.attr('string'),
  recording: DS.belongsTo(App.Recording),
  link: function() {
    return this.get('linkPrefix') + this.get('recording.id')
  }.property('linkPrefix', 'recording')
});

Now your handlebars template is easy:

        {{#each filter in recording}}
          <a {{action markFilterAsRead filter}} {{bindAttr href="filter.link"}}>{{filter.name}}</a>
        {{/each}}
于 2013-08-15T04:16:54.537 回答