-
Notifications
You must be signed in to change notification settings - Fork 0
/
osteoporosis.js
executable file
·160 lines (135 loc) · 3.56 KB
/
osteoporosis.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*! Osteoporosis.js v0.0.2 By TAKANASHI Ginpei */
/* https://github.com/ginpei/Osteoporosis.js */
var Osteoporosis = (function() {
var O = {};
// make codes more short when minimized
var S_PROTOTYPE = 'prototype';
var S_EXTEND = 'extend';
var S_TRIGGER = 'trigger';
var S_ATTRIBUTES = 'attributes';
var S__LISTENERS = '_listeners';
var slice = [].slice;
var extend = (typeof _ === 'undefined' ? $[S_EXTEND] : _[S_EXTEND]);
var noop = function() { };
// ----------------------------------------------------------------
O[S_EXTEND] = function(prototype, statics) {
function Child(attributes) {
this.__osteoporosis__(attributes);
this.initialize(attributes);
}
Child[S_EXTEND] = O[S_EXTEND];
extend(Child[S_PROTOTYPE], this[S_PROTOTYPE], prototype);
extend(Child, statics);
return Child;
};
// ----------------------------------------------------------------
/**
* Event
*/
var eventPrototype = O.eventPrototype = {
/**
* Binds `listener` to this object as a callback function.
* FYI: `off()` is not provided.
* @param {String} type
* @param {Function} listener
*/
on: function(type, listener) {
var allListeners = this[S__LISTENERS];
if (!allListeners) {
allListeners = this[S__LISTENERS] = {};
}
var listeners = allListeners[type];
if (!listeners) {
listeners = allListeners[type] = [];
}
listeners.push(listener);
},
/**
* Fires an event named `type`.
* @param {String} type
*/
trigger: function(type) {
var allListeners = this[S__LISTENERS];
if (allListeners && allListeners[type]) {
var args = slice.call(arguments, 1);
allListeners[type].forEach(function(listener) {
listener.apply(null, args);
});
}
}
};
// ----------------------------------------------------------------
/**
* Model
*/
O.Model = function Model() {}
O.Model[S_EXTEND] = O[S_EXTEND];
extend(O.Model[S_PROTOTYPE], {
/**
* The constructor for model.
* @param {Object} attributes Key-value pairs to be set.
*/
__osteoporosis__: function(attributes) {
this[S_ATTRIBUTES] = {};
return this.set(attributes);
},
initialize: noop,
/**
* Sets a storage value.
* @param {Object} attributes Pairs of keys and values to be stored.
*/
set: function(attributes) {
var storage = this[S_ATTRIBUTES];
for (var key in attributes) {
var value = attributes[key];
var lastValue = storage[key];
if (value !== lastValue) {
storage[key] = value;
this[S_TRIGGER]('change:'+key, this, value);
this[S_TRIGGER]('change', this);
}
}
return this;
},
/**
* Gives a storage value.
* @param {String} key Name of the storage value.
* @returns {Object} The content of storaged value.
*/
get: function(key) {
return this[S_ATTRIBUTES][key];
},
// event methods
on: eventPrototype.on,
trigger: eventPrototype[S_TRIGGER]
});
// ----------------------------------------------------------------
/**
* View
*/
O.View = function View() {};
O.View[S_EXTEND] = O[S_EXTEND];
extend(O.View[S_PROTOTYPE], {
/**
* The constructor for view.
* @param {Object} options Any options within `el`.
*/
__osteoporosis__: function(options) {
options = options || {};
this.$el = $(options.el || document);
},
initialize: noop,
/**
* Finds element(s) under own element by specified selector.
* @param {String} selector
* @returns {Element}
*/
$: function(selector) {
return this.$el.find(selector);
},
// event methods
on: eventPrototype.on,
trigger: eventPrototype[S_TRIGGER]
});
return O;
})();