如何在创建子ember js时获取父ID

时间:2013-04-25 05:36:31

标签: ruby-on-rails ember.js ember-data

我想在子记录中设置parent_id。

我的模型是Recipe(id,title)和Ingredients(id,about,recipe_id)。

我有一个表格,我用很多成分创造食谱。一切正常,但我不知道如何在创建父记录后在子记录中设置parent_id。

示例:

我们创建食谱(id = 1,title ='Title01')。所以我们所有的成分都有字段recipe_id 1(id = 2,约='这是成分',recipe_id = 1)

recipe.js.coffee

App.Recipe = DS.Model.extend
  title: DS.attr('string')
  ingredients: DS.hasMany('App.Ingredient')

ingredient.js.coffee

App.Ingredient = DS.Model.extend
  about: DS.attr('string')
  recipe: DS.belongsTo('App.Recipe')

new.emblem(这里我创建了父母食谱和儿童成分)

h1 Create recipe

form
  div
    label{bindAttr for='title.elementId'} Title
    Ember.TextField valueBinding='title' name='title' viewName='title'

  div
    label{bindAttr for='about.elementId'} About
    Ember.TextArea valueBinding='about' name='about' viewName='about'

  #ingredients
    h3 Ingredients
    each ingredient in content.ingredients itemViewClass='App.ItemView'
      h3= this.view.rebasedIndex
      .control-group
        .controls
          Ember.TextField valueBinding='ingredient.about'
          a{ action removeIngredient ingredient target='controller' } href='#' Remove
    div
      a{ action addIngredient target='controller' } href='#' Add Ingredient

  button.btn.btn-success{ action save this } Create
  button.btn.btn-inverse{ action cancel this } Cancel

1 个答案:

答案 0 :(得分:0)

我不知道咖啡脚本,但你能为每个成分视图创建一个parentIdBinding吗?

编辑提供了一个例子:

{{#each recipe in recipes}}
    {{#each ingredient in ingredients}}
        {{view ingredientView parentBinding="recipe"}}
    {{/each}}
{{/each}}
相关问题