nodulator
Version:
Complete NodeJS Framework for Restfull APIs
69 lines (56 loc) • 1.8 kB
text/coffeescript
ResourceService = (name, injects) ->
if '$http' not in injects
injects.push '$http'
if 'socket' not in injects
injects.push 'socket'
class _ResourceService extends Service name, injects
list: []
current: null
_resName: name + 's'
_lName: name
Init: ->
window.addEventListener "load", (event) =>
.On 'new_' + , (item) => item
.On 'update_' + , (item) => item
.On 'delete_' + , (item) => item
()
, false
OnNew: (item) ->
.push item
OnUpdate: (item) ->
toChange = _().findWhere id: item.id
toChange = _(toChange).extend item
OnDelete: (item) ->
= _().reject (i) -> i.id is item.id
List: (done) ->
.get '/api/1/' +
.success (data) =>
= data
done null, if done?
.error (data) ->
done data if done?
Fetch: (id, done) ->
.get '/api/1/' + + '/' + id
.success (data) =>
= data
done null, if done?
.error (data) ->
done data if done?
Delete: (id, done) ->
.delete '/api/1/' + + '/' + id
.success (data) =>
done() if done?
.error (data) ->
done data if done?
Add: (blob, done) ->
.post '/api/1/' + , blob
.success (data) =>
done null, data if done?
.error (data) ->
done data if done?
Update: (blob, done) ->
.put '/api/1/' + + '/' + blob.id, blob
.success (data) =>
done null, data if done?
.error (data) ->
done data if done?