javascript – 为嵌套的rest url定义ember数据模型

前端之家收集整理的这篇文章主要介绍了javascript – 为嵌套的rest url定义ember数据模型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想做一些听起来很简单的事情,但我找不到解决方案.

我的应用程序需要编辑包含页面的文档.

这是我的模型:

  1. MyApplication.Document = DS.Model.extend({
  2. title: DS.attr('string'),pages: DS.hasMany('page',{async: true})
  3. });
  4. MyApplication.Page = DS.Model.extend({
  5. document: DS.belongsTo('document',{async: true}),title: DS.attr('string'),params: DS.attr(),objects: DS.attr()
  6. });

路线:

  1. MyApplication.Router.map(function () {
  2. this.resource('document',{path: '/document/:document_id'});
  3. });
  4. MyApplication.Document = Ember.Route.extend({
  5. model: function (params) {
  6. return this.store.find('document',params.document_id);
  7. }
  8. });

当我加载文档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响应中添加链接

  1. {
  2. "document": {
  3. "id": "1","title": "Titre du document","pages": ["1","2","3"],"links": {"pages" : "pages"}
  4. },

但是当我调用这些页面时,它会在没有id的情况下请求http://www.myserver.com/api/document/1/pages.

当我要求页面时,我也尝试指定文档:

  1. this.store.find("page",1,{document:1});

找不到关于这个主题的完整文档,所以如果有人能解释我的错误,我会很高兴的.

谢谢.

解决方法

取决于:EMBER DATA> = V1.0.0-BETA.9

release notes下隐藏了处理嵌套路由的方法

>需要像这样回复回复链接

  1. {
  2. "document": {
  3. "id": 1,"links": {"pages" : "/documents/1/pages"}
  4. }

>您需要自定义适配器:页面的buldUrl方法

  1. MyApplication.PageAdapter = DS.RestAdapter.extend({
  2. // NOTE: this is just a simple example,but you might actually need more customization when necessary
  3. buildURL: function(type,id,snapshot) {
  4. return '/documents/' + snapshot.record.get('document.id') + '/pages/' + id;
  5. }
  6. });

猜你在找的JavaScript相关文章