2012-01-25 194 views
3

我想使用Ember.select创建一个下拉列表,我的问题是当它第一次呈现,我想设置一个默认值。一旦我改变了选择,它将不会检查默认值,直到刷新页面。设置选择默认值与Ember.Select

下面是代码:

 ET.selectedAppYearController = Ember.Object.create({ 
      appYear: null, 
      isChanged: function() { 
       if (this.appYear != null) { 
        LoadETByAppYearETTypeID(this.appYear.text, ET.selectedEmailTypesController.emailType.email_template_type_id); 
       } 
      } .observes('appYear'), 
      setDefault: function() { 
       if (this.appYear.text == 2012) { 
        this.set('selection',2012); 
       } 
      } .observes('appYear') 

     }); 

的观点:

{{view Ember.Select 
     contentBinding = "ET.appYearController.content" 
     selectionBinding="ET.selectedAppYearController.isDefault" 
     optionLabelPath="content.text" 
     optionValuePath="content.value" 
    }} 

我想我需要设置selectionBinding的东西......但什么样的价值,我应该结合? 1.下拉列表值是JSON类型。

+0

我改变selectionBinding =“2012”下拉列表中产生,但选择为空。它没有绑定到值...或者我应该使用这个selectionBinding的其他值? – Princa

回答

1

从代码示例很难说清楚。你可以做的一件事就是提取你所有的域特定需求,并设置一个活的jsfiddle示例,试图以更通用的方式完成你正在尝试做的事情。 http://jsfiddle.net/你可以在这里看到在其他emberjs帖子中使用jsfiddle的例子。

但我说过的一件事是您的selectionBinding似乎是错误的。这应该绑定到您尝试设置的值,而不是默认值。如果你喜欢,你可以在控制器中设置一个默认值(只要将它分配给由selectionBinding绑定的值)。所以我认为你的selectionBinding应该是“ET.selectedAppYearController.appYear”,如果我正确理解你的例子。

4

我打算为此做一个jsfiddle,但现在看起来似乎没有了。如果它稍后恢复,将会编辑一个,因为它们通常会让事情变得更加清晰。

如果视图设置为

{{view Ember.Select 
    contentBinding = "ET.appYearController" 
    selectionBinding="ET.selectedAppYearController.appYear" 
    optionLabelPath="content.text" 
    optionValuePath="content.value" 
}} 

,然后成立了selectedAppYearController的东西,如:

ET.selectedAppYearController = Ember.Object.create({ 
    appYear: ET.appYearController.get('lastObject') 
} 

,那么你应该在你的appYearController的最后一个元素设置为默认值,而当你改变选择ET.selectedAppYearController.appYear会反映这一点。

很明显,如果你想要的东西不是appYearController的最后一个元素,那么你可以将appYear的值设置为任何你想要的值。

+0

1. ET.selectedAppYearController.appYear应该是ET.appYearController的一个对象,所以我不能只为它分配一个值。 2.我尝试了你说的方式,将最后一个对象分配给appYear,但它不起作用,它仍然会选择第一个对象 3.我提出这个问题: appYear:{text:2012,value:2012} 下拉列表选择空白作为默认,但ET.selectedAppYearController.appYear.text显示正确的值,想法?! – Princa

+0

http://jsfiddle.net/Princa/4TZ7h/14/这里是我创建的一个,我把问号放在上面。 :D – Princa

2

我对这次聚会很迟,但在最新版本的ember 0.9.7.1中有一个“提示”属性,你可以在你的Ember.Select中设置。它看起来像,

{{view Ember.Select 
    prompt="This is selected by default"}} 

希望帮助某人。

+0

也可能值得注意的是,如果观察到该值,则附加值为'null'。这可能有助于知道何时将具有promt值的select添加到DOM。 –

0

不知道是否有人仍然需要,但我做的方式是像

{{view Ember.Select content=templates optionValuePath="content.id" optionLabelPath="content.name" value=selectedTemplateId class="form-control"}} 

的东西结合到值,则在控制器实现selectedTemplateId的计算性能,工作既作为setter和getter:

selectedTemplateId: ((key, value)-> 
    # setter code: 
    if arguments.length > 1 
     @set('selTemplateId', value) 
     return value 

    # getter code 
    else 
     if @get('selTemplateId')? 
      return @get('selTemplateId') 
     else 
      return @get('templates')?.objectAt(0).id 
).property() 

不好意思用CoffeeScript代替js。JS版本在:http://goo.gl/5FZHFh

文档的对于计算性能的位:http://emberjs.com/api/classes/Ember.ComputedProperty.html