Issues (121)

src/js/mivhak.component.js (4 issues)

1
/**
2
 * The list of registered components.
3
 * 
4
 * @type Array
5
 */
6
Mivhak.components = [];
0 ignored issues
show
The variable Mivhak seems to be never declared. If this is a global, consider adding a /** global: Mivhak */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
7
8
/**
9
 * Register a new component
10
 * 
11
 * @param {string} name The components name
12
 * @param {Object} options A list of component properties
13
 */
14
Mivhak.component = function(name, options)
15
{
16
    Mivhak.components[name] = options;
0 ignored issues
show
The variable Mivhak seems to be never declared. If this is a global, consider adding a /** global: Mivhak */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
17
};
18
19
/**
20
 * Render a new component
21
 * 
22
 * TODO: move this into a seperate library
23
 * 
24
 * @param {string} name The components name
25
 * @param {Object} props A list of component properties. 
26
 * This overrides the component's initial property values.
27
 */
28
Mivhak.render = function(name, props)
29
{
30
    var component = $.extend(true, {}, Mivhak.components[name]);
0 ignored issues
show
The variable Mivhak seems to be never declared. If this is a global, consider adding a /** global: Mivhak */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
    var el = {};
32
    
33
    // Create the element from the template
34
    el.$el = $(component.template);
35
    
36
    // Create all methods
37
    $.each(component.methods, function(name, method){
38
        el[name] = function() {return method.apply(el,arguments);};
39
    });
40
    
41
    // Set properties
42
    $.each(component.props, function(name, prop){
43
        el[name] = (typeof props !== 'undefined' && props.hasOwnProperty(name) ? props[name] : prop);
44
    });
45
    
46
    // Bind events
47
    $.each(component.events, function(name, method){
48
        el.$el.on(name, function() {return method.apply(el,arguments);});
49
    });
50
    
51
    // Call the 'created' function if exists
52
    if(component.hasOwnProperty('created')) component.created.call(el);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
    
54
    return el;
55
};