简体   繁体   English

Emberjs:使用父母和孩子的数据创建复杂的操作

[英]Emberjs: Create a complex action with data from parent and child

So I have this ember app I am building and everything has gone smoothly until now. 所以我有一个正在开发的余烬应用程序,到目前为止一切都进行得很顺利。 I am in a bit of a jam and can't quite understand what I have to do, but I know what I want to do. 我陷入了困境,无法完全理解我必须做什么,但是我知道我想做什么。 The place where I am stuck is this: I want to display all the recordings for a user, and for each recording there will be 1 or 2 filtered versions (maybe more later). 我受困的地方是:我想为用户显示所有记录,并且每个记录将有1个或2个过滤版本(可能以后会有)。 In the table that displays all a users recordings, I want to render a link for each version. 在显示所有用户录制内容的表中,我想为每个版本呈现一个链接。 Currently we form those links in the API by prepending something to the public id for the recording. 目前,我们通过在公共ID之前添加一些用于录制的API来形成这些链接。

So here is some pseudocode to help illustrate what I am asking about: 因此,这里有一些伪代码可以帮助说明我在问什么:

//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>

If it isn't obvious, in the table cell for "Filters" I want to display names like "unfiltered" and "enhanced". 如果不是很明显,我想在“过滤器”的表格单元格中显示“未过滤”和“增强”之类的名称。 Each name should link to the appropriate version of that recording which for now will just need to open in a new tab (I know, yuck). 每个名称都应链接到该记录的相应版本,该版本目前仅需要在新选项卡中打开(我知道,很抱歉)。

So, after struggling with this for a while, I found I couldn't figure out how to get the parent recording id and the child linkPrefix to be rendered into a view; 因此,经过一段时间的努力,我发现我无法弄清楚如何将父记录ID和子linkPrefix呈现到视图中。 the big problem for me was that I couldn't get the view to "know about" both the parent recording and the child filter. 对我来说,最大的问题是我无法通过视图了解父记录和子过滤器。 Filter.name was a piece of cake, but I fear that this is not the "ember way." Filter.name简直是小菜一碟,但我担心这不是“灰烬之路”。 My gut tells me there is a way to do this part without even using an each loop. 我的直觉告诉我,有一种方法可以做到这一点,甚至不需要使用each循环。 Thoughts? 有什么想法吗?

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. 首先添加一个belongsTo关系,以便您可以进入记录,然后添加一个计算属性,该属性返回给定记录和linkPrefix的过滤器URL。

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}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM