mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
30 lines
895 B
CoffeeScript
30 lines
895 B
CoffeeScript
angular.module("ofn.admin").factory "pendingChanges", (dataSubmitter) ->
|
|
pendingChanges: {}
|
|
|
|
add: (id, attr, change) ->
|
|
@pendingChanges["#{id}"] = {} unless @pendingChanges.hasOwnProperty("#{id}")
|
|
@pendingChanges["#{id}"]["#{attr}"] = change
|
|
|
|
removeAll: ->
|
|
@pendingChanges = {}
|
|
|
|
remove: (id, attr) ->
|
|
if @pendingChanges.hasOwnProperty("#{id}")
|
|
delete @pendingChanges["#{id}"]["#{attr}"]
|
|
delete @pendingChanges["#{id}"] if @changeCount( @pendingChanges["#{id}"] ) < 1
|
|
|
|
submitAll: ->
|
|
all = []
|
|
for id, objectChanges of @pendingChanges
|
|
for attrName, change of objectChanges
|
|
all.push @submit(change)
|
|
all
|
|
|
|
submit: (change) ->
|
|
dataSubmitter(change).then (data) =>
|
|
@remove change.object.id, change.attr
|
|
change.scope.reset( data["#{change.attr}"] )
|
|
|
|
changeCount: (objectChanges) ->
|
|
Object.keys(objectChanges).length
|