All methods on StandingOrder service are instance methods, StandingOrders service loads instances of StandingOrder

This commit is contained in:
Rob Harrington
2016-11-24 15:46:34 +11:00
parent 6f4c9070f6
commit 05bc2bd293
3 changed files with 16 additions and 18 deletions

View File

@@ -1,19 +1,19 @@
angular.module("admin.standingOrders").controller "StandingOrderController", ($scope, StandingOrder, customers, schedules, paymentMethods, shippingMethods) ->
$scope.standingOrder = StandingOrder.standingOrder
$scope.standingOrder = new StandingOrder()
$scope.customers = customers
$scope.schedules = schedules
$scope.paymentMethods = paymentMethods
$scope.shippingMethods = shippingMethods
$scope.errors = StandingOrder.errors
$scope.errors = $scope.standingOrder.errors
$scope.distributor_id = $scope.standingOrder.shop_id # variant selector requires distributor_id
$scope.view = if $scope.standingOrder.id? then 'review' else 'details'
$scope.save = ->
$scope.standing_order_form.$setPristine()
if $scope.standingOrder.id?
StandingOrder.update()
$scope.standingOrder.update()
else
StandingOrder.create()
$scope.standingOrder.create()
$scope.setView = (view) -> $scope.view = view

View File

@@ -1,5 +1,12 @@
angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $http, StatusMessage, InfoDialog, StandingOrderResource) ->
instanceMethods =
class StandingOrder extends StandingOrderResource
errors: {}
constructor: (obj={}) ->
angular.extend(@, obj)
if $injector.has('standingOrder')
angular.extend(@, $injector.get('standingOrder'))
buildItem: (item) ->
return false unless item.variant_id > 0
return false unless item.quantity > 0
@@ -19,19 +26,10 @@ angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $htt
else
@standing_line_items.splice(index,1)
new class StandingOrder
standingOrder: new StandingOrderResource()
errors: {}
constructor: ->
if $injector.has('standingOrder')
angular.extend(@standingOrder, $injector.get('standingOrder'), instanceMethods)
create: ->
StatusMessage.display 'progress', 'Saving...'
delete @errors[k] for k, v of @errors
@standingOrder.$save().then (response) =>
angular.extend(@standingOrder, instanceMethods)
@$save().then (response) =>
StatusMessage.display 'success', 'Saved'
, (response) =>
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
@@ -40,8 +38,7 @@ angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $htt
update: ->
StatusMessage.display 'progress', 'Saving...'
delete @errors[k] for k, v of @errors
@standingOrder.$update().then (response) =>
angular.extend(@standingOrder, instanceMethods)
@$update().then (response) =>
StatusMessage.display 'success', 'Saved'
, (response) =>
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'

View File

@@ -1,4 +1,4 @@
angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOrderResource) ->
angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOrderResource, StandingOrder) ->
new class StandingOrders
byID: {}
pristineByID: {}
@@ -9,5 +9,6 @@ angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOr
load: (standingOrders) ->
for standingOrder in standingOrders
standingOrder = new StandingOrder(standingOrder)
@byID[standingOrder.id] = standingOrder
@pristineByID[standingOrder.id] = angular.copy(standingOrder)