emberjs - 处理从ajax调用返回的数据

时间:2018-03-12 04:57:00

标签: ajax ember.js

我确信这很简单但是......

我有一个使用Ajax调用来检索数据数组的ember路由。我想从该数组创建一个模型对象数组。

当我尝试创建有问题的模型实例时,我收到错误

Cannot read property '_attributes' of null TypeError: Cannot read property '_attributes' of null

为了尝试定义问题,我创建了几个模型实例,与Ajax调用返回的数据无关,例如:

var testPccB = ParentCostCentre.create({cceClientCode : "ABC" });

那里也会出现错误。

整个路线代码如下所示:

import Ember from 'ember';
import ParentCostCentre from "../models/parentcostcentre";

export default Ember.Route.extend({

    model() {
        return Ember.RSVP.hash({

            costcentres: this.store.findAll('costcentre'),

            parentcostcentres: this.testFindParents(),

        })
    },

    testFindParents: function () {
        var result = [];
        return new Ember.RSVP.Promise(function (resolve, reject) {

            const theDrvId = 888;
            const theClientCode = 'ABC';
            const theCceIdentifier = 'XYZXY'; 

            console.log("About to create testPccA");
            //this works
            var testPccA = ParentCostCentre.create();
            console.log("Finished create testPccA");
            console.log("About to create testPccB");
            //this generates the error
            var testPccB = ParentCostCentre.create({cceClientCode : "ABC" });
            console.log("Finished create testPccB");

            var theUrl = "api/parentcostcentres/" + theDrvId + "/" + theClientCode + "/" + theCceIdentifier ;

            Ember.$.ajax({
                type: 'GET',
                url: theUrl,
                success: function (data) {
                    data.forEach(function (item) {
                        result.push(ParentCostCentre.create(item));
                    });
                    resolve(result);
                },
                error: function (request, textStatus, error) {
                    console.log(error);
                    reject(error);
                }
            });
        });
    },     

    setupController(controller, model) {
            controller.set('costcentres', model.costcentres);
            controller.set('parentcostcentres', model.parentcostcentres);
    }
});

我在这里做不到的事情可以让它发挥作用吗?

编辑1:

这就是parentcostcentre模型的样子:

import DS from 'ember-data';

export default DS.Model.extend({
  cceClientCode: DS.attr('string'),
  cceIdentifier: DS.attr('string'),
  cciActiveFrom: DS.attr('date'),
  cciActiveTo: DS.attr('date'),
  cciAutoid: DS.attr('number'),
  cciCcGeoLevel: DS.attr('number'),
  cciCceId: DS.attr('number'),
  cciDescription: DS.attr('string'),
  cciPraId: DS.attr('number'),
  cciMatEmpId: DS.attr('number'),
  cciIsDisabled: DS.attr('number'),
  cciPostsummFlag: DS.attr('string'),
  cciTdEmpId: DS.attr('number'),
  emiActiveToPra: DS.attr('date'),
  emiActiveToTd: DS.attr('date'),
  emiEmailAddressPra: DS.attr('string'),
  emiEmailAddressTd: DS.attr('string'),
  emiNameFamilyPra: DS.attr('string'),
  emiNameFamilyTd: DS.attr('string'),
  emiNameFirstPra: DS.attr('string'),
  emiNameFirstTd: DS.attr('string')
});

编辑2 值得一提的是,API调用返回的数据如下所示。我不确定即使是这样的处理也是如此相关......

var testPccB = ParentCostCentre.create({cceClientCode:“ABC”});

...生成错误,但我将其包含在内以保证完整性。

[
  {
    "id": 5101,
    "cceClientCode": "ABC",
    "cceIdentifier": "XYZXY",
    "cciAutoid": 81415,
    "cciCceId": 5111,
    "cciActiveFrom": "2017-03-27T11:47:23",
    "cciActiveTo": "2300-01-01T00:00:00",
    "cciGeoId": 888,
    "cciIsDisabled": 0,
    "cciPraEmpId": 40336,
    "cciTdEmpId": 14694,
    "cciDescription": "South Bigtown",
    "cciPostsummFlag": "S",
    "cciCcFinancialLevel": 1,
    "emiNameFirstPra": "Phil",
    "emiNameFamilyPra": "Franklin",
    "emiActiveToPra": "2300-01-01T00:00:00",
    "emiEmailAddressPra": "Phil.Franklin@example.com",
    "emiNameFirstTd": "Phillipa",
    "emiNameFamilyTd": "Howard",
    "emiActiveToTd": "2300-01-01T00:00:00",
    "emiEmailAddressTd": "Phillipa.Howard@example.com"
  }
]

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。

我只是直接从Ajax传递响应,而不是尝试从中构建数组。所以testFindParents代码现在看起来像这样:

testFindParents: function () {
    var result = [];
    return new Ember.RSVP.Promise(function (resolve, reject) {

        const theDrvId = 888;
        const theClientCode = 'ABC';
        const theCceIdentifier = 'XYZXY'; 


        var theUrl = "api/parentcostcentres/" + theDrvId + "/" + theClientCode + "/" + theCceIdentifier ;

        Ember.$.ajax({
            type: 'GET',
            url: theUrl,
            success: function (data) {
                resolve(data);
            },
            error: function (request, textStatus, error) {
                console.log(error);
                reject(error);
            }
        });
    });
},     

当然,这并不能解释为什么我可以在测试代码中实例化parentcostcentre的实例,但至少解决了主要问题。