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