R
R
Robert2016-08-11 13:10:18
JavaScript
Robert, 2016-08-11 13:10:18

Is it good practice to initialize many variables from REST API in angular?

Here is a case:
There is a seller profile, on which the buyer can leave reviews only when he has made some order with him.
There is a Rest API in which, for a specific request, you can get a [boolean] response, whether it can or not.
How to correctly initialize such a variable in Angular, and when?
I always did ng-init, and already in this function I initialized everything that was needed. But some guys said it was bad practice.
So how do you need it?

angular.module('deserts').controller 'ReviewsController', [
  '$scope',
  '$http',
  'ReviewDataService',
  ($scope, $http, ReviewDataService) ->
    $scope.data = {reviews: {published: false}}

    initReviewsCounters = ->
      ReviewDataService.getReviewsPositive($scope.cId).then (d) ->
        $scope.reviewsPositiveCount = d.length
      ReviewDataService.getReviewsNegative($scope.cId).then (d) ->
        $scope.reviewsNegativeCount = d.length

    initUserReviewStatus = (confectionerId, userId) ->
      ReviewDataService.userCanReview(сId, userId).then (d) ->
        $scope.userCanReview = d

    $scope.init = (cId, userId) ->
      $scope.reviewMark = ''
      $scope.reviewType = 'all'
      $scope.cId = cId
      $scope.userId = userId
      initUserReviewStatus($scope.cId, $scope.userId)
      initReviewsCounters()
      
## code ###
]

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