2013-04-25 64 views
0

我想在子记录中设置parent_id。如何在创建子烬时获取父ID js

我的模型是食谱(id,title)和成分(id,about,recipe_id)。

我有一个窗体,我创建了许多配料的食谱。所有运作良好,但我不知道如何在创建父记录后在子记录中设置parent_id。

实施例:

我们创建配方(ID = 1,标题= 'Title01')。所以我们所有的成分将与1场recipe_id(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 

回答

0

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

编辑提供了一个例子:

{{#each recipe in recipes}} 
    {{#each ingredient in ingredients}} 
     {{view ingredientView parentBinding="recipe"}} 
    {{/each}} 
{{/each}} 
+0

你可以给我一个例子,如何做到这一点?在js或咖啡上它并不重要。 – EJIqpEP9 2013-04-25 18:07:26