Last active
October 4, 2016 18:50
-
-
Save cesarhdz/c574d4833caf184dd558452e21c1c79a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Store | |
factory(QuoteStore, function(quoteService){ | |
var subscribers = []; | |
var state; | |
function QuoteStore(){} | |
QuoteStore.prototype.getState = function(){ | |
return state; | |
} | |
QuoteStore.prototype.dispatch = function(action, payload){ | |
var store = this; | |
var state = this.getState(); | |
// Update state | |
state = quoteService[action](state, payload); | |
return _.map(this.subscribers, function(fn){ | |
return fn(store); | |
}); | |
}; | |
QuoteStore.prototype.suscribe = function(fn){ | |
var store = this; | |
subscribers = subscribers.concat([fn]); | |
return function(){ | |
subscribers = _.filter(subscribers, function(fn){ | |
return fn !== fn; | |
}) | |
} | |
}; | |
QuoteStore.prototype.startWith = function(data){ | |
state = data; | |
return this; | |
}; | |
return new QuoteStore(); | |
}); | |
// Service | |
QuoteService.prototype.addPromotion = function(state, payload){ | |
return state; | |
} | |
// Route Controller | |
directive('SaleFinceCtrl', function(quoteStore, quote){ | |
function vm(state){ | |
$scope.quote = state; | |
} | |
var unsubscribe = quoteStore.subscribe(vm); | |
$scope.$on('$destroy', unsubscribe); | |
// Init scope | |
vm(QuoteStore.startWith(quote).getState()); | |
}) | |
// Directive | |
directive('QuoteEditorCtrl', function(quoteStore){ | |
return { | |
scope: { | |
resource: 'resource' | |
}, | |
controllerAs: 'quote', | |
controller: 'QuoteEditor' | |
} | |
}); | |
// Directive Controller | |
controller('QuoteEditorCtrl', function(quoteStore, $scope){ | |
function vm(){ | |
this.line = $scope.line; | |
this.cmd = x; | |
} | |
// Actions | |
this.addPromotion = function(cmd){ | |
QuoteStore.dispatch('addPromotion', cmd); | |
} | |
var unsubscribe = QuoteStore.subscribe(vm); | |
$scope.$on('$destroy', unsubscribe); | |
init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment