简体   繁体   中英

Ember-data resolves wrong API endpoint

I'm having an issue getting Ember.js with Ember Data to hit a nested resource API endpoint. Here is my code:

https://gist.github.com/feliksg/7470254

Here is what i'm using:

DEBUG: ------------------------------- ember.js:3224
DEBUG: Ember      : 1.2.0-beta.3 ember.js:3224
DEBUG: Ember Data : 1.0.0-beta.2 ember.js:3224
DEBUG: Handlebars : 1.0.0 ember.js:3224
DEBUG: jQuery     : 2.0.3 ember.js:3224
DEBUG: -------------------------------

I'm also using Ember Appkit as the base for this project.

Basically the issue is when I try to submit a new post, ember data does the following:

POST request to /user/posts

instead of a

POST request to /users/1/posts

In addition, for some reason the request payload as shown by chrome inspector shows the form data being passed to the API looks like this:

{ "user/post": { "published":false, "created_at":null, "user":"1" } }

However, I would expect the data to be passed in like this:

{"post": { "body":"some text...", "published":false, "created_at":null, "user_id":"1" } }

So for some reason, it doesn't even pass in the 'body' field even though I have it in the form.

Any help is greatly appreciated!




UPDATE 1

When I visit http://localhost:8000/#/users/1/posts , it sends a GET API request to /users.json . There must be something wrong with the way I set up the PostsRoute but i'm not sure how to fix it.




UPDATE 2

I've updated my PostsRoute to fetch the JSON without using Ember Data which returns the records, but now the posts template does not render. My PostsRoute now looks like this:

 PostsRoute = Ember.Route.extend model: (params) -> user = @modelFor('user') userId = user.get('id') return $.getJSON('http://localhost:5000/api/v1/users/' + userId + '/posts.json') 

I also get the following error:

Error while loading route: TypeError: Object # has no method 'slice'

So when you create/use 'users/post' you are defining a namespace where the post lives, not that it's underneath a specific model. AKA, it isn't going to use the associated user model to build up your url, it's just going to make requests using users as part of the post.

I'm not totally positive why the body isn't being attached to the post, are you sure the model includes the body? Are you sure the body property exists on the model you are sending into the createRecord?

BTW, needs doesn't do anything on a route, it only applies to controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM