【问题标题】:Bubbling action event with custom Object from component to controller using Morris.js graph in Ember component使用 Ember 组件中的 Morris.js 图形从组件到控制器的自定义对象冒泡动作事件
【发布时间】:2014-04-21 00:06:46
【问题描述】:

我正在实现一个基本的 Ember 组件,它将呈现一个 Morris.js 线图。我需要监听 morris 上的点击事件并将其传递给操作。

App.ProgressGraphComponent = Ember.Component.extend(
{
    graph: null,

    tagName: 'div',

    renderGraph: function()
    {
        this.graph.setData(this.get('progress'));

    }.observes('progress'),

    didInsertElement: function()
    {
        var element = this.get('element').id;

        var self = this;

        this.graph = new Morris.Line(
        {
            element: element,
            xkey: 'date',
            ykeys: ['amount', 'increase'],
            labels: ['Amount', 'increase'],
            resize: true,
            smooth: false,
            parseTime:false

        }).on('click', function(i, row){

            self.set('clicked', row);
            self.sendAction('goToSession');

        });
    }

});

在我的控制器中,当goToSession 事件被触发时,它传入component 对象。如果它只是传入row 对象,我会更喜欢它。

这是用row 参数冒泡action 的正确方法吗?

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    最好将行对象与操作本身一起发送。 Ember.js 组件允许发送包含参数的操作。例如:

    this.sendAction('goToSession', row);
    

    在动作中翻译成这样的东西(存在于控制器或路由中):

    actions: {
      goToSession: function(row) {
        // manipulate morris row
      }
    }
    

    在Ember.js components guide 中了解有关在组件外部发送操作的更多信息。

    【讨论】:

    猜你喜欢
    • 2013-11-15
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    相关资源
    最近更新 更多