D
D
Dmitry Arushanov2015-01-08 17:18:59
Angular
Dmitry Arushanov, 2015-01-08 17:18:59

How to understand given entry in Angular?

Good day to all. I installed a theme written in AngularJS as well.
A couple of questions arose during the analysis of the topic. Here is the original code for one of the controllers.

(function() {
    'use strict';
    angular.module('staff.controller', []).controller('StaffCtrl', [
        '$scope', '$filter', function($scope, $filter) {
            var init;
            $scope.stores = [
                {
                    name: 'Nijiya Market',
                    price: '$$',
                    sales: 292,
                    rating: 4.0
                },  ... {
                    name: 'House of Bagels',
                    price: '$',
                    sales: 82,
                    rating: 4.4
                }
            ];
            $scope.searchKeywords = '';
            $scope.filteredStores = [];
            $scope.row = '';
            $scope.select = function(page) {
                var end, start;
                start = (page - 1) * $scope.numPerPage;
                end = start + $scope.numPerPage;
                return $scope.currentPageStores = $scope.filteredStores.slice(start, end);
            };
            $scope.onFilterChange = function() {
                $scope.select(1);
                $scope.currentPage = 1;
                return $scope.row = '';
            };
            $scope.onNumPerPageChange = function() {
                $scope.select(1);
                return $scope.currentPage = 1;
            };
            $scope.onOrderChange = function() {
                $scope.select(1);
                return $scope.currentPage = 1;
            };
            $scope.search = function() {
                $scope.filteredStores = $filter('filter')($scope.stores, $scope.searchKeywords);
                return $scope.onFilterChange();
            };
            $scope.order = function(rowName) {
                if ($scope.row === rowName) {
                    return;
                }
                $scope.row = rowName;
                $scope.filteredStores = $filter('orderBy')($scope.stores, rowName);
                return $scope.onOrderChange();
            };
            $scope.numPerPageOpt = [3, 5, 10, 20];
            $scope.numPerPage = $scope.numPerPageOpt[2];
            $scope.currentPage = 1;
            $scope.currentPageStores = [];
            init = function() {
                $scope.search();
                return $scope.select($scope.currentPage);
            };
             return init();
        }
    ]);

}).call(this);

For the first time I meet the entry return init(); In the controller. Theoretically I understand why it is, and how it is. But I can't even explain it to myself. Because replacing return init() with a simple init() without return - everything works the same. Why return here.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-01-08
Protko @Fesor

Shit code and all. Don't sweat it. The author of this code wrote too much in jQuery.

V
Vladislav, 2015-01-08
Kozulya @5angel

I have been working with Angular for several years, but I am just as intrigued by the motivation of the author of this code.
However, there is one guess. He seems to really like it when functions return a value (note, EVERY function returns something, even if it is not necessary at all), so it makes sense that both init and the controller function itself should return something.
In general, the organization of the code in this controller leaves much to be desired.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question