Test Setup Failed
Push — master ( 982d9a...a2862f )
by
unknown
03:40
created

customer-component.js ➔ define   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 83
rs 8.7468
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A customer-component.js ➔ ... ➔ BaseComponent.extend.initialize 0 20 1
A customer-component.js ➔ ... ➔ BaseComponent.extend.dispose 0 9 2
A customer-component.js ➔ ... ➔ BaseComponent.extend.onCustomerDialogSelect 0 5 1
A customer-component.js ➔ ... ➔ BaseComponent.extend.onCustomerDialogInit 0 15 2
A customer-component.js ➔ ... ➔ CustomerComponent 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
define(function(require) {
2
    'use strict';
3
4
    var _ = require('underscore');
5
    var tools = require('oroui/js/tools');
6
    var mediator = require('oroui/js/mediator');
7
    var BaseComponent = require('oroui/js/app/components/base/component');
8
    var CreateCustomerView = require('orosales/js/app/views/create-customer-view');
9
    var CustomerComponent = BaseComponent.extend({
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable CustomerComponent already seems to be declared on line 21. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
10
        views: [],
11
        $el: null,
12
        inputSelector: null,
13
        requiredOptions: [
14
            'inputSelector',
15
            'customerSelector'
16
        ],
17
18
        /**
19
         * @inheritDoc
20
         */
21
        constructor: function CustomerComponent() {
22
            CustomerComponent.__super__.constructor.apply(this, arguments);
23
        },
24
25
        /**
26
         * @inheritDoc
27
         */
28
        initialize: function(options) {
29
            _.each(this.requiredOptions, function(optionName) {
30
                if (!_.has(options, optionName)) {
31
                    throw new Error('Required option "' + optionName + '" not found.');
32
                }
33
            });
34
            this.inputSelector = options.inputSelector;
35
            this.$el = options._sourceElement;
36
37
            mediator.on('customer-dialog:select', this.onCustomerDialogSelect, this);
38
            mediator.on('widget_registration:customer-dialog', this.onCustomerDialogInit, this);
39
40
            var $customers = this.$el.find(options.customerSelector);
41
            _.each($customers, function(customer) {
42
                this.views.push(new CreateCustomerView({
43
                    el: customer,
44
                    inputSelector: this.inputSelector
45
                }));
46
            }, this);
47
        },
48
49
        onCustomerDialogInit: function(widget) {
50
            var routeParams = this.$el.find(this.inputSelector).data('select2_query_additional_params') || {};
51
            widget.options.routeParams = routeParams;
52
53
            var widgetUrl = widget.options.url;
54
            var widgetUrlRoot = widgetUrl.substring(0, widgetUrl.indexOf('?'));
55
            var widgetUrlParts = tools.unpackFromQueryString(
56
                widgetUrl.substring(widgetUrl.indexOf('?'), widgetUrl.length)
57
            );
58
            if (!_.isEmpty(routeParams)) {
59
                routeParams = _.extend({}, widgetUrlParts, {params: routeParams}, routeParams);
60
                widgetUrl = widgetUrlRoot || widgetUrl + '?' + tools.packToQueryString(routeParams);
61
                widget.options.url = widgetUrl;
62
            }
63
        },
64
65
        onCustomerDialogSelect: function(id) {
66
            var $input = this.$el.find(this.inputSelector);
67
            $input.inputWidget('val', id, true);
68
            $input.inputWidget('focus');
69
        },
70
71
        dispose: function() {
72
            if (this.disposed) {
73
                return;
74
            }
75
76
            mediator.off('customer-dialog:select', this.onCustomerSelect, this);
77
78
            CustomerComponent.__super__.dispose.apply(this, arguments);
79
        }
80
    });
81
82
    return CustomerComponent;
83
});
84