Conditions | 1 |
Paths | 1 |
Total Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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({ |
||
|
|||
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 |
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.