Answer the question
In order to leave comments, you need to log in
How then to stick to the DRY principle in angularjs?
Friends, I'm very interested in the question of DRY principles in angular.
We made an auto-call with a factory in the controller:
GunService.posts({}).$promise.then(function (data) {
$scope.guns = data.results;
});
$scope.addNewGun = function () {
GunService.publish({
gun_type: "fireball",
data: {
text: $scope.post.text
}
}).$promise.then(function () {
//как правильно тут вызвать (reload) GunService.posts? Нужно ее завернуть в функцию ранее?
})
};
Answer the question
In order to leave comments, you need to log in
function GunService ($rootScope){
this.onGunAdded = function (callback){
return $rootScope.$on(GunService.events.ADDED, callback);
};
this.publish = function (params){
// do your thing ...
.then(function (newGun){
$rootScope.$emit(GunService.events.ADDED, newGun);
});
}
}
GunService.events = {
ADDED: 'GunService.added'
};
function SomeController(GunService) {
GunService.onGunAdded(gunAdded);
this.add = function (){
// do your thing
}
function gunAdded(newGun) {
// Use newly created gun or do .posts
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question