Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmberJS: How to render a template on select change

I'm new to ember and am trying to figure out how to render a template when a select control changes.

CODE:

    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"}}

When the locationType changes the locationTypeChanged function is fired in the controller. But how do I render some content into the dom from there? (this.render()?)...

like image 205
Danny Avatar asked Sep 06 '13 00:09

Danny


2 Answers

Yes you have to use this.render() only, but the key here is into option inside it.

App.LocationTypeController = Ember.ArrayController.extend({

 selectedLocationType: null,

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

Have the action in your route as

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

and have an {{outlet}} in your locationType's template.

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

{{outlet}}

Sample JSBin for your requirement

like image 123
Hyder Avatar answered Oct 16 '22 11:10

Hyder


If you need to show only a frament, when exist something selected, you can use the if handlebars helper:

In your template

...

{{#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"}}

I hope it helps

like image 39
Marcio Junior Avatar answered Oct 16 '22 12:10

Marcio Junior