javascript - Backbone Collection url with dynamic parameters -
javascript - Backbone Collection url with dynamic parameters -
i'm writing rails app backbone.js.
in rails, have route in config/routes.rb takes argument this: /api/u/foo. gets posts argument, in case foo. output of posts foo. foo user, has many posts.
in backbone, need set url attribute on collection take arguments route, this: /u/foo. want go /api/u/foo.json. how this? tried taking user argument on backbone route, could't collection.
the relevant part of routes.rb file:
scope 'api' "/u/:user", :action => "index", :controller => "posts" end
assuming foo parameter, makes sense backbone apply url fetch single user, not collection.
i believe routes fine /api/u should homecoming collection , /api/u/123 single user. want send parameters /api/u?foo=bar filter collection.
just send these in .fetch() phone call in backbone working. apply necessary changes in controller filter according various parameters possible.
updateknowing want posts per user, question has nested routes. has been asked here: backbone , rails nested routes
the documentation on backbone.js here: http://documentcloud.github.com/backbone/#faq-nested
to remain bit more restful, define routes so:
scope '/api' resources :users, only: [], path: 'u' resources :posts, only: :index end end which results in:
api_user_posts /api/u/:user_id/posts(.:format) api/posts#index
javascript ruby-on-rails backbone.js
Comments
Post a Comment