methuselah
methuselah

Reputation: 13216

Removing variable logic from controller, moving it to service

I want to move the logic for the variable replacers from the controller PhraseListCtrl in controllers.js to CategoryService in services.js.

The current setup (which works fine), is as follows:

In controller.js

    function PhraseListCtrl($scope, $stateParams, CategoryService) {

      $scope.categoryId = $stateParams.categoryId;
      $scope.category = $stateParams.category;

      CategoryService.getCategoryDetail($scope.categoryId).then(function(dataResponse) {
        $scope.categoryDetail = dataResponse.data;

        var replacers = {
          '{{group}}': '<a class="button button-small button-outline button-positive button-side-padding">group</a>',
          '{{attribute}}': '<a class="button button-small button-outline button-assertive button-side-padding">attribute</a>',
          '{{factor}}': '<a class="button button-small button-outline button-assertive button-side-padding">factor</a>',
          '{{person}}': '<a class="button  button-small button-outline button-positive button-side-padding">person</a>'
        }

        console.log(replacers);

        $scope.categoryDetail.forEach(function(e) {
          Object.keys(replacers).forEach(function(key) {
            e['phrase_filter'] = e['phrase_filter'].replace(new RegExp(key, 'g'), replacers[key]);
          })
        })

      });

    }

In services.js

function CategoryService($http) {

        this.getCategoryList = function () {

            return $http({
                method: 'GET',
                url: 'http://127.0.0.1:8000/api/category/',
            });

        }


        this.getCategoryDetail = function (categoryId) {

            return $http({
                method: 'GET',
                url: 'http://127.0.0.1:8000/api/category/' + categoryId,
            });

        }

    }

However, when I move the logic around a bit, it I can't seem to access the variable replacers anymore.

In controller.js

    function PhraseListCtrl($scope, $stateParams, CategoryService) {

      $scope.categoryId = $stateParams.categoryId;
      $scope.category = $stateParams.category;

      CategoryService.getCategoryDetail($scope.categoryId).then(function(dataResponse) {
        $scope.categoryDetail = dataResponse.data;

        var replacers = CategoryService.getCategoryFilter;

        console.log(replacers);

        $scope.categoryDetail.forEach(function(e) {
          Object.keys(replacers).forEach(function(key) {
            e['phrase_filter'] = e['phrase_filter'].replace(new RegExp(key, 'g'), replacers[key]);
          })
        })

      });

    }

In services.js

function CategoryService($http) {

    this.getCategoryList = function () {

        return $http({
            method: 'GET',
            url: 'http://127.0.0.1:8000/api/category/',
        });

    }


    this.getCategoryDetail = function (categoryId) {

        return $http({
            method: 'GET',
            url: 'http://127.0.0.1:8000/api/category/' + categoryId,
        });

    }

    this.getCategoryFilter = function () {

        var replacers = [{
            '{{group}}' : '<a class="button button-small button-outline button-positive button-side-padding">group</a>',
            '{{attribute}}' : '<a class="button button-small button-outline button-assertive button-side-padding">attribute</a>',
            '{{factor}}' : '<a class="button button-small button-outline button-assertive button-side-padding">factor</a>',
            '{{person}}' : '<a class="button  button-small button-outline button-positive button-side-padding">person</a>'
        }]

        return replacers;

    }

}

What am I doing wrong?

Upvotes: 0

Views: 35

Answers (1)

k4l4m
k4l4m

Reputation: 453

You are getting the reference for the function in this line:

var replacers = CategoryService.getCategoryFilter;

You should call the function like this

var replacers = CategoryService.getCategoryFilter();

That should do it.

Edit

Also it happens that you are creating an array for the replacers in the service, but you want to access an object.

Upvotes: 1

Related Questions