public/js/auth/fasterjs.js   A
last analyzed

Complexity

Total Complexity 13
Complexity/F 1.18

Size

Lines of Code 138
Function Count 11

Duplication

Duplicated Lines 138
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 13
eloc 86
dl 138
loc 138
rs 10
c 0
b 0
f 0
cc 0
nc 1024
mnd 1
bc 13
fnc 11
bpm 1.1818
cpm 1.1818
noi 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
$(function(){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
    /**
4
     *  Submit forms with AJAX
5
     */
6
    $("body").delegate("form[data-role='ajax-request']", "submit", function(event)
7
    {
8
        event.preventDefault();
9
10
        var formObject = $(this);
11
12
        formObject.find("input").attr("readonly", "readonly");
13
        formObject.find("select").attr("readonly", "readonly");
14
        formObject.find("button[type='submit']").attr("disabled", "disabled");
15
16
        var url  = $(this).attr('action');
17
        var type = $(this).attr('method');
18
        var box  = $(this).attr('data-response');
19
        var data = $(this).attr('data-object');
20
21
        var call = eval($(this).attr('data-callback')) || {};
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
22
23
        call.complete = call.complete || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
24
        call.success  = call.success || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
25
        call.before   = call.before  || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
26
        call.error    = call.error   || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
27
28
        var form_data = $(this).serializeArray();
29
30
        var parsed = eval(data);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
31
32
        for (var i in parsed)
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
33
        {
34
            form_data.push({ name: i, value: parsed[i] });
35
        }
36
37
        $.ajax({
38
            url: url,
39
            type: type,
40
            data: form_data,
41
            beforeSend: function() {
42
                var loader = "<div class='loader-container'><div class='loader'><span></span><span></span><span></span><span></span><span></span></div></div>";
43
                $(box).html(loader);
44
                call.before();
45
            },
46
            error: function(jqXHR, textStatus, errorThrown)
47
            {
48
                $(box).html("Error processing request!.<br />");
49
50
                var e = {};
51
                e.jqXHR = jqXHR;
52
                console.info(jqXHR);
53
                e.textStatus = textStatus;
54
                e.errorThrown = errorThrown;
55
56
                call.error(e);
57
58
                $(box).append("Error <strong>" + jqXHR.status + "</strong> - " + jqXHR.statusText + "<br />");
59
                $(box).append("Status: " + textStatus + "<br />");
60
                $(box).append("readyState: " + jqXHR.readyState + "<br />");
61
                $(box).append(jqXHR.responseText + "<br /><br />");
62
            },
63
            success: function(data)
64
            {
65
                $(box).html(data);
66
                call.success(data);
67
            },
68
            complete: function(data)
69
            {
70
                formObject.find("input").removeAttr("readonly");
71
                formObject.find("select").removeAttr("readonly");
72
                formObject.find("button[type='submit']").removeAttr("disabled");
73
                call.success(data);
74
            }
75
        });
76
    });
77
78
    /**
79
     *  General AJAX request
80
     */
81
    $("body").delegate(":not(form)[data-role='ajax-request']", "click", function(event)
82
    {
83
        var url  = $(this).attr('data-url');
84
        var type = $(this).attr('data-type');
85
        var box  = $(this).attr('data-response');
86
        var data = $(this).attr('data-object');
87
        var frm  = $(this).attr('data-form');
88
89
        var call = eval($(this).attr('data-callback')) || {};
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
90
91
        call.complete = call.complete   || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
92
        call.success  = call.success || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
93
        call.before   = call.before  || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
94
        call.error    = call.error   || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
95
96
        var form_data = $(frm).serializeArray();
97
98
        var parsed = eval(data);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
99
100
        for (var i in parsed)
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
101
        {
102
            form_data.push({ name: i, value: parsed[i] });
103
        }
104
105
        $.ajax({
106
            url: url,
107
            type: type,
108
            data: form_data,
109
            beforeSend: function() {
110
                var loader = "<div class='loader-container'><div class='loader'><span></span><span></span><span></span><span></span><span></span></div></div>";
111
                $(box).html(loader);
112
                call.before();
113
            },
114
            error: function(jqXHR, textStatus, errorThrown)
115
            {
116
                event.preventDefault();
117
118
                $(box).html("Error processing request!. " + errorThrown);
119
120
                var e = {};
121
                e.jqXHR = jqXHR;
122
                e.textStatus = textStatus;
123
                e.errorThrown = errorThrown;
124
125
                call.error(e);
126
            },
127
            success: function(data)
128
            {
129
                $(box).html(data);
130
                call.success(data);
131
            },
132
            complete: function()
133
            {
134
                call.complete();
135
            }
136
        });
137
    });
138
});