Skip to content

Instantly share code, notes, and snippets.

@harshalbhakta
Created September 16, 2013 07:13
Show Gist options
  • Select an option

  • Save harshalbhakta/6577525 to your computer and use it in GitHub Desktop.

Select an option

Save harshalbhakta/6577525 to your computer and use it in GitHub Desktop.
Angular resource with additional methods.
app.factory "User", ["$http","$resource",($http,$resource) ->
User = $resource("/roles/:id", {id: "@id"}, {update: {method: "PUT"}})
# Initialize the data using a static method
User.init = (data) ->
angular.extend this, data
# a static method to retrieve User by ID
User.get_it = (id) ->
$http.get("/roles/" + id).then (response) ->
new User.init(response.data)
# an instance method to create a new Book
User::create = ->
user = this
$http.post("/roles/", user).then (response) ->
user.id = response.data.id
user
User
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment