Generalising datasubmitter pendingChanges and watcher directive

This commit is contained in:
Rob Harrington
2015-04-15 11:29:51 +10:00
parent 30bb948d35
commit b16fa9cdc1
3 changed files with 66 additions and 53 deletions

View File

@@ -1,18 +1,36 @@
angular.module("ofn.admin").directive "ofnLineItemUpdAttr", (switchClass, pendingChanges) ->
scope:
lineItem: "&ofnLineItemUpdAttr"
attrName: "@"
object: "&ofnLineItemUpdAttr"
type: "@ofnLineItemUpdAttr"
attr: "@attrName"
link: (scope, element, attrs) ->
element.dbValue = scope.lineItem()[scope.attrName]
scope.$watch "lineItem().#{scope.attrName}", (value) ->
if value == element.dbValue
pendingChanges.remove(scope.lineItem().id, scope.attrName)
switchClass( element, "", ["update-pending", "update-error", "update-success"], false )
scope.savedValue = scope.object()[scope.attr]
scope.$watch "object().#{scope.attr}", (value) ->
if value == scope.savedValue
pendingChanges.remove(scope.object().id, scope.attr)
scope.clear()
else
changeObj =
lineItem: scope.lineItem()
element: element
attrName: scope.attrName
url: "/api/orders/#{scope.lineItem().order.number}/line_items/#{scope.lineItem().id}?line_item[#{scope.attrName}]=#{value}"
pendingChanges.add(scope.lineItem().id, scope.attrName, changeObj)
switchClass( element, "update-pending", ["update-error", "update-success"], false )
change =
object: scope.object()
type: scope.type
attr: scope.attr
value: value
scope: scope
scope.pending()
pendingChanges.add(scope.object().id, scope.attr, change)
scope.reset = (value) ->
scope.savedValue = value
scope.success = ->
switchClass( element, "update-success", ["update-pending", "update-error"], 3000 )
scope.pending = ->
switchClass( element, "update-pending", ["update-error", "update-success"], false )
scope.error = ->
switchClass( element, "update-error", ["update-pending", "update-success"], false )
scope.clear = ->
switchClass( element, "", ["update-pending", "update-error", "update-success"], false )