2013-09-06 4 views
6

Я новичок в ember и пытаюсь выяснить, как визуализировать шаблон при изменении элемента управления.EmberJS: Как визуализировать шаблон при выборе смены

КОД:

App.LocationTypeController = Ember.ArrayController.extend({ 

    selectedLocationType: null, 

    locationTypeChanged: function() { 
     //Render template 
    }.observes('selectedLocationType') 
}); 

{{view Ember.Select 
    contentBinding="model" 
    selectionBinding="selectedLocationType" 
    optionValuePath="content.id" 
    optionLabelPath="content.name"}} 

Когда locationType изменяет функцию locationTypeChanged обжигают в контроллере. Но как я могу отдать какой-то контент в дом? (This.render (?)) ...

ответ

7

Да, вы должны использовать только this.render(), но здесь ключ into.

App.LocationTypeController = Ember.ArrayController.extend({ 

selectedLocationType: null, 

locationTypeChanged: function() { 
    var selectedLocationType = this.get('selectedLocationType'); 
    this.send('changeTemplate',selectedLocationType); 
}.observes('selectedLocationType') 
}); 

Есть действия в вашем маршруте, как

changeTemplate: function(selection) { 
      this.render('template'+selection.id,{into:'locationType'}); 
} 

и имеют {{outlet}} в шаблоне ваших locationType «S.

{{view Ember.Select 
     contentBinding="model" 
     selectionBinding="selectedLocationType" 
     optionValuePath="content.id" 
     optionLabelPath="content.name"}} 

{{outlet}} 

Образец JSBin для вашего требования

4

Если вам нужно показать только frament, когда существует что-то выбрано, вы можете использовать помощник if Рулей:

В шаблоне

... 

{{#if selectedLocationType}} 
    Any content here will be visible when selectedLocationType has some value 
{{/if}} 

... 

{{view Ember.Select 
    contentBinding="model" 
    selectionBinding="selectedLocationType" 
    optionValuePath="content.id" 
    optionLabelPath="content.name"}} 

Надеюсь, это поможет

Смежные вопросы