emberjs - 处理从 ajax 调用返回的数据
emberjs - processing data returned from ajax call
我相信这很简单但是...
我有一个 ember 路由,它使用 Ajax 调用来检索数据数组。我想从该数组创建一个模型对象数组。
当我尝试创建相关模型的实例时出现错误
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"
}
]
好的,我找到了答案。
我只是直接传递了 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
的实例,但至少主要问题已经解决了。
我相信这很简单但是...
我有一个 ember 路由,它使用 Ajax 调用来检索数据数组。我想从该数组创建一个模型对象数组。
当我尝试创建相关模型的实例时出现错误
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"
}
]
好的,我找到了答案。
我只是直接传递了 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
的实例,但至少主要问题已经解决了。