2013-03-16 43 views
1

我在玩一个玩具应用程序来学习余烬。我有属于风格的食谱。样式只包含一个名称属性(以及隐含的ID)。我无法在新模板中设置选择字段来选择配方的哪种样式。使用emberjs选择视图填充选择框

这是我目前有我的路线,控制器和模板:

路线:

App.RecipesNewRoute = Ember.Route.extend({ 
    model: function() { 
    return App.Recipe.createRecord({ 
     title: '', 
     description: '', 
     instructions: '' 
    }); 
    }, 
    setupController: function(controller, model) { 
    controller.set('content', model); 
    } 
}); 

控制器:

App.RecipesController = Ember.ArrayController.extend({}); 

App.RecipesNewController = Ember.ObjectController.extend({ 
    create: function() { 
    this.transitionToRoute('recipes.show', this.content); 
    }, 

    cancel: function() { 
    this.content.deleteRecord(); 
    this.transitionToRoute('recipes.index'); 
    }, 

    buttonTitle: 'Add Beer Recipe' 
}); 

模板:

食谱/新形式部分:

<script type="text/x-handlebars" data-template-name="recipes/new"> 
    {{ partial 'recipes/form' }} 
</script> 

<script type="text/x-handlebars" data-template-name="recipes/_form"> 
    {{ title }} 
    <form> 
    <fieldset> 
     <div> 
     <label {{bindAttr for="titleField.elementId"}}>Title</label> 
     {{view Ember.TextField valueBinding='title' name='title' viewName='titleField'}} 
     </div> 
     <div> 
     <label>Style</label> 
     {{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='App.Style.find()'}} 
     </div> 
     <div> 
     <label {{bindAttr for='descriptionField.elementId'}}>Description</label> 
     {{view Ember.TextArea valueBinding='description' name='description' viewName='descriptionField'}} 
     </div> 
     <div> 
     <label {{bindAttr for='instructionsField.elementId'}}>Instructions</label> 
     {{view Ember.TextArea valueBinding='instructions' name='instructions' viewName='instructionsField'}} 
     </div> 
     <a href='#' {{action create target='controller'}}>{{buttonTitle}}</a> 
    </fieldset> 
    </form> 

    <a href='#' {{action cancel target='controller'}}>Cancel</a> 
</script> 

主要外卖福利:{{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='App.Style.find()'}}

我也试过在控制器中建立一个风格变量: 在RecipesNewController:styles: App.Style.find()和以及在路由的setupController {{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='styles' optionValuePath='styles.id' optionLabelPath='styles.name'}}

视图:controller.set('styles', App.Style.find())(使用与在控制器中设置相同的视图代码)。

任何帮助都会很棒,我相信这很简单。

感谢

更新

所以,我想我已经几乎得到了它。

在我的RecipesNewRoute的setupController中我有controller.set('styles', App.Style.find());。然后在我看来我有{{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='controller.styles' contentValuePath='content.id' contentLabelPath='content.name'}}。现在我的问题是我有选择框正在填充,除了标签和值设置为<App.Style:ember407:100>而不是id和名称。

回答

6

Ember Select使用optionValuePath和optionLabelPath,看起来您使用的是contentValuePath和contentLabelPath。

这里有一个例子的jsfiddle:http://jsfiddle.net/nrionfx/BJwLR/2/

从烬文档:http://emberjs.com/api/classes/Ember.Select.html

{{view Ember.Select 
     contentBinding="App.programmers" 
     optionValuePath="content.id" 
     optionLabelPath="content.firstName"}} 
+1

啊,谢谢,看起来像我是在正确的道路上刚刚我的命名约定搞砸了。 – spullen 2013-03-16 14:44:03