Skip to content

Instantly share code, notes, and snippets.

@asqd
Created January 26, 2015 13:12
Show Gist options
  • Save asqd/c3bea3006fb177fce576 to your computer and use it in GitHub Desktop.
Save asqd/c3bea3006fb177fce576 to your computer and use it in GitHub Desktop.
Ember Custom REST actions mixin
# Provides custom REST actions in your application
#
# For example:
# We need to update rating of current post using action /posts/:id/voite
#
# Extend your model with mixin
# App.Post = DS.Model.extend App.CustomRestMixin,
#
# Then in Post controller you can use custom REST actions
#
# voite: ->
# options =
# data:
# post:
# rating: 5
# userId: @get('currentUser.id')
#
# @get('model').put("%@/voite".fmt(@get('id')), options)
#
App.CustomRestMixin = Ember.Mixin.create
_ajax: (url, method, options) ->
type = @get('constructor')
adapter = @get('store').adapterFor type
url = '%@/%@'.fmt adapter.buildURL(type.typeKey), url
adapter.ajax url, method, options
get: (url, options) ->
@_ajax url, 'GET', options
post: (url, options) ->
@_ajax url, 'POST', options
put: (url, options) ->
@_ajax url, 'PUT', options
delete: (url, options) ->
@_ajax url, 'DELETE', options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment