灰烬 - 渲染问题 - 多对多

时间:2014-01-07 17:56:48

标签: ember.js ember-data handlebars.js

在那里,谢谢花时间帮助我。

我有以下型号:

App.Project = DS.Model.extend({
    name: DS.attr('string'),
    projectTeams: DS.hasMany('projectTeam'),
});

App.ProjectTeam = DS.Model.extend({
    sdeEffort: DS.attr('number'),
    team: DS.belongsTo('team'),
    project: DS.belongsTo('project'),
});

App.Team = DS.Model.extend({
    projectTeams: DS.hasMany('projectTeam'),
    name: DS.attr('string')
});

以及以下html:

<table class="table table-bordered" border="1">
    <tr>
      <th>
        Name
      </th>
      {{#each team in teams}}
        <th>
          team.name
        </th>
      {{/each}}
    </tr>
    {{#each project in model}}
    <tr>
      <td>
        {{project.name}}
      </td>
      <td>
        // WHAT SHOULD COME HERE??

类似于foreach团队,foreach project.project_teams,如果team.id == project_team.team_id打印project_team.sdeEffort否则.. ???如果我只创建一个空白列,那么每个团队都有numberOfProjectTeamsForProject-1列,这不是我想要的......我想要计算多少

      </td>
    </tr>
    {{/each}}
  </table>

所以基本上我可以看到三个模型。项目有很多项目团队。团队有很多项目团队。 ProjectTeam同属于Project和Team。

我想做的是以下内容:

我打印出一个带有项目名称的表格,作为一个表格标题我正在为每个可能的团队渲染columsn。现在,对于每个项目,我只想为那些属于我正在使用ProjectTeam(sdeEffort)中找到的值进行迭代的项目的团队填充团队列。如果有问题的团队不是项目的一部分,我想渲染一个空列。我已经尝试实现我自己的把手辅助方法和所有这些,但没有一个会起作用..

这在Ember / Handlebars中是否可行?!

Desired out(table):

Project name  T1   T2   T3
__________________________
Project 1 |    | 1  |    |
Project 2 | 2  | 10 |    |
Project 3 |    |    |    |
Project 4 |    |    |  7 |

团队1仅参与项目1,团队2参与项目2和3,团队3参与项目4.没有团队参与项目3

1 个答案:

答案 0 :(得分:0)

您可以在控制器上创建一个teamsProjectsTable属性,该属性派生自您的模型,并返回一个常规JS对象,如下所示:

{
  teamNames: ["T1", "T2", "T3"],
  projects: [
    {name: "Project 1", cells: [1, null, 1, null] },
    {name: "Project 1", cells: [2, 2, 10, null] },
    {name: "Project 1", cells: [3, null, null, null] },
    {name: "Project 1", cells: [4, null, null, 7] }
  ]
}

在您的属性中使用Javascript构造此对象是微不足道的,然后视图也变得微不足道。

例如,它看起来像这样:

App.ProjectsController = Ember.Controller.extend({
  function teamsProjectsTable() {
     var result = {teamNames: [], projects: []};
     var teams = this.get("teams");
     var projects = this.get("projects");

     // do lots of stuff here with teams and projects to make result look like the thing above

     return result;
  }.property(); 
});

在视图中:

<table class="table table-bordered" border="1">
<tr>
  <th>
    Name
  </th>
  {{#each teamName in teamsProjectsTable.teamNames}}
    <th>
      teamName
    </th>
  {{/each}}
</tr>
{{#each project in teamsProjectsTable.projects}}
<tr>
  <td>
    {{project.name}}
  </td>
  {{#each teamCell in project.cells}}
     <td>{{teamCell}}</td>
  {{/each}}
<tr>
{{/each}}
</table>