Mil*_*Joe 11 javascript ember.js asp.net-web-api ember-data ember-old-router
我正在尝试将一些东西与ember + emberdata + router + asp.net web api放在一起.大多数它似乎工作,但我陷入了一个错误消息,当ember-data尝试findAll通过适配器为我的模型时我得到.
在我的后端,我有一个这样的模型(C#):
public class Genre {
[Key]
public int Id { get; set; }
[Required]
[StringLength(50, MinimumLength=3)]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我使用ember-data代表它:
App.Genre = DS.Model.extend({
id: DS.attr("number"),
name: DS.attr("string")
}).reopenClass({
url: 'api/genre'
});
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用RESTAdapter定义了一个Store,如下所示:
App.store = DS.Store.create({
revision: 4,
adapter: DS.RESTAdapter.create({
bulkCommit: false
})
});
Run Code Online (Sandbox Code Playgroud)
商店在我的控制器中使用如下:
App.GenreController = Ember.ArrayController.extend({
content: App.store.findAll(App.Genre),
selectedGenre: null
});
Run Code Online (Sandbox Code Playgroud)
路由器定义为
App.router = Em.Router.create({
enableLogging: true,
location: 'hash',
root: Ember.Route.extend({
//...
genre: Em.Route.extend({
route: '/genre',
index: Ember.Route.extend({
connectOutlets: function (router, context) {
router.get('applicationController').connectOutlet('genre');
}
})
}),
//...
})
})
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我得到具有相同结构的每个对象的以下消息:
未捕获错误:断言失败:您的服务器返回带有键0的哈希,但您没有映射
作为参考,这是服务返回的json:
[
{
"id": 1,
"name": "Action"
},
{
"id": 2,
"name": "Drama"
},
{
"id": 3,
"name": "Comedy"
},
{
"id": 4,
"name": "Romance"
}
]
Run Code Online (Sandbox Code Playgroud)
我无法确切地知道问题是什么,因为断言提到我需要映射,我想知道:
欢迎任何帮助.
如果您认为这还不足以理解问题,我绝对可以添加更多信息.
编辑:我在后端更改了一些内容,现在我findAll()在服务器中的等效操作将输出序列化为以下json:
{
"genres": [
{ "id": 1, "name": "Action" },
{ "id": 2, "name": "Drama" },
{ "id": 3, "name": "Comedy" },
{ "id": 4, "name": "Romance" }
]
}
Run Code Online (Sandbox Code Playgroud)
但我仍然无法让它在客户端填充我的模型,我的错误消息已更改为:
未捕获错误:断言失败:您的服务器返回了具有关键类型的哈希,但您没有映射
不知道还有什么我可能做错了.
抛出此异常的方法是sideload检查映射,如下所示:
sideload: function (store, type, json, root) {
var sideloadedType, mappings, loaded = {};
loaded[root] = true;
for (var prop in json) {
if (!json.hasOwnProperty(prop)) { continue; }
if (prop === root) { continue; }
sideloadedType = type.typeForAssociation(prop);
if (!sideloadedType) {
mappings = get(this, 'mappings');
Ember.assert("Your server returned a hash with the key " + prop + " but you have no mappings", !!mappings);
//...
Run Code Online (Sandbox Code Playgroud)
此调用sideloadedType = type.typeForAssociation(prop);返回undefined,然后我收到错误消息.该方法typeForAssociation()检查'associationsByName'返回空的for 键Ember.Map.
目前仍然没有解决方案.
顺便说说...
我的行动现在是这样的:
// GET api/genres
public object GetGenres() {
return new { genres = context.Genres.AsQueryable() };
}
// GET api/genres
//[Queryable]
//public IQueryable<Genre> GetGenres()
//{
// return context.Genres.AsQueryable();
//}
Run Code Online (Sandbox Code Playgroud)
我不得不删除由json.NET序列化的原始实现,因为我找不到配置选项来生成jmber输出,因为Ember-Data期望(如{resource_name : [json, json,...]}).这样做的副作用是我失去了内置的OData支持,但我想保留它.有谁知道如何配置它来为集合生成不同的json?
sly*_*7_7 12
可以在DS.RESTAdapter中定义映射.我想你可以尝试定义这样的东西:
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.create({
bulkCommit: true,
mappings: {
genres: App.Genre
},
// you can also define plurals, if there is a unregular plural
// usually, RESTAdapter simply add a 's' for plurals.
// for example at work we have to define something like this
plurals: {
business_process: 'business_processes'
//else it tries to fetch business_processs
}
}),
revision: 4
});
Run Code Online (Sandbox Code Playgroud)
希望这可以解决您的问题.
更新:
在这个时候,这还没有很好的记录,我不记得我们是通过自己阅读代码找到它,还是Tom Dale指出它.
无论如何,这里是复数的重点
对于映射,我认为我们是由与你相同的错误驱动的,要么我们尝试过,要么汤姆教我们这个.
该RESTAdapter期望返回JSON到的形式为:
{
"genres": [{
"id": 1,
"name": "action"
},{
"id": 2,
"name": "Drama"
}]
}
Run Code Online (Sandbox Code Playgroud)
测试是一个很好的文档来源,请参阅https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/rest_adapter_test.js#L315-329
| 归档时间: |
|
| 查看次数: |
8098 次 |
| 最近记录: |