我正在尝试按照http://emberjs.com/guides/models/connecting-to-an-http-server/使用 ember 数据,但是在创建其余 api 之后,我收到了 js 错误,例如:
加载路线时出错:
类型错误:数据未定义
关于代码:
Ember.assert("你必须id
在传递给push
", data.id != null);
我最初认为我需要在我创建的模型中包含“id”字段,但如果我这样做,它会引发错误,说我不应该将 id 字段放入模型中。其余 API 的输出是正确的:
{
"analyticsRun": {
"id": 1,
"analyticsPlan": "some plan",
"commandScript": "some script",
"analyticsRunParameters": [
"homeDir",
"historyDir",
"Sourcefolder",
"resultsDir",
"dataDir"
]
},
"analyticsRunParameters": [
{
"id": "homeDir",
"default": "Some default",
"description": "Where is the analytics folder",
"name": "homeDir",
"value": "Q:/Ember",
"type": "global"
},
{
"id": "historyDir",
"default": "Rhistory",
"description": "Where R image will be saved",
"name": "historyDir",
"value": "Rhistory",
"type": "global"
},
{
"id": "Sourcefolder",
"default": "J:/Analytics_R_Source_Scripts",
"description": "Where are source codes and functions are saved",
"name": "Sourcefolder",
"value": "J:/_R_Source_Scripts",
"type": "type 1"
},
{
"id": "resultsDir",
"default": "results",
"description": "Where are results stored",
"name": "resultsDir",
"value": "results",
"type": "global"
},
{
"id": "dataDir",
"default": "Export",
"description": "Where is raw csv stored",
"name": "dataDir",
"value": "Export",
"type": "global"
}
]
}
我的模型定义为:
AS.AnalyticsRun = DS.Model.extend({
'analyticsPlan' : DS.attr('string'),
'commandScript' : DS.attr('string'),
'parameters' : DS.hasMany('analyticsRunParameter')
});
AS.AnalyticsRunParameter = DS.Model.extend({
'name' : DS.attr('string'),
'type' : DS.attr('string'),
'description' : DS.attr('string'),
'default' : DS.attr('string'),
'value' : DS.attr('string')
});
我的路线是:
AS.AnalyticsConfigRoute = Ember.Route.extend({
model: function(param) {
var store = this.get('store');
return store.find('AnalyticsRun',param.runId);
}
});
从错误看来,代码正在执行 id 查找但未能找到它。以前有没有人遇到过这个问题,或者您对如何解决这个问题有任何建议?
注意:isuse 似乎是在 _find 函数中的 ember-data.js 文件中引起的。
return resolve(promise).then(function(payload) {
Ember.assert("You made a request for a " + type.typeKey + " with id " + id + ", but the adapter's response did not have any data", payload);
//payload has data here
payload = serializer.extract(store, type, payload, id, 'find');
//payload is undefined here!!
return store.push(type, payload);
}
谢谢,迪