ofnTrackProduct and ofnTrackVariant accept nested properties as arguments

This commit is contained in:
Rohan Mitchell
2014-02-11 19:13:11 +11:00
parent 1ca9a86042
commit c7c70252d0
2 changed files with 22 additions and 16 deletions

View File

@@ -20,30 +20,31 @@ productEditModule.directive "ofnDecimal", ->
viewValue
productEditModule.directive "ofnTrackProduct", ->
productEditModule.directive "ofnTrackProduct", ['$parse', ($parse) ->
require: "ngModel"
link: (scope, element, attrs, ngModel) ->
property_name = attrs.ofnTrackProduct
ngModel.$parsers.push (viewValue) ->
if ngModel.$dirty
addDirtyProperty scope.dirtyProducts, scope.product.id, property_name, viewValue
parsedPropertyName = $parse(attrs.ofnTrackProduct)
addDirtyProperty scope.dirtyProducts, scope.product.id, parsedPropertyName, viewValue
scope.displayDirtyProducts()
viewValue
]
productEditModule.directive "ofnTrackVariant", ->
productEditModule.directive "ofnTrackVariant", ['$parse', ($parse) ->
require: "ngModel"
link: (scope, element, attrs, ngModel) ->
property_name = attrs.ofnTrackVariant
ngModel.$parsers.push (viewValue) ->
dirtyVariants = {}
dirtyVariants = scope.dirtyProducts[scope.product.id].variants if scope.dirtyProducts.hasOwnProperty(scope.product.id) and scope.dirtyProducts[scope.product.id].hasOwnProperty("variants")
if ngModel.$dirty
addDirtyProperty dirtyVariants, scope.variant.id, property_name, viewValue
addDirtyProperty scope.dirtyProducts, scope.product.id, "variants", dirtyVariants
parsedPropertyName = $parse(attrs.ofnTrackVariant)
addDirtyProperty dirtyVariants, scope.variant.id, parsedPropertyName, viewValue
addDirtyProperty scope.dirtyProducts, scope.product.id, $parse("variants"), dirtyVariants
scope.displayDirtyProducts()
viewValue
]
productEditModule.directive "ofnToggleVariants", ->
link: (scope, element, attrs) ->
@@ -515,13 +516,11 @@ filterSubmitProducts = (productsToFilter) ->
filteredProducts
addDirtyProperty = (dirtyObjects, objectID, propertyName, propertyValue) ->
if dirtyObjects.hasOwnProperty(objectID)
dirtyObjects[objectID][propertyName] = propertyValue
else
addDirtyProperty = (dirtyObjects, objectID, parsedPropertyName, propertyValue) ->
if !dirtyObjects.hasOwnProperty(objectID)
dirtyObjects[objectID] = {}
dirtyObjects[objectID]["id"] = objectID
dirtyObjects[objectID][propertyName] = propertyValue
parsedPropertyName.assign(dirtyObjects[objectID], propertyValue)
removeCleanProperty = (dirtyObjects, objectID, propertyName) ->

View File

@@ -202,10 +202,17 @@ describe "filtering products for submission to database", ->
describe "Maintaining a live record of dirty products and properties", ->
parse = null
beforeEach ->
module "ofn.bulk_product_edit"
beforeEach inject(($parse) ->
parse = $parse
)
describe "adding product properties to the dirtyProducts object", -> # Applies to both products and variants
it "adds the product and the property to the list if property is dirty", ->
dirtyProducts = {}
addDirtyProperty dirtyProducts, 1, "name", "Product 1"
addDirtyProperty dirtyProducts, 1, parse("name"), "Product 1"
expect(dirtyProducts).toEqual 1:
id: 1
name: "Product 1"
@@ -216,7 +223,7 @@ describe "Maintaining a live record of dirty products and properties", ->
id: 1
notaname: "something"
addDirtyProperty dirtyProducts, 1, "name", "Product 3"
addDirtyProperty dirtyProducts, 1, parse("name"), "Product 3"
expect(dirtyProducts).toEqual 1:
id: 1
notaname: "something"
@@ -228,7 +235,7 @@ describe "Maintaining a live record of dirty products and properties", ->
id: 1
name: "Product 1"
addDirtyProperty dirtyProducts, 1, "name", "Product 2"
addDirtyProperty dirtyProducts, 1, parse("name"), "Product 2"
expect(dirtyProducts).toEqual 1:
id: 1
name: "Product 2"