如何在Ember JS控制器加载时启动功能?

时间:2014-08-11 20:43:12

标签: javascript ember.js

我是Ember.js的新手。我正在尝试设置数据集的值:类别和数据集:数据:此脚本中的数据。情况是我跟着this article将Highcharts视图插入到我的模板中,但我需要动态获取数据,所以我尝试修改控制器来处理这个问题。我究竟做错了什么?我该如何解决这个问题?

App.InsightsController = Ember.ArrayController.extend({
setupController: function(controller, model) {
    var chartJSON = []; // Empty in example but I have a valid JSON

    var KPICategories = [],
        KPIValues = [];
    for (var i = 0; i < chartJSON.length; i++) {
        var categoryDate = new Date(chartJSON[i]['file_creation_time']);
        KPICategories.push(categoryDate.getHours() + ':' + categoryDate.getMinutes());
        KPIValues.push(Math.round(chartJSON[i]['kpi_value']));
    };

    console.log(KPICategories);
    controller.set('dataset:categories', KPICategories);
    controller.set('dataset:data:data', KPIValues);
},
dataset: {
    categories: null,
    data: [{
        name: 'Downlink Throuput',
        data: null
    }]
}
});

1 个答案:

答案 0 :(得分:0)

setupController是一个路由挂钩。将其剪切/粘贴到路线中:

App.InsightsRoute = Ember.Route.extend({
  // This will fire after the model hook callback has returned, allowing you to interact with it.
  setupController: function(controller, model) {
    var chartJSON = []; // Empty in example but I have a valid JSON

    var KPICategories = [],
        KPIValues = [];
    for (var i = 0; i < chartJSON.length; i++) {
        var categoryDate = new Date(chartJSON[i]['file_creation_time']);
        KPICategories.push(categoryDate.getHours() + ':' + categoryDate.getMinutes());
        KPIValues.push(Math.round(chartJSON[i]['kpi_value']));
    };

    console.log(KPICategories);
    controller.set('dataset:categories', KPICategories);
    controller.set('dataset:data:data', KPIValues);
  }
});

您可以直接从此处启动控制器中的任何方法。例如:

App.InsightsRoute = Ember.Route.extend({
  // This will fire after the model hook callback has returned, allowing you to interact with it.
  setupController: function(controller, model) {
    //... You already set up the properties and whatnot...
    controller.fireOffMethod(args);
    controller.set('property_bar', controller.get('foo'));
  }
});