简体   繁体   English

EmberJS:如何在选择更改时呈现模板

[英]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. 我是ember的新手,我正试图弄清楚当select控件发生变化时如何渲染模板。

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. 当locationType更改时,将在控制器中触发locationTypeChanged函数。 But how do I render some content into the dom from there? 但是如何从那里将一些内容渲染到dom中呢? (this.render()?)... (this.render()?)...

Yes you have to use this.render() only, but the key here is into option inside it. 是的,你必须使用this.render()而已,但这里的关键是into里面的选项。

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. 并在locationType的模板中添加{{outlet}}

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

{{outlet}}

Sample JSBin for your requirement 根据您的要求提供JSBin示例

If you need to show only a frament, when exist something selected, you can use the if handlebars helper: 如果您只需要显示一个框架,当存在选定的内容时,您可以使用if把手帮助器:

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 我希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM