J
J
jack3d2015-08-26 15:43:31
Angular
jack3d, 2015-08-26 15:43:31

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;
    });

Added a new gun:
$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

1 answer(s)
_
_ _, 2015-08-26
@AMar4enko

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 question

Ask a Question

731 491 924 answers to any question