2012-01-25 191 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