Skip to content

Instantly share code, notes, and snippets.

@cesarhdz
Last active October 4, 2016 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarhdz/c574d4833caf184dd558452e21c1c79a to your computer and use it in GitHub Desktop.
Save cesarhdz/c574d4833caf184dd558452e21c1c79a to your computer and use it in GitHub Desktop.
// 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