将Ember-Data模型转换为数组

时间:2014-11-17 03:24:47

标签: ember.js ember-data handsontable

我试图将Ember-Data Model实例转换为数组,目标是将其加载到Handsontable(http://handsontable.com)中。 toArray()方法似乎不会将StatVals模型的属性转换为数组,而是创建一个StatVals对象数组。如何从Ember-Data模型属性中正确构造数组?

此外,我希望新数组包含timeelement,而不是statVal模型的其他属性。我该怎么做呢?

构建Handsontable的视图:

App.chapterView = Ember.View.extend({
  tagName: 'div',
  classNames: ['dataTable'],
  insertTable: function(){
    var divElement = jQuery('.dataTable');
    var data = this.get('controller.model.statVals').toArray();
    divElement.handsontable({ data: data });
  }.on('didInsertElement')
});

型号:

App.Chapter = DS.Model.extend({
  name: DS.attr('string'),
  createdDate: DS.attr('date'),
  statVals: DS.hasMany('statVal', { inverse: 'chapter', async: true}),
  user: DS.belongsTo('user', { inverse: 'chapter', async: true} )
});


App.StatVal = DS.Model.extend({
  time: DS.attr('date'),
  Element: DS.attr('number'),
  Chapter: DS.belongsTo('chapter'),
  user: DS.belongsTo('user', { inverse: 'statVals', async: true} )
});

我发现了以下相关问题,但我并没有说服他们提出最适合我情况的方法:

ember-data as data for d3

What is the Ember way of converting retrieved Ember Data records into plain objects?

1 个答案:

答案 0 :(得分:1)

只需移除statVals中的var data = this.get('controller.model.statVals').toArray();,我就可以使用来自Ember-Data的数据提供Handsontable表格。完整的View代码是:

    App.chapterView = Ember.View.extend({
      tagName: 'div',
      classNames: ['dataTable'],
      insertTable: function(){
        var divElement = jQuery('.dataTable');
        var data = this.get('controller.model').toArray();
        divElement.handsontable({ data: data });
      }.on('didInsertElement')
    });