首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在组件上下文中执行函数

无法在组件上下文中执行函数
EN

Stack Overflow用户
提问于 2014-08-12 13:30:50
回答 1查看 76关注 0票数 0

我需要执行应用程序控制器中的bubbleData函数

以下方法不起作用。

代码语言:javascript
复制
this.get("data").call();
TypeError: undefined is not a function
this.get("data")();
TypeError: undefined is not a function
this.get("data")()
TypeError: undefined is not a function

控制台输出:通过在组件绘制函数中键入this.get('data')

代码语言:javascript
复制
function (){
    var self = this;    
    debugger;
    return {
      name: 'tools',
      children: this.get("data").map(function(t){
        return{
          name: t.name,
          size: 300
        }
      })
    };

  } 

视图

代码语言:javascript
复制
{{bubble-chart width=560 height=350 data=bubbleData}}      

控制器

代码语言:javascript
复制
App.ApplicationController = Ember.ArrayController.extend({
  bubbleData: function(){
    var self = this;    
    debugger;
    return {
      name: 'tools',
      children: this.get("data").map(function(t){
        return{
          name: t.name,
          size: 300
        }
      })
    };
  },....

组件

代码语言:javascript
复制
App.BubbleChartComponent = Em.Component.extend({
  draw: function(){
    debugger;
  }.observes('data').on('didInsertElement')
}
EN

回答 1

Stack Overflow用户

发布于 2014-08-12 17:41:47

该组件可以使用sendAction函数(http://emberjs.com/guides/components/sending-actions-from-components-to-your-application/)发送操作。

因此,正确的方法应该是在hbs

代码语言:javascript
复制
data="bubbleData" 

和在组件的js

代码语言:javascript
复制
this.sendAction("data");

示例

http://emberjs.jsbin.com/luwixanemunu/1/edit

但是,如果需要传递函数本身,则必须注意调用该函数的上下文。这就是this.get("data")();无法工作的原因。在示例中,test2演示了调用它的正确方式,

代码语言:javascript
复制
var theController = this.get("parentView.controller");
      this.get("data2").apply(theController);

该示例的代码

hbs

代码语言:javascript
复制
<script type="text/x-handlebars">
    <h2>Welcome to Ember.js</h2>


    {{bubble-chart width=560 height=350 data="bubbleData2" data2=bubbleData test=testProp}}   


  </script>

  <script type="text/x-handlebars" id="components/bubble-chart">
  the bubble component <br/>
  {{test}}
  <br/>
  <button {{action "test1"}}>test1</button>
  <button {{action "test2"}}>test2</button>
  </script>

js

代码语言:javascript
复制
App.ApplicationController = Ember.ArrayController.extend({
  testProp:"test",
  bubbleData: function(){
    var self = this;    
    debugger;
    return {
      name: 'tools',
      children: this.get("data").map(function(t){
        return{
          name: t.name,
          size: 300
        };
      })
    };
  },
  actions:{
    bubbleData2:function(){alert("bubbleData in actions");}
  }
});



App.BubbleChartComponent = Em.Component.extend({
  draw: function(){
//     debugger;
  }.observes('data').on('didInsertElement'),
  actions:{
    test1:function(){
      this.sendAction("data");
    },
    test2:function(){
      var theController = this.get("parentView.controller");
      this.get("data2").apply(theController);
    }
  }

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25256793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档