Emberjs-data Trying
In my old emberjs project(money,food-ntpc) i didn’t use ember-data to handle data processing with backend server. The ember-data is closed to 1.0.0(now is 1.0.0.beta4) so i give it a try. The official document is quite less and some posts in StackOverflow are out of date. Checkout their source code is the quickest way to understand how they work and do customized. I write down tips here for my reference in the future.
Define your store to use ember-data
1 2 3 4 5 |
|
Assume your have a record
object and json format is
1 2 3 4 5 6 7 8 |
|
Define your model object.
1 2 3 4 |
|
Get all MyApp.Record
from backend server.
1 2 3 4 |
|
If you want to use store without in route,controller
1
|
|
The backend should provide the REST api path http://127.0.0.1/records
.
Server response json should be like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The default root of json become records
not record
and content should be a Array.
In case you want to pass parameter into URL like http://127.0.0.1/records?year=2014
.
1 2 3 4 |
|
Get single record.
1 2 3 4 5 |
|
The backend should provide the REST api path http://127.0.0.1/record/1
.
Customerized
change default request url
Your backend server may provide REST api like http://127.0.0.1/api/records
.
You could add namesapce to your rest adapter.
1 2 |
|
change single model request url
Your backend server doesn’t provide REST api http://127.0.0.1/api/records
but http://127.0.0.1/api/usages
.
The responsed json format doesn’t change.
1 2 3 4 5 6 7 8 9 10 11 |
|
change root of responsed json
Your backend server change the default json format from
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Simple extend the original serializer and overwrite methods
1 2 3 |
|
Refer to rest_serializer.js to know more customerized.