handlebone 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +16 -0
- data/Guardfile +18 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +58 -0
- data/Rakefile +67 -0
- data/VERSION +1 -0
- data/generators/backbone_view/backbone_view_generator.rb +9 -0
- data/generators/backbone_view/templates/view.coffee.erb +2 -0
- data/generators/backbone_view/templates/view_template.html.handlebars +2 -0
- data/lib/assets/javascripts/handlebars_helpers.coffee +46 -0
- data/lib/assets/javascripts/handlebars_view.coffee +26 -0
- data/lib/generators/backbone/USAGE +8 -0
- data/lib/generators/backbone/backbone_generator.rb +6 -0
- data/lib/handlebone.rb +5 -0
- data/lib/handlebone/engine.rb +9 -0
- data/lib/handlebone/helper.rb +13 -0
- data/rakelib/coffeescript.rake +30 -0
- data/spec/javascripts/fixtures/child_view.handlebars.html +4 -0
- data/spec/javascripts/fixtures/multiple_child_parent_view.handlebars.html +7 -0
- data/spec/javascripts/fixtures/parent_view.handlebars.html +6 -0
- data/spec/javascripts/fixtures/simple_view.handlebars.html +4 -0
- data/spec/javascripts/handlebars_view_spec.coffee +55 -0
- data/spec/javascripts/helpers/SpecHelper.js +9 -0
- data/spec/javascripts/helpers/loadTemplateHelper.js +17 -0
- data/spec/javascripts/jasmine-jquery-1.1.3.js +205 -0
- data/spec/javascripts/jquery.js +167 -0
- data/spec/javascripts/support/jasmine.yml +78 -0
- data/spec/javascripts/support/jasmine_config.rb +23 -0
- data/spec/javascripts/support/jasmine_runner.rb +32 -0
- data/test/helper.rb +18 -0
- data/test/test_backbone-handlebars.rb +7 -0
- data/vendor/assets/javascripts/backbone.js +1066 -0
- data/vendor/assets/javascripts/handlebars.js +1417 -0
- data/vendor/assets/javascripts/underscore.js +748 -0
- metadata +164 -0
@@ -0,0 +1,748 @@
|
|
1
|
+
// Underscore.js 1.1.3
|
2
|
+
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
|
3
|
+
// Underscore is freely distributable under the MIT license.
|
4
|
+
// Portions of Underscore are inspired or borrowed from Prototype,
|
5
|
+
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
6
|
+
// For all details and documentation:
|
7
|
+
// http://documentcloud.github.com/underscore
|
8
|
+
|
9
|
+
(function() {
|
10
|
+
|
11
|
+
// Baseline setup
|
12
|
+
// --------------
|
13
|
+
|
14
|
+
// Establish the root object, `window` in the browser, or `global` on the server.
|
15
|
+
var root = this;
|
16
|
+
|
17
|
+
// Save the previous value of the `_` variable.
|
18
|
+
var previousUnderscore = root._;
|
19
|
+
|
20
|
+
// Establish the object that gets returned to break out of a loop iteration.
|
21
|
+
var breaker = {};
|
22
|
+
|
23
|
+
// Save bytes in the minified (but not gzipped) version:
|
24
|
+
var ArrayProto = Array.prototype, ObjProto = Object.prototype;
|
25
|
+
|
26
|
+
// Create quick reference variables for speed access to core prototypes.
|
27
|
+
var slice = ArrayProto.slice,
|
28
|
+
unshift = ArrayProto.unshift,
|
29
|
+
toString = ObjProto.toString,
|
30
|
+
hasOwnProperty = ObjProto.hasOwnProperty;
|
31
|
+
|
32
|
+
// All **ECMAScript 5** native function implementations that we hope to use
|
33
|
+
// are declared here.
|
34
|
+
var
|
35
|
+
nativeForEach = ArrayProto.forEach,
|
36
|
+
nativeMap = ArrayProto.map,
|
37
|
+
nativeReduce = ArrayProto.reduce,
|
38
|
+
nativeReduceRight = ArrayProto.reduceRight,
|
39
|
+
nativeFilter = ArrayProto.filter,
|
40
|
+
nativeEvery = ArrayProto.every,
|
41
|
+
nativeSome = ArrayProto.some,
|
42
|
+
nativeIndexOf = ArrayProto.indexOf,
|
43
|
+
nativeLastIndexOf = ArrayProto.lastIndexOf,
|
44
|
+
nativeIsArray = Array.isArray,
|
45
|
+
nativeKeys = Object.keys;
|
46
|
+
|
47
|
+
// Create a safe reference to the Underscore object for use below.
|
48
|
+
var _ = function(obj) { return new wrapper(obj); };
|
49
|
+
|
50
|
+
// Export the Underscore object for **CommonJS**, with backwards-compatibility
|
51
|
+
// for the old `require()` API. If we're not in CommonJS, add `_` to the
|
52
|
+
// global object.
|
53
|
+
if (typeof module !== 'undefined' && module.exports) {
|
54
|
+
module.exports = _;
|
55
|
+
_._ = _;
|
56
|
+
} else {
|
57
|
+
root._ = _;
|
58
|
+
}
|
59
|
+
|
60
|
+
// Current version.
|
61
|
+
_.VERSION = '1.1.3';
|
62
|
+
|
63
|
+
// Collection Functions
|
64
|
+
// --------------------
|
65
|
+
|
66
|
+
// The cornerstone, an `each` implementation, aka `forEach`.
|
67
|
+
// Handles objects implementing `forEach`, arrays, and raw objects.
|
68
|
+
// Delegates to **ECMAScript 5**'s native `forEach` if available.
|
69
|
+
var each = _.each = _.forEach = function(obj, iterator, context) {
|
70
|
+
var value;
|
71
|
+
if (nativeForEach && obj.forEach === nativeForEach) {
|
72
|
+
obj.forEach(iterator, context);
|
73
|
+
} else if (_.isNumber(obj.length)) {
|
74
|
+
for (var i = 0, l = obj.length; i < l; i++) {
|
75
|
+
if (iterator.call(context, obj[i], i, obj) === breaker) return;
|
76
|
+
}
|
77
|
+
} else {
|
78
|
+
for (var key in obj) {
|
79
|
+
if (hasOwnProperty.call(obj, key)) {
|
80
|
+
if (iterator.call(context, obj[key], key, obj) === breaker) return;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
};
|
85
|
+
|
86
|
+
// Return the results of applying the iterator to each element.
|
87
|
+
// Delegates to **ECMAScript 5**'s native `map` if available.
|
88
|
+
_.map = function(obj, iterator, context) {
|
89
|
+
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
|
90
|
+
var results = [];
|
91
|
+
each(obj, function(value, index, list) {
|
92
|
+
results[results.length] = iterator.call(context, value, index, list);
|
93
|
+
});
|
94
|
+
return results;
|
95
|
+
};
|
96
|
+
|
97
|
+
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
98
|
+
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
99
|
+
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
100
|
+
var initial = memo !== void 0;
|
101
|
+
if (nativeReduce && obj.reduce === nativeReduce) {
|
102
|
+
if (context) iterator = _.bind(iterator, context);
|
103
|
+
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
104
|
+
}
|
105
|
+
each(obj, function(value, index, list) {
|
106
|
+
if (!initial && index === 0) {
|
107
|
+
memo = value;
|
108
|
+
} else {
|
109
|
+
memo = iterator.call(context, memo, value, index, list);
|
110
|
+
}
|
111
|
+
});
|
112
|
+
return memo;
|
113
|
+
};
|
114
|
+
|
115
|
+
// The right-associative version of reduce, also known as `foldr`.
|
116
|
+
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
|
117
|
+
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
118
|
+
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
119
|
+
if (context) iterator = _.bind(iterator, context);
|
120
|
+
return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
|
121
|
+
}
|
122
|
+
var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
|
123
|
+
return _.reduce(reversed, iterator, memo, context);
|
124
|
+
};
|
125
|
+
|
126
|
+
// Return the first value which passes a truth test. Aliased as `detect`.
|
127
|
+
_.find = _.detect = function(obj, iterator, context) {
|
128
|
+
var result;
|
129
|
+
any(obj, function(value, index, list) {
|
130
|
+
if (iterator.call(context, value, index, list)) {
|
131
|
+
result = value;
|
132
|
+
return true;
|
133
|
+
}
|
134
|
+
});
|
135
|
+
return result;
|
136
|
+
};
|
137
|
+
|
138
|
+
// Return all the elements that pass a truth test.
|
139
|
+
// Delegates to **ECMAScript 5**'s native `filter` if available.
|
140
|
+
// Aliased as `select`.
|
141
|
+
_.filter = _.select = function(obj, iterator, context) {
|
142
|
+
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
|
143
|
+
var results = [];
|
144
|
+
each(obj, function(value, index, list) {
|
145
|
+
if (iterator.call(context, value, index, list)) results[results.length] = value;
|
146
|
+
});
|
147
|
+
return results;
|
148
|
+
};
|
149
|
+
|
150
|
+
// Return all the elements for which a truth test fails.
|
151
|
+
_.reject = function(obj, iterator, context) {
|
152
|
+
var results = [];
|
153
|
+
each(obj, function(value, index, list) {
|
154
|
+
if (!iterator.call(context, value, index, list)) results[results.length] = value;
|
155
|
+
});
|
156
|
+
return results;
|
157
|
+
};
|
158
|
+
|
159
|
+
// Determine whether all of the elements match a truth test.
|
160
|
+
// Delegates to **ECMAScript 5**'s native `every` if available.
|
161
|
+
// Aliased as `all`.
|
162
|
+
_.every = _.all = function(obj, iterator, context) {
|
163
|
+
iterator = iterator || _.identity;
|
164
|
+
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
|
165
|
+
var result = true;
|
166
|
+
each(obj, function(value, index, list) {
|
167
|
+
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
|
168
|
+
});
|
169
|
+
return result;
|
170
|
+
};
|
171
|
+
|
172
|
+
// Determine if at least one element in the object matches a truth test.
|
173
|
+
// Delegates to **ECMAScript 5**'s native `some` if available.
|
174
|
+
// Aliased as `any`.
|
175
|
+
var any = _.some = _.any = function(obj, iterator, context) {
|
176
|
+
iterator = iterator || _.identity;
|
177
|
+
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
|
178
|
+
var result = false;
|
179
|
+
each(obj, function(value, index, list) {
|
180
|
+
if (result = iterator.call(context, value, index, list)) return breaker;
|
181
|
+
});
|
182
|
+
return result;
|
183
|
+
};
|
184
|
+
|
185
|
+
// Determine if a given value is included in the array or object using `===`.
|
186
|
+
// Aliased as `contains`.
|
187
|
+
_.include = _.contains = function(obj, target) {
|
188
|
+
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
|
189
|
+
var found = false;
|
190
|
+
any(obj, function(value) {
|
191
|
+
if (found = value === target) return true;
|
192
|
+
});
|
193
|
+
return found;
|
194
|
+
};
|
195
|
+
|
196
|
+
// Invoke a method (with arguments) on every item in a collection.
|
197
|
+
_.invoke = function(obj, method) {
|
198
|
+
var args = slice.call(arguments, 2);
|
199
|
+
return _.map(obj, function(value) {
|
200
|
+
return (method ? value[method] : value).apply(value, args);
|
201
|
+
});
|
202
|
+
};
|
203
|
+
|
204
|
+
// Convenience version of a common use case of `map`: fetching a property.
|
205
|
+
_.pluck = function(obj, key) {
|
206
|
+
return _.map(obj, function(value){ return value[key]; });
|
207
|
+
};
|
208
|
+
|
209
|
+
// Return the maximum element or (element-based computation).
|
210
|
+
_.max = function(obj, iterator, context) {
|
211
|
+
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
212
|
+
var result = {computed : -Infinity};
|
213
|
+
each(obj, function(value, index, list) {
|
214
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
215
|
+
computed >= result.computed && (result = {value : value, computed : computed});
|
216
|
+
});
|
217
|
+
return result.value;
|
218
|
+
};
|
219
|
+
|
220
|
+
// Return the minimum element (or element-based computation).
|
221
|
+
_.min = function(obj, iterator, context) {
|
222
|
+
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
223
|
+
var result = {computed : Infinity};
|
224
|
+
each(obj, function(value, index, list) {
|
225
|
+
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
226
|
+
computed < result.computed && (result = {value : value, computed : computed});
|
227
|
+
});
|
228
|
+
return result.value;
|
229
|
+
};
|
230
|
+
|
231
|
+
// Sort the object's values by a criterion produced by an iterator.
|
232
|
+
_.sortBy = function(obj, iterator, context) {
|
233
|
+
return _.pluck(_.map(obj, function(value, index, list) {
|
234
|
+
return {
|
235
|
+
value : value,
|
236
|
+
criteria : iterator.call(context, value, index, list)
|
237
|
+
};
|
238
|
+
}).sort(function(left, right) {
|
239
|
+
var a = left.criteria, b = right.criteria;
|
240
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
241
|
+
}), 'value');
|
242
|
+
};
|
243
|
+
|
244
|
+
// Use a comparator function to figure out at what index an object should
|
245
|
+
// be inserted so as to maintain order. Uses binary search.
|
246
|
+
_.sortedIndex = function(array, obj, iterator) {
|
247
|
+
iterator = iterator || _.identity;
|
248
|
+
var low = 0, high = array.length;
|
249
|
+
while (low < high) {
|
250
|
+
var mid = (low + high) >> 1;
|
251
|
+
iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
|
252
|
+
}
|
253
|
+
return low;
|
254
|
+
};
|
255
|
+
|
256
|
+
// Safely convert anything iterable into a real, live array.
|
257
|
+
_.toArray = function(iterable) {
|
258
|
+
if (!iterable) return [];
|
259
|
+
if (iterable.toArray) return iterable.toArray();
|
260
|
+
if (_.isArray(iterable)) return iterable;
|
261
|
+
if (_.isArguments(iterable)) return slice.call(iterable);
|
262
|
+
return _.values(iterable);
|
263
|
+
};
|
264
|
+
|
265
|
+
// Return the number of elements in an object.
|
266
|
+
_.size = function(obj) {
|
267
|
+
return _.toArray(obj).length;
|
268
|
+
};
|
269
|
+
|
270
|
+
// Array Functions
|
271
|
+
// ---------------
|
272
|
+
|
273
|
+
// Get the first element of an array. Passing **n** will return the first N
|
274
|
+
// values in the array. Aliased as `head`. The **guard** check allows it to work
|
275
|
+
// with `_.map`.
|
276
|
+
_.first = _.head = function(array, n, guard) {
|
277
|
+
return n && !guard ? slice.call(array, 0, n) : array[0];
|
278
|
+
};
|
279
|
+
|
280
|
+
// Returns everything but the first entry of the array. Aliased as `tail`.
|
281
|
+
// Especially useful on the arguments object. Passing an **index** will return
|
282
|
+
// the rest of the values in the array from that index onward. The **guard**
|
283
|
+
// check allows it to work with `_.map`.
|
284
|
+
_.rest = _.tail = function(array, index, guard) {
|
285
|
+
return slice.call(array, _.isUndefined(index) || guard ? 1 : index);
|
286
|
+
};
|
287
|
+
|
288
|
+
// Get the last element of an array.
|
289
|
+
_.last = function(array) {
|
290
|
+
return array[array.length - 1];
|
291
|
+
};
|
292
|
+
|
293
|
+
// Trim out all falsy values from an array.
|
294
|
+
_.compact = function(array) {
|
295
|
+
return _.filter(array, function(value){ return !!value; });
|
296
|
+
};
|
297
|
+
|
298
|
+
// Return a completely flattened version of an array.
|
299
|
+
_.flatten = function(array) {
|
300
|
+
return _.reduce(array, function(memo, value) {
|
301
|
+
if (_.isArray(value)) return memo.concat(_.flatten(value));
|
302
|
+
memo[memo.length] = value;
|
303
|
+
return memo;
|
304
|
+
}, []);
|
305
|
+
};
|
306
|
+
|
307
|
+
// Return a version of the array that does not contain the specified value(s).
|
308
|
+
_.without = function(array) {
|
309
|
+
var values = slice.call(arguments, 1);
|
310
|
+
return _.filter(array, function(value){ return !_.include(values, value); });
|
311
|
+
};
|
312
|
+
|
313
|
+
// Produce a duplicate-free version of the array. If the array has already
|
314
|
+
// been sorted, you have the option of using a faster algorithm.
|
315
|
+
// Aliased as `unique`.
|
316
|
+
_.uniq = _.unique = function(array, isSorted) {
|
317
|
+
return _.reduce(array, function(memo, el, i) {
|
318
|
+
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
|
319
|
+
return memo;
|
320
|
+
}, []);
|
321
|
+
};
|
322
|
+
|
323
|
+
// Produce an array that contains every item shared between all the
|
324
|
+
// passed-in arrays.
|
325
|
+
_.intersect = function(array) {
|
326
|
+
var rest = slice.call(arguments, 1);
|
327
|
+
return _.filter(_.uniq(array), function(item) {
|
328
|
+
return _.every(rest, function(other) {
|
329
|
+
return _.indexOf(other, item) >= 0;
|
330
|
+
});
|
331
|
+
});
|
332
|
+
};
|
333
|
+
|
334
|
+
// Zip together multiple lists into a single array -- elements that share
|
335
|
+
// an index go together.
|
336
|
+
_.zip = function() {
|
337
|
+
var args = slice.call(arguments);
|
338
|
+
var length = _.max(_.pluck(args, 'length'));
|
339
|
+
var results = new Array(length);
|
340
|
+
for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
|
341
|
+
return results;
|
342
|
+
};
|
343
|
+
|
344
|
+
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
|
345
|
+
// we need this function. Return the position of the first occurrence of an
|
346
|
+
// item in an array, or -1 if the item is not included in the array.
|
347
|
+
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
|
348
|
+
_.indexOf = function(array, item) {
|
349
|
+
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
|
350
|
+
for (var i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
|
351
|
+
return -1;
|
352
|
+
};
|
353
|
+
|
354
|
+
|
355
|
+
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
|
356
|
+
_.lastIndexOf = function(array, item) {
|
357
|
+
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
|
358
|
+
var i = array.length;
|
359
|
+
while (i--) if (array[i] === item) return i;
|
360
|
+
return -1;
|
361
|
+
};
|
362
|
+
|
363
|
+
// Generate an integer Array containing an arithmetic progression. A port of
|
364
|
+
// the native Python `range()` function. See
|
365
|
+
// [the Python documentation](http://docs.python.org/library/functions.html#range).
|
366
|
+
_.range = function(start, stop, step) {
|
367
|
+
var args = slice.call(arguments),
|
368
|
+
solo = args.length <= 1,
|
369
|
+
start = solo ? 0 : args[0],
|
370
|
+
stop = solo ? args[0] : args[1],
|
371
|
+
step = args[2] || 1,
|
372
|
+
len = Math.max(Math.ceil((stop - start) / step), 0),
|
373
|
+
idx = 0,
|
374
|
+
range = new Array(len);
|
375
|
+
while (idx < len) {
|
376
|
+
range[idx++] = start;
|
377
|
+
start += step;
|
378
|
+
}
|
379
|
+
return range;
|
380
|
+
};
|
381
|
+
|
382
|
+
// Function (ahem) Functions
|
383
|
+
// ------------------
|
384
|
+
|
385
|
+
// Create a function bound to a given object (assigning `this`, and arguments,
|
386
|
+
// optionally). Binding with arguments is also known as `curry`.
|
387
|
+
_.bind = function(func, obj) {
|
388
|
+
var args = slice.call(arguments, 2);
|
389
|
+
return function() {
|
390
|
+
return func.apply(obj || {}, args.concat(slice.call(arguments)));
|
391
|
+
};
|
392
|
+
};
|
393
|
+
|
394
|
+
// Bind all of an object's methods to that object. Useful for ensuring that
|
395
|
+
// all callbacks defined on an object belong to it.
|
396
|
+
_.bindAll = function(obj) {
|
397
|
+
var funcs = slice.call(arguments, 1);
|
398
|
+
if (funcs.length == 0) funcs = _.functions(obj);
|
399
|
+
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
|
400
|
+
return obj;
|
401
|
+
};
|
402
|
+
|
403
|
+
// Memoize an expensive function by storing its results.
|
404
|
+
_.memoize = function(func, hasher) {
|
405
|
+
var memo = {};
|
406
|
+
hasher = hasher || _.identity;
|
407
|
+
return function() {
|
408
|
+
var key = hasher.apply(this, arguments);
|
409
|
+
return key in memo ? memo[key] : (memo[key] = func.apply(this, arguments));
|
410
|
+
};
|
411
|
+
};
|
412
|
+
|
413
|
+
// Delays a function for the given number of milliseconds, and then calls
|
414
|
+
// it with the arguments supplied.
|
415
|
+
_.delay = function(func, wait) {
|
416
|
+
var args = slice.call(arguments, 2);
|
417
|
+
return setTimeout(function(){ return func.apply(func, args); }, wait);
|
418
|
+
};
|
419
|
+
|
420
|
+
// Defers a function, scheduling it to run after the current call stack has
|
421
|
+
// cleared.
|
422
|
+
_.defer = function(func) {
|
423
|
+
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
424
|
+
};
|
425
|
+
|
426
|
+
// Internal function used to implement `_.throttle` and `_.debounce`.
|
427
|
+
var limit = function(func, wait, debounce) {
|
428
|
+
var timeout;
|
429
|
+
return function() {
|
430
|
+
var context = this, args = arguments;
|
431
|
+
var throttler = function() {
|
432
|
+
timeout = null;
|
433
|
+
func.apply(context, args);
|
434
|
+
};
|
435
|
+
if (debounce) clearTimeout(timeout);
|
436
|
+
if (debounce || !timeout) timeout = setTimeout(throttler, wait);
|
437
|
+
};
|
438
|
+
};
|
439
|
+
|
440
|
+
// Returns a function, that, when invoked, will only be triggered at most once
|
441
|
+
// during a given window of time.
|
442
|
+
_.throttle = function(func, wait) {
|
443
|
+
return limit(func, wait, false);
|
444
|
+
};
|
445
|
+
|
446
|
+
// Returns a function, that, as long as it continues to be invoked, will not
|
447
|
+
// be triggered. The function will be called after it stops being called for
|
448
|
+
// N milliseconds.
|
449
|
+
_.debounce = function(func, wait) {
|
450
|
+
return limit(func, wait, true);
|
451
|
+
};
|
452
|
+
|
453
|
+
// Returns the first function passed as an argument to the second,
|
454
|
+
// allowing you to adjust arguments, run code before and after, and
|
455
|
+
// conditionally execute the original function.
|
456
|
+
_.wrap = function(func, wrapper) {
|
457
|
+
return function() {
|
458
|
+
var args = [func].concat(slice.call(arguments));
|
459
|
+
return wrapper.apply(wrapper, args);
|
460
|
+
};
|
461
|
+
};
|
462
|
+
|
463
|
+
// Returns a function that is the composition of a list of functions, each
|
464
|
+
// consuming the return value of the function that follows.
|
465
|
+
_.compose = function() {
|
466
|
+
var funcs = slice.call(arguments);
|
467
|
+
return function() {
|
468
|
+
var args = slice.call(arguments);
|
469
|
+
for (var i=funcs.length-1; i >= 0; i--) {
|
470
|
+
args = [funcs[i].apply(this, args)];
|
471
|
+
}
|
472
|
+
return args[0];
|
473
|
+
};
|
474
|
+
};
|
475
|
+
|
476
|
+
// Object Functions
|
477
|
+
// ----------------
|
478
|
+
|
479
|
+
// Retrieve the names of an object's properties.
|
480
|
+
// Delegates to **ECMAScript 5**'s native `Object.keys`
|
481
|
+
_.keys = nativeKeys || function(obj) {
|
482
|
+
if (_.isArray(obj)) return _.range(0, obj.length);
|
483
|
+
var keys = [];
|
484
|
+
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
|
485
|
+
return keys;
|
486
|
+
};
|
487
|
+
|
488
|
+
// Retrieve the values of an object's properties.
|
489
|
+
_.values = function(obj) {
|
490
|
+
return _.map(obj, _.identity);
|
491
|
+
};
|
492
|
+
|
493
|
+
// Return a sorted list of the function names available on the object.
|
494
|
+
// Aliased as `methods`
|
495
|
+
_.functions = _.methods = function(obj) {
|
496
|
+
return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
497
|
+
};
|
498
|
+
|
499
|
+
// Extend a given object with all the properties in passed-in object(s).
|
500
|
+
_.extend = function(obj) {
|
501
|
+
each(slice.call(arguments, 1), function(source) {
|
502
|
+
for (var prop in source) obj[prop] = source[prop];
|
503
|
+
});
|
504
|
+
return obj;
|
505
|
+
};
|
506
|
+
|
507
|
+
// Create a (shallow-cloned) duplicate of an object.
|
508
|
+
_.clone = function(obj) {
|
509
|
+
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
|
510
|
+
};
|
511
|
+
|
512
|
+
// Invokes interceptor with the obj, and then returns obj.
|
513
|
+
// The primary purpose of this method is to "tap into" a method chain, in
|
514
|
+
// order to perform operations on intermediate results within the chain.
|
515
|
+
_.tap = function(obj, interceptor) {
|
516
|
+
interceptor(obj);
|
517
|
+
return obj;
|
518
|
+
};
|
519
|
+
|
520
|
+
// Perform a deep comparison to check if two objects are equal.
|
521
|
+
_.isEqual = function(a, b) {
|
522
|
+
// Check object identity.
|
523
|
+
if (a === b) return true;
|
524
|
+
// Different types?
|
525
|
+
var atype = typeof(a), btype = typeof(b);
|
526
|
+
if (atype != btype) return false;
|
527
|
+
// Basic equality test (watch out for coercions).
|
528
|
+
if (a == b) return true;
|
529
|
+
// One is falsy and the other truthy.
|
530
|
+
if ((!a && b) || (a && !b)) return false;
|
531
|
+
// One of them implements an isEqual()?
|
532
|
+
if (a.isEqual) return a.isEqual(b);
|
533
|
+
// Check dates' integer values.
|
534
|
+
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
|
535
|
+
// Both are NaN?
|
536
|
+
if (_.isNaN(a) && _.isNaN(b)) return false;
|
537
|
+
// Compare regular expressions.
|
538
|
+
if (_.isRegExp(a) && _.isRegExp(b))
|
539
|
+
return a.source === b.source &&
|
540
|
+
a.global === b.global &&
|
541
|
+
a.ignoreCase === b.ignoreCase &&
|
542
|
+
a.multiline === b.multiline;
|
543
|
+
// If a is not an object by this point, we can't handle it.
|
544
|
+
if (atype !== 'object') return false;
|
545
|
+
// Check for different array lengths before comparing contents.
|
546
|
+
if (a.length && (a.length !== b.length)) return false;
|
547
|
+
// Nothing else worked, deep compare the contents.
|
548
|
+
var aKeys = _.keys(a), bKeys = _.keys(b);
|
549
|
+
// Different object sizes?
|
550
|
+
if (aKeys.length != bKeys.length) return false;
|
551
|
+
// Recursive comparison of contents.
|
552
|
+
for (var key in a) if (!(key in b) || !_.isEqual(a[key], b[key])) return false;
|
553
|
+
return true;
|
554
|
+
};
|
555
|
+
|
556
|
+
// Is a given array or object empty?
|
557
|
+
_.isEmpty = function(obj) {
|
558
|
+
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
|
559
|
+
for (var key in obj) if (hasOwnProperty.call(obj, key)) return false;
|
560
|
+
return true;
|
561
|
+
};
|
562
|
+
|
563
|
+
// Is a given value a DOM element?
|
564
|
+
_.isElement = function(obj) {
|
565
|
+
return !!(obj && obj.nodeType == 1);
|
566
|
+
};
|
567
|
+
|
568
|
+
// Is a given value an array?
|
569
|
+
// Delegates to ECMA5's native Array.isArray
|
570
|
+
_.isArray = nativeIsArray || function(obj) {
|
571
|
+
return !!(obj && obj.concat && obj.unshift && !obj.callee);
|
572
|
+
};
|
573
|
+
|
574
|
+
// Is a given variable an arguments object?
|
575
|
+
_.isArguments = function(obj) {
|
576
|
+
return !!(obj && obj.callee);
|
577
|
+
};
|
578
|
+
|
579
|
+
// Is a given value a function?
|
580
|
+
_.isFunction = function(obj) {
|
581
|
+
return !!(obj && obj.constructor && obj.call && obj.apply);
|
582
|
+
};
|
583
|
+
|
584
|
+
// Is a given value a string?
|
585
|
+
_.isString = function(obj) {
|
586
|
+
return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
|
587
|
+
};
|
588
|
+
|
589
|
+
// Is a given value a number?
|
590
|
+
_.isNumber = function(obj) {
|
591
|
+
return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
|
592
|
+
};
|
593
|
+
|
594
|
+
// Is the given value NaN -- this one is interesting. NaN != NaN, and
|
595
|
+
// isNaN(undefined) == true, so we make sure it's a number first.
|
596
|
+
_.isNaN = function(obj) {
|
597
|
+
return toString.call(obj) === '[object Number]' && isNaN(obj);
|
598
|
+
};
|
599
|
+
|
600
|
+
// Is a given value a boolean?
|
601
|
+
_.isBoolean = function(obj) {
|
602
|
+
return obj === true || obj === false;
|
603
|
+
};
|
604
|
+
|
605
|
+
// Is a given value a date?
|
606
|
+
_.isDate = function(obj) {
|
607
|
+
return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear);
|
608
|
+
};
|
609
|
+
|
610
|
+
// Is the given value a regular expression?
|
611
|
+
_.isRegExp = function(obj) {
|
612
|
+
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
|
613
|
+
};
|
614
|
+
|
615
|
+
// Is a given value equal to null?
|
616
|
+
_.isNull = function(obj) {
|
617
|
+
return obj === null;
|
618
|
+
};
|
619
|
+
|
620
|
+
// Is a given variable undefined?
|
621
|
+
_.isUndefined = function(obj) {
|
622
|
+
return obj === void 0;
|
623
|
+
};
|
624
|
+
|
625
|
+
// Utility Functions
|
626
|
+
// -----------------
|
627
|
+
|
628
|
+
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
|
629
|
+
// previous owner. Returns a reference to the Underscore object.
|
630
|
+
_.noConflict = function() {
|
631
|
+
root._ = previousUnderscore;
|
632
|
+
return this;
|
633
|
+
};
|
634
|
+
|
635
|
+
// Keep the identity function around for default iterators.
|
636
|
+
_.identity = function(value) {
|
637
|
+
return value;
|
638
|
+
};
|
639
|
+
|
640
|
+
// Run a function **n** times.
|
641
|
+
_.times = function (n, iterator, context) {
|
642
|
+
for (var i = 0; i < n; i++) iterator.call(context, i);
|
643
|
+
};
|
644
|
+
|
645
|
+
// Add your own custom functions to the Underscore object, ensuring that
|
646
|
+
// they're correctly added to the OOP wrapper as well.
|
647
|
+
_.mixin = function(obj) {
|
648
|
+
each(_.functions(obj), function(name){
|
649
|
+
addToWrapper(name, _[name] = obj[name]);
|
650
|
+
});
|
651
|
+
};
|
652
|
+
|
653
|
+
// Generate a unique integer id (unique within the entire client session).
|
654
|
+
// Useful for temporary DOM ids.
|
655
|
+
var idCounter = 0;
|
656
|
+
_.uniqueId = function(prefix) {
|
657
|
+
var id = idCounter++;
|
658
|
+
return prefix ? prefix + id : id;
|
659
|
+
};
|
660
|
+
|
661
|
+
// By default, Underscore uses ERB-style template delimiters, change the
|
662
|
+
// following template settings to use alternative delimiters.
|
663
|
+
_.templateSettings = {
|
664
|
+
evaluate : /<%([\s\S]+?)%>/g,
|
665
|
+
interpolate : /<%=([\s\S]+?)%>/g
|
666
|
+
};
|
667
|
+
|
668
|
+
// JavaScript micro-templating, similar to John Resig's implementation.
|
669
|
+
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
670
|
+
// and correctly escapes quotes within interpolated code.
|
671
|
+
_.template = function(str, data) {
|
672
|
+
var c = _.templateSettings;
|
673
|
+
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
674
|
+
'with(obj||{}){__p.push(\'' +
|
675
|
+
str.replace(/\\/g, '\\\\')
|
676
|
+
.replace(/'/g, "\\'")
|
677
|
+
.replace(c.interpolate, function(match, code) {
|
678
|
+
return "'," + code.replace(/\\'/g, "'") + ",'";
|
679
|
+
})
|
680
|
+
.replace(c.evaluate || null, function(match, code) {
|
681
|
+
return "');" + code.replace(/\\'/g, "'")
|
682
|
+
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
|
683
|
+
})
|
684
|
+
.replace(/\r/g, '\\r')
|
685
|
+
.replace(/\n/g, '\\n')
|
686
|
+
.replace(/\t/g, '\\t')
|
687
|
+
+ "');}return __p.join('');";
|
688
|
+
var func = new Function('obj', tmpl);
|
689
|
+
return data ? func(data) : func;
|
690
|
+
};
|
691
|
+
|
692
|
+
// The OOP Wrapper
|
693
|
+
// ---------------
|
694
|
+
|
695
|
+
// If Underscore is called as a function, it returns a wrapped object that
|
696
|
+
// can be used OO-style. This wrapper holds altered versions of all the
|
697
|
+
// underscore functions. Wrapped objects may be chained.
|
698
|
+
var wrapper = function(obj) { this._wrapped = obj; };
|
699
|
+
|
700
|
+
// Expose `wrapper.prototype` as `_.prototype`
|
701
|
+
_.prototype = wrapper.prototype;
|
702
|
+
|
703
|
+
// Helper function to continue chaining intermediate results.
|
704
|
+
var result = function(obj, chain) {
|
705
|
+
return chain ? _(obj).chain() : obj;
|
706
|
+
};
|
707
|
+
|
708
|
+
// A method to easily add functions to the OOP wrapper.
|
709
|
+
var addToWrapper = function(name, func) {
|
710
|
+
wrapper.prototype[name] = function() {
|
711
|
+
var args = slice.call(arguments);
|
712
|
+
unshift.call(args, this._wrapped);
|
713
|
+
return result(func.apply(_, args), this._chain);
|
714
|
+
};
|
715
|
+
};
|
716
|
+
|
717
|
+
// Add all of the Underscore functions to the wrapper object.
|
718
|
+
_.mixin(_);
|
719
|
+
|
720
|
+
// Add all mutator Array functions to the wrapper.
|
721
|
+
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
722
|
+
var method = ArrayProto[name];
|
723
|
+
wrapper.prototype[name] = function() {
|
724
|
+
method.apply(this._wrapped, arguments);
|
725
|
+
return result(this._wrapped, this._chain);
|
726
|
+
};
|
727
|
+
});
|
728
|
+
|
729
|
+
// Add all accessor Array functions to the wrapper.
|
730
|
+
each(['concat', 'join', 'slice'], function(name) {
|
731
|
+
var method = ArrayProto[name];
|
732
|
+
wrapper.prototype[name] = function() {
|
733
|
+
return result(method.apply(this._wrapped, arguments), this._chain);
|
734
|
+
};
|
735
|
+
});
|
736
|
+
|
737
|
+
// Start chaining a wrapped Underscore object.
|
738
|
+
wrapper.prototype.chain = function() {
|
739
|
+
this._chain = true;
|
740
|
+
return this;
|
741
|
+
};
|
742
|
+
|
743
|
+
// Extracts the result from a wrapped and chained object.
|
744
|
+
wrapper.prototype.value = function() {
|
745
|
+
return this._wrapped;
|
746
|
+
};
|
747
|
+
|
748
|
+
})();
|