E
E
E_K2016-06-18 03:35:50
Angular
E_K, 2016-06-18 03:35:50

How to properly organize code reuse when using $Resource?

To use the $resource service, a factory is used (taken from here: https://gist.github.com/djavier/8186812):

module.factory( 'Resource', [ '$resource', function( $resource ) {
   return function( url, params, methods ) {
     var defaults = {
       update: { method: 'put', isArray: false },
       create: { method: 'post' }
     };    
     methods = angular.extend( defaults, methods );
     var resource = $resource( url, params, methods );
     resource.prototype.$save = function() {
       if ( !this.id ) {
         return this.$create();
       }
       else {
         return this.$update();
       }
     };
     return resource;
   };
 }]);

Getting records will be done in several different controllers in the same way:
Resource.query(
  function(resource){
    $scope.items=resource;
  },
  function(resource){
    msgBox.notify ("Ошибка получения данных");// msgBox сервис с контроллером и директивой
 });

Tell me, please, is it possible to somehow optimize code reuse in different controllers?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question