I'm planning on using ember.js, however my REST api doesn't exactly align with the packaged REST Adapter. I would like to "override" find and be able to put my own ajax in it. I dislike how an ember findAll retrieves all my documents with no options for pagination, so that along with other query parameters would be useful --which is why I want to write my own ajax. I've been unable to find any documentation on how I would go about doing this.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Carriage Return (ASCII chr 13) is missing from tex
- void before promise syntax
- Keeping track of variable instances
For Ember Data
This is up to date as of Ember Data 1.0 beta 9.
Extend one of the Ember Data Adapters. To make it site wide:
To make it model specific:
Then you will define the implementation you'd like to override. You always have the option to call
this._super
and revert to the base implementation. e.g.Or you can completely override the implementation:
To see the complete api you can override see: http://emberjs.com/api/data/classes/DS.RESTAdapter.html
Serializer
Often more important will be rolling your own serializer for massaging the data to fit your rest endpoint. Here's some useful information from the transition document https://github.com/emberjs/data/blob/master/TRANSITION.md .
The short version is that once an Ajax request has completed, the resulting payload is sent through the following hooks:
Rolling your own
Then from your route, or wherever
Now personally I'd inject the adapter onto the routes just to make my life easier:
Then on the route you could be lazier and do:
Example: http://emberjs.jsbin.com/OxIDiVU/676/edit
You can read more about Ember without Ember Data: Ember without Ember Data
For those who code adapter themselves, if you need to return a value from your adapter (for example, userId), you can either return json or promise. Here is example of returning promise:
You can get more info about Ember promises here: https://hackhands.com/3-ways-ember-js-leverages-promises/ or here http://emberjs.com/api/classes/RSVP.Promise.html
I had the same problem. I too wanted to use a slightly different format with my backend (cakePHP) and could not figure out how to do it. The previous answers are great but you might not need to redefine every method but simply change the format of the URL by overriding the buildURL in the RESTAdapter.
For example, I want to use cakePHP's extension and want my urls to looks like so, application wide:
After much hair pulling and the realization that ember-data is essential I used the following code:
Ember's docs are good, but most of their examples use FIXTURE data. I wish they had a simple example how to write different types of adapters for different situations.