最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - EmberJS: How to render a template on select change - Stack Overflow

programmeradmin1浏览0评论

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()?)...

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()?)...

Share Improve this question asked Sep 6, 2013 at 0:24 DannyDanny 1,3314 gold badges13 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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

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

发布评论

评论列表(0)

  1. 暂无评论