我想做一些听起来很简单的事情,但我找不到解决方案.
我的应用程序需要编辑包含页面的文档.
这是我的模型:
- MyApplication.Document = DS.Model.extend({
- title: DS.attr('string'),pages: DS.hasMany('page',{async: true})
- });
- MyApplication.Page = DS.Model.extend({
- document: DS.belongsTo('document',{async: true}),title: DS.attr('string'),params: DS.attr(),objects: DS.attr()
- });
路线:
- MyApplication.Router.map(function () {
- this.resource('document',{path: '/document/:document_id'});
- });
- MyApplication.Document = Ember.Route.extend({
- model: function (params) {
- return this.store.find('document',params.document_id);
- }
- });
当我加载文档1时,应用程序调用http://www.myserver.com/api/document/1.
> http://www.myserver.com/api/pages/ID
代替
> http://www.myserver.com/api/document/1/pages/ID
这些嵌套URL在我的应用程序中很重要.
我在这个主题上找到了不同的东西,比如在JSON响应中添加链接:
- {
- "document": {
- "id": "1","title": "Titre du document","pages": ["1","2","3"],"links": {"pages" : "pages"}
- },
但是当我调用这些页面时,它会在没有id的情况下请求http://www.myserver.com/api/document/1/pages.
当我要求页面时,我也尝试指定文档:
- this.store.find("page",1,{document:1});
找不到关于这个主题的完整文档,所以如果有人能解释我的错误,我会很高兴的.
谢谢.
解决方法
取决于:EMBER DATA> = V1.0.0-BETA.9
在release notes下隐藏了处理嵌套路由的方法
- {
- "document": {
- "id": 1,"links": {"pages" : "/documents/1/pages"}
- }
- MyApplication.PageAdapter = DS.RestAdapter.extend({
- // NOTE: this is just a simple example,but you might actually need more customization when necessary
- buildURL: function(type,id,snapshot) {
- return '/documents/' + snapshot.record.get('document.id') + '/pages/' + id;
- }
- });