如何调用序列化程序

时间:2015-06-29 11:01:23

标签: ember-cli

我正在使用ember cli

ember 1.12.0 英国数据1.0.0-beta.18

router.js:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('datasource');
});

//export default Router;
export default Router;

路由/ datasource.js:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    // the model is an Array of all of the posts
    // fetched from this url
    return Ember.$.ajax('/datasource/');
    //return [{'datasource': '1'}, {'datasource': '2'}];
  }
});

适配器/ application.js中:

import DS from 'ember-data';

export default DS.Adapter.extend({
  // ...your code here
});

模型/ datasource.js:

import DS from 'ember-data';
export default DS.Model.extend({
  dsn: DS.attr()
});

串行器/ datasource.js:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({

  extractArray: function(store, type, payload) {

    var datasources = payload._items;

    payload = {datasources: datasources};

    return this._super(store, type, payload);

  }


});

即我的api返回键_items中的项目列表。

但看起来好像永远不会执行序列化程序,

如何使序列化程序生效?

这是错误 -

Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed '{_items: [object Object],[object Object], _links: [object Object], _meta: [object Object]}' (wrapped in (generated datasource controller))Ember.default.assert @ ember.debug.js:4854exports.default.CollectionView.default.extend._assertArrayLike @ ember.debug.js:38837(anonymous function) @ ember.debug.js:37836ContainerView.default.extend.init @ ember.debug.js:37804superWrapper @ ember.debug.js:17426superFunction @ ember.debug.js:13805mixin.Mixin.create.init @ ember.debug.js:38898superWrapper @ ember.debug.js:17426superFunction @ ember.debug.js:13805exports.default.CollectionView.default.extend.init @ ember.debug.js:38832superWrapper @ ember.debug.js:17426Class @ ember.debug.js:30649ClassMixinProps.create @ ember.debug.js:31071mixin.Mixin.create.createChildView @ ember.debug.js:35755merge.default.appendChild @ ember.debug.js:39847mixin.Mixin.create.appendChild @ ember.debug.js:35697appendTemplatedView @ ember.debug.js:8051viewHelper @ ember.debug.js:7559collectionHelper @ ember.debug.js:6410eachHelper @ ember.debug.js:6598block @ ember.debug.js:7807render @ datasource.js:89renderHTMLBarsTemplate @ ember.debug.js:8491renderView @ ember.debug.js:8463renderView @ ember.debug.js:35400mixin.Mixin.create.render @ ember.debug.js:35423EmberRenderer_createElement @ ember.debug.js:37468Renderer_renderTree @ ember.debug.js:9140scheduledRenderTree @ ember.debug.js:9216Queue.invoke @ ember.debug.js:878Queue.flush @ ember.debug.js:943DeferredActionQueues.flush @ ember.debug.js:748Backburner.end @ ember.debug.js:173(anonymous function) @ ember.debug.js:576

1 个答案:

答案 0 :(得分:1)

在路线的模型钩子中,您正在进行ajax调用,而不是调用商店来查找记录。使用ajax调用,您绕过了商店,因此永远不会调用序列化程序来序列化从服务器返回的有效负载。

为了将有效负载传递到商店,您可以这样做:

// route
model: function() {
    var store = this.store;
    return Ember.$.ajax('/datasource/').then(function(payload) {
        store.pushPayload('datasource', payload);
    });
}

或者,您可以致电商店拨打您的后端电话,这样您就不必自己进行ajax调用(不确定是否打算出于某种原因打算进行自定义调用):

// route
model: function() {
    return this.store.find('datasource');
}