Completed
Push — master ( fb754a...2a3626 )
by Antonio
03:49
created

clone.js ➔ define   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 39
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 65
rs 8.9439

4 Functions

Rating   Name   Duplication   Size   Complexity  
A clone.js ➔ ... ➔ BaseForm.extend.getIdentifier 0 3 1
A clone.js ➔ ... ➔ BaseForm.extend.initialize 0 4 1
A clone.js ➔ ... ➔ BaseForm.extend.render 0 12 2
A clone.js ➔ ... ➔ BaseForm.extend.openModal 0 23 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
'use strict';
2
3
/**
4
 * Clone product extension
5
 */
6
define([
7
    'jquery',
8
    'pim/form',
9
    'underscore',
10
    'oro/translator',
11
    'backbone',
12
    'pim/form-builder',
13
    'flagbit/template/product/clone-button',
14
    ],
15
    function (
16
        $,
17
        BaseForm,
18
        _,
19
        __,
20
        Backbone,
21
        FormBuilder,
22
        template
23
    ) {
24
        return BaseForm.extend({
25
            template: _.template(template),
26
27
            initialize(config) {
28
                this.config = config.config;
29
                BaseForm.prototype.initialize.apply(this, arguments);
30
            },
31
32
            openModal() {
33
                return FormBuilder.build(this.config.formName).then(modal => {
34
35
                    const rootModel = this.getRoot().model;
36
                    var productType, codeToClone;
37
                    if (rootModel.has('identifier')) {
38
                        productType = 'product';
39
                        codeToClone = rootModel.get('identifier')
40
                    } else {
41
                        productType = 'model';
42
                        codeToClone = rootModel.get('code')
43
                    }
44
45
                    const initialModalState = {
46
                        parent: rootModel.get('parent'),
47
                        values: {},
48
                        code_to_clone: codeToClone,
49
                        type: productType
50
                    };
51
                    modal.setData(initialModalState);
52
                    modal.open();
53
                });
54
            },
55
56
            /**
57
             * {@inheritdoc}
58
             */
59
            getIdentifier: function () {
60
                return this.getFormData().meta.id;
61
            },
62
63
            /**
64
             * {@inheritdoc}
65
             */
66
            render: function () {
67
                if (!this.getFormData().meta) {
68
                    return;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Are you sure this return statement is not missing an argument? If this is intended, consider adding an explicit undefined like return undefined;.
Loading history...
69
                }
70
71
                this.$el.html(this.template());
72
73
                $('.clone-product-button').on('click', () => {
74
                    this.openModal();
75
                });
76
                return this;
77
            }
78
        });
79
});
80