-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathComponent.js
More file actions
64 lines (59 loc) · 1.98 KB
/
Copy pathComponent.js
File metadata and controls
64 lines (59 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Creates a UI component class.
*
* @param {object} methods An object containing methods by name.
* @return {Function} Returns an instantiable function.
* @example
* // Create a component class:
* var CounterButtonView = Component({
* init: function(initialCount) {
* this.count = initialCount || 0;
* },
* render: function() {
* if (!this.el) {
* this.el = $('<button></button>');
* this.el.on('click', this.increment);
* }
* this.el.text(this.count);
* return this.el;
* },
* increment: function() {
* this.count += 1;
* this.render();
* }
* });
*
* // Usage:
* var view = new CounterButtonView(1);
* $('body').append(view.render());
*/
function Component(methods) {
// READ ME:
// This function creates a "class" by attaching the methods
// given in the `methods` parameter to a function's prototype.
// Read more about how prototypes work here:
// https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes
// ensure the `methods` object is useful
if (typeof methods !== 'object' || methods === null) {
throw new Error('Invalid methods object provided.');
}
// The class constructor that we'll return later.
// We use this function's prototype to attach methods to.
var Constructor = function() {
// auto-bind the function scope to simplify event binding
for (var methodName in methods) {
this[methodName] = this[methodName].bind(this);
}
// call the constructor
this.init.apply(this, arguments);
};
// make sure we have all required methods
methods.init = methods.init || function() {};
methods.render = methods.render || function() {};
// attach methods to constructor function prototype
for (var methodName in methods) {
Constructor.prototype[methodName] = methods[methodName];
}
// return the class constructor
return Constructor;
}