Test Setup Failed
Push — master ( ae6bbc...554531 )
by
unknown
09:25 queued 04:41
created

  B

Complexity

Conditions 1
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 5
dl 0
loc 28
rs 8.8571
c 1
b 0
f 0
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
B controller.js ➔ ... ➔ $.when.then 0 22 6
1
define(function(require) {
2
    'use strict';
3
4
    var $ = require('jquery');
5
    var _ = require('underscore');
6
    var Chaplin = require('chaplin');
7
    var BaseView = require('oroui/js/app/views/base/view');
8
    var BaseController;
9
    var reuses;
10
    var promiseLoads;
11
12
    BaseController = Chaplin.Controller.extend({
13
        /**
14
         * Handles before-action activity
15
         *  - initializes cache (on first route)
16
         *  - when all compositions are ready to reuse, resolves the deferred to execute action
17
         *
18
         * @override
19
         */
20
        beforeAction: function(params, route, options) {
0 ignored issues
show
Unused Code introduced by
The parameter options is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
21
            BaseController.__super__.beforeAction.apply(this, arguments);
22
23
            var self = this;
24
25
            return $.when.apply($, promiseLoads).then(function() {
26
                var i;
27
                var $el;
28
                // if it's first time route
29
                if (!route.previous) {
30
                    // initializes page cache
31
                    self.cache.init(route);
32
                }
33
                // compose global instances
34
                for (i = 0; i < reuses.length; i += 1) {
35
                    if (
36
                        _.isObject(reuses[i][2]) && reuses[i][2].el &&
37
                        !(($el = BaseView.resolveElOption(reuses[i][2].el)) && $el.length)
38
                    ) {
39
                        // temporary fix for BAP-16640 issue
40
                        // solid fix will be applied in OPA-92
41
                        // skip initialization if the element does not exist
42
                        continue;
43
                    }
44
                    self.reuse.apply(self, reuses[i]);
45
                }
46
            });
47
        },
48
49
        /**
50
         * Combines full URL for the route
51
         *
52
         * @param {Object} route
53
         * @returns {string}
54
         * @private
55
         */
56
        _combineRouteUrl: function(route) {
57
            var url;
58
            url = Chaplin.mediator.execute('combineFullUrl', route.path, route.query);
59
            return url;
60
        },
61
62
        /**
63
         * Cache accessor
64
         */
65
        cache: {
66
            /**
67
             * Executes 'init' handler for pageCache manager
68
             *
69
             * @param {Object} route
70
             */
71
            init: function(route) {
72
                var path = route.path;
73
                var query = route.query;
74
                var userName = Chaplin.mediator.execute('retrieveOption', 'userName') || false;
75
                Chaplin.mediator.execute({
76
                    name: 'pageCache:init',
77
                    silent: true
78
                }, path, query, userName);
79
            },
80
81
            /**
82
             * Executes 'get' handler for pageCache manager
83
             *
84
             * @param {string=} path
85
             * @returns {Object|undefined}
86
             */
87
            get: function(path) {
88
                return Chaplin.mediator.execute({
89
                    name: 'pageCache:get',
90
                    silent: true
91
                }, path);
92
            }
93
        }
94
    });
95
96
    reuses = [];
97
    promiseLoads = [
98
        // add DOM Ready promise to loads promises,
99
        // in order to prevent route action execution before the page is ready
100
        $.ready
101
    ];
102
103
    /**
104
     * Collects compositions to reuse before controller action
105
     * @static
106
     */
107
    BaseController.addToReuse = function() {
108
        var args = Array.prototype.slice.call(arguments, 0);
109
        reuses.push(args);
110
    };
111
112
    /**
113
     * Wrapper over "require" method.
114
     *  - loads modules, executes callback and resolves deferred object
115
     *
116
     * @param {Array} modules
117
     * @param {function(...[*])} initCallback
118
     * @returns {jQuery.Deferred}
119
     * @static
120
     */
121
    BaseController.loadBeforeAction = function(modules, initCallback) {
122
        var deferredLoad = $.Deferred();
123
        promiseLoads.push(deferredLoad.promise());
124
        var callback = function() {
125
            var args;
126
            args = Array.prototype.slice.call(arguments, 0);
127
            initCallback.apply(null, args);
128
            deferredLoad.resolve();
129
        };
130
        require(modules, callback);
131
    };
132
133
    return BaseController;
134
});
135