如何在Ember js中获取用户字段有很多关联

时间:2013-04-13 16:07:31

标签: ruby-on-rails ember.js

我有两种型号User和Recipe。我想在用户页面上的每个食谱中的食谱页面和用户名上获取用户字段。我不知道怎么做。在rails中我有两个在User和Recipe之间有很多关联。我添加了我从rails获得的json数组的例子。

我的json用户数组

user: {
  id: 1,
  name: "ejiqpep",
  email: "ejiqpep@gmail.com",
  recipe_ids: [
    1,
    2
  ],
  box_recipe_ids: [
    1,
    2
  ]
}

我的json配方数组

recipe: {
  id: 1,
  title: "recipe01",
  about: "about01",
  ingredients: "ingredients01",
  steps: "steps01",
  visibility_type: true,
  slug: "recipe01",
  user: {
    id: 1,
    name: "ejiqpep",
    email: "ejiqpep@gmail.com",
    recipe_ids: [
      1,
      2
    ],
    box_recipe_ids: [
      1,
      2
    ]
  }
}

user.js.coffee

App.User = DS.Model.extend
  name: DS.attr('string')
  email: DS.attr('string')
  recipes: DS.hasMany('App.Recipe')
  box_recipes: DS.hasMany('App.Recipe')

recipe.js.coffee

App.Recipe = DS.Model.extend
  user: DS.belongsTo('App.User')
  title: DS.attr('string')

user.handlebars(我想在每个食谱中获取用户名)

<h1>{{name}}</h1>
<p><b>Email:</b> {{email}}</p>

<h3>User recipes</h3>
{{#each recipe in recipes}}
  <div>
    <span>{{recipe.id}}</span>
    <span>!!!I want to show username here!!!</span>
    <span>{{#linkTo recipe recipe}} {{recipe.title}} {{/linkTo}}</span>
  </div>
{{/each}}

<h3>User box_recipes</h3>
{{#each fav in box_recipes}}
  <div>
    <span>{{fav.id}}</span>
    <span>!!!I want to show username here!!!</span>
    <span>{{#linkTo recipe fav}} {{fav.title}} {{/linkTo}}</span>
  </div>
{{/each}}

recipe.handlebars

<h1>{{title}}</h1>
<p>User {{user.name}}</p>  # I want to show username here!!!

1 个答案:

答案 0 :(得分:0)

我不确切知道它是否有效......但试一试:

更新:

{{#each recipes}}
  <div>
    <span>{{id}}</span>
    <span>{{../name}}!!!I want to show username here!!!</span>
    <span>{{#linkTo recipe this}} {{title}} {{/linkTo}}</span>
  </div>
{{/each}}