ember.js - RestAdaptor and API Structure -


i'm newbie ember data , i've done date fixture data today i'm trying graduate real deal , realising don't know enough how connect model , api's call signature.

specifically i'd able call endpoint get /activities/[:user_id]/[date]. load array of "activity" objects given date. know can offset api's directory with:

 ds.restadapter.reopen({    namespace: 'api'  }); 

in case api prefix appropriate. think should able date component solved setting route this:

this.resource('activities', { path: '/activities' }, function() {     this.route('by_date', {path: '/:target_date'}); }); 

the above educated guess because i'm @ loss on how user_id component url signature. can help? there tutorials or examples of basic ember data use cases?

also, because know i'm going run next ... how 1 add parameters url string (aka, /foobar?something=value) versus parameters url (like above)?

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= update -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

i've implemented suggestions @intuitivepixel still having problems ...

first off tried hard code values userid , dateby:

router:

this.resource('activities', { path: '/activities' }, function() {     this.route('by_date', {path: '/:user_id/:by_date'}); }); 

route:

app.activitiesbydateroute = ember.route.extend({       serialize: function(activity) {       return {         userid: 1,         dateby: "2013-07-01"       };     }      }); 

sadly did not work. think understand why -- although don't have quick way fix -- more disturbing me when manually put in parameters url: http://restful.service.com/api/activities/1/2013-07-01. results quite surprising me:

  • initially debugging messages suggest success:
  • this however, not correct no network requests made
  • if reload browser, go out , activities surprise goes out find specified user. hmmm. that's ok, user 1 pulled successfully.
  • the activity, however, get /activities call fails because endpoint needs user , date qualifier work. why weren't these included in request?

i know can offset api's directory with:

you can set different url of our api if it's case:

ds.restadapter.reopen({   url: 'http://myapihost.com',   namespace: 'api' }); 

this produce url http://myapihost.com/api/

the above educated guess because i'm @ loss on how user_id component url signature.

following example , adding user_id dynamic segment, let's have router map definition:

this.resource('activities', { path: '/activities' }, function() {   this.route('bydate', {path: '/:user_id/:target_date'}); }); 

and {{linkto}} helper:

{{#each activity in model}}   {{#linkto 'activities.bydate' activity}} {{/each}} 

then build url out of multiple dynamic segments hook route's serialize function , returning hash composed out of dynamic segments url needs:

app.activitiesbydateroute = ember.route.extend({   serialize: function(activity) {     return {       user_id: activity.get('userid'),       target_date: activity.get('targetdate')     }   } }); 

the code above generate url /activities/[userid]/[targetdate] when {{linkto}} link clicked. example assumes properties need build url available in activity model.

also, because know i'm going run next ... how 1 add parameters url string (aka, /foobar?something=value) versus parameters url (like above)?

this kind of url queries not yet supported ember framework, workarounds/projects try deal missing feature like: https://github.com/alexspeller/ember-query, , if pr get's merged day have them in ember, time beeing use above mentioned library have support custom queries. current status whether lib get's merged or not have here: http://discuss.emberjs.com/t/query-string-support-in-ember-router there discussion going on.

hope helps.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -