从路径文件

时间:2016-02-19 15:45:24

标签: javascript ember.js components

我在index.hbs文件中有这段代码:

<button {{action 'showHotspots'}} class="btn btn-success padding">{{hotspotCategory.name}}</button>

我的routes / index.js文件中有showHotspots方法:

showHotspots: function( selection ) {
  this.toggleProperty( 'idFromButton' );
}

该方法应在我的routes / index.js中切换此部分:

{{#if idFromButton}}
  <p>Test1</p>
{{/if}}

然而,这不起作用,当我将它作为一个组件工作时,我需要在我的页面上的特定位置上的Test1(在所有生成的按钮下面)。当我在组件中执行它时,Test1会显示在按钮之间。

我没有在我的index.js文件中使用我想要的组件,所以我可以控制页面的结构。

简而言之:在我的index.hbs上,我有一个if语句,用于检查变量是否为true。我想使用在同一个index.hbs文件或组件-1.hbs中调用的方法,从我的routes / index.js文件(或从其他地方,实际上并不重要)设置此变量文件。

2 个答案:

答案 0 :(得分:1)

如果我理解了这个问题,你使用组件,你应该在你的组件的动作上使用方法“showHotspots”,如果不是,你应该对你的控制器的动作使用这个方法。
最好更详细地描述一下你的问题 编辑:如果您使用旧版本的ember js,这是一个示例

//#### controller index.js
App.IndexController = Ember.Controller.extend({
    idFromButton: false,
    actions: {
        showHotspots: function( selection ) {
            this.toggleProperty( 'idFromButton' );
        }
    }
});


//#### index.hbs  (your index controller template)
<button {{action 'showHotspots'}} class="btn btn-success padding">some text</button>
{{#if idFromButton}}
  <p>Test1</p>
{{/if}}

答案 1 :(得分:0)

(简单)解决方案是:

this.controller.toggleProperty( 'idFromButton' );