2013-08-07 24 views
0

我有以下下拉菜單:更新一個屬性是不是在DOM可見

{{view SettingsApp.Select2SelectView 
    id="country-id" 
    contentBinding=currentCountries 
    optionValuePath="content.code" 
    optionLabelPath="content.withFlag" 
    selectionBinding=selectedCountry 
    prompt="Select a country ..."}} 

... 

{{view SettingsApp.Select2SelectView 
    id="city-id" 
    contentBinding=currentCities 
    selectionBinding=selectedCity 
    prompt="Select a city ..."}} 

結合的性質列於控制器定義:

SettingsApp.ServicesEditController = SettingsApp.BaseEditController.extend(SettingsApp.ServicesMixin, { 
    needs   : ['servicesIndex'], 
    selectedCountry : null, 
    selectedCity  : null, 
    currentCountries : null, 
    currentCities : null, 
    init : function() { 
     this._super(); 
    }, 
    setupController : function (entry) { 
     this._super(entry); 
     var locator = SettingsApp.locators.getLocator(this.get('content.properties.locator')); 
     var countryCode = locator.get('country'), city = locator.get('city'); 
     this.set('currentCountries', SettingsApp.countries.getCountries()); 
     this.set('currentCities', SettingsApp.locators.getCities(countryCode)); 
     this.set('selectedCountry', SettingsApp.countries.getCountry(countryCode)); 
     this.set('selectedCity',  city); 
     // Add observers now, once everything is setup 
     this.addObserver('selectedCountry', this.selectedCountryChanged); 
    }, 
    selectedCountryChanged: function() { 
     var countryCode = this.get('selectedCountry.code'); 
     var currentCities = SettingsApp.locators.getCities(countryCode); 
     var selectedCity = currentCities[0]; 
     this.set('currentCities', currentCities); 
     this.set('selectedCity', selectedCity); 
    }, 
    ... 
}); 

初始設置工作正常,但即使調用觀察者(selectedCountryChanged)並且this.set('selectedCity', selectedCity);按預期工作(如控制檯日誌記錄中所示),更改國家/地區選擇並不會更新下拉列表中的城市選擇。觀察者運行後,currentCities設置正確,但活動(選定)值不正確(保持不變)。

是否有任何已知的綁定屬性的編程更新問題,在這種情況下是selectionBinding

Select2SelectView是:

SettingsApp.Select2SelectView = Ember.Select.extend({ 
    prompt: 'Please select...', 
    classNames: ['input-xlarge'], 

    didInsertElement: function() { 
     Ember.run.scheduleOnce('afterRender', this, 'processChildElements'); 
    }, 

    processChildElements: function() { 
     this.$().select2({ 
      // do here any configuration of the 
      // select2 component 
      escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results 
     }); 
    }, 

    willDestroyElement: function() { 
     this.$().select2('destroy'); 
    } 

}); 

回答

1

檢查是否選擇的城市是越來越除去選擇2顯示(正常選擇替換)。如果是這樣,selectbinding必須傳播到select2。

+0

事實上,情況就是這樣!我怎樣才能「將selectionBinding'傳播到'Select2SelectView'」? – dangonfast

+0

因爲我只是用一些簡單的方法來擴展'Ember.Select',所以我應該從'Ember.Select'繼承正確的行爲,關於'selection'屬性。我想知道我的'Select2SelectView'中有什麼會誤入歧途... – dangonfast

+0

觀察選擇綁定值。每當它發生變化時,使用數據方法在select2中更新它。請參閱關於如何使用數據方法的select2文檔。 – Dinesh