Define an ember data model for a nested remainder url

I am trying to do something that sounds simple, but I cannot find a solution.

My application should edit documents containing pages.

Here is my model:

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() }); 

And routes:

 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); } }); 

When I download document 1, the application call is http://www.myserver.com/api/document/1 .

The problem is that when I want to find a document page, it calls

  • http://www.myserver.com/api/pages/ID

instead

  • http://www.myserver.com/api/document/1/pages/ID

Nested abstract urls are important in my application.

I found various things on this subject, for example by adding links to the JSON response:

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

But when I call the pages, it asks for http://www.myserver.com/api/document/1/pages without an identifier.

I will also try to specify the document when I request the page:

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

It is not possible to find complete documentation on this, therefore, if someone can explain to me what happened, I will be happy.

Thanks.

+7
javascript rest ember-data
source share
3 answers

Depends on: EMBERG DATA → V1.0.0-BETA.9

The way to handle nested routes is hidden under release notes

  • Need to send back links with an answer like this

     { "document": { "id": 1, "title": "Titre du document", "links": {"pages" : "/documents/1/pages"} } 
  • You need to configure the adapter:page buldUrl , for example

     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; } }); 
+3
source share

@ code-jaff answer adapted to Ember 2.1.0:

 // app/adapters/page.js import DS from './application'; // I suppose you have your adapter/application.js export default DS.RESTAdapter.extend({ buildURL: function(type, id, snapshot) { return this.host + '/' + this.namespace + '/documents/' + snapshot.record.get('document.id') + '/pages/' + id; } }); 
+2
source share

Your problem is with quotes that surround your JSON ids. If you change your serializer so that there are no quotes for identifiers by either the document identifier or the page identifiers, you should get the expected behavior. In addition, you need to change the formatting of your links to indicate the relative path:

The resulting JSON should look like this:

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

See this answer for a description of why meeting Ember's expectations regarding the JSON format is important for an overview of the expected format.

0
source share

All Articles