Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated underscore

  • Loading branch information...
commit b3a81ab6b8d0a17d736151e7d3a953397a1387c3 1 parent c422bcb
@kmalakoff authored
Sorry, we could not display the entire diff because it was too big.
View
10 Bakefile.coffee
@@ -129,7 +129,7 @@ module.exports =
# knockback optional dependencies
'cp -v lodash vendor/optional/lodash.js'
- 'uglifyjs -o vendor/optional/lodash-0.6.1.min.js vendor/optional/lodash-0.6.1.js'
+ 'uglifyjs -o vendor/optional/lodash-0.8.1.min.js vendor/optional/lodash-0.8.1.js'
# 'cp -v lodash/lodash.min.js vendor/optional/lodash.min.js' # packaged lodash.min.js doesn't concatenate properly
'cp -v backbone-modelref vendor/optional/backbone-modelref.js'
'cp -v backbone-modelref/backbone-modelref.min.js vendor/optional/backbone-modelref.min.js'
@@ -137,10 +137,10 @@ module.exports =
'cp -v backbone-relational/backbone-relational.min.js vendor/optional/backbone-relational.min.js'
# full and core stack
- 'cat src/license-header-full-stack.js vendor/underscore-1.3.3.js vendor/backbone-0.9.2.js vendor/knockout-2.1.0.js knockback.js > knockback-full-stack.js'
- 'cat src/license-header-full-stack.js vendor/underscore-1.3.3.min.js vendor/backbone-0.9.2.min.js vendor/knockout-2.1.0.min.js knockback.min.js > knockback-full-stack.min.js'
- 'cat src/license-header-core-stack.js vendor/underscore-1.3.3.js vendor/backbone-0.9.2.js vendor/knockout-2.1.0.js knockback-core.js > knockback-core-stack.js'
- 'cat src/license-header-core-stack.js vendor/underscore-1.3.3.min.js vendor/backbone-0.9.2.min.js vendor/knockout-2.1.0.min.js knockback-core.min.js > knockback-core-stack.min.js'
+ 'cat src/license-header-full-stack.js vendor/underscore-1.4.2.js vendor/backbone-0.9.2.js vendor/knockout-2.1.0.js knockback.js > knockback-full-stack.js'
+ 'cat src/license-header-full-stack.js vendor/underscore-1.4.2.min.js vendor/backbone-0.9.2.min.js vendor/knockout-2.1.0.min.js knockback.min.js > knockback-full-stack.min.js'
+ 'cat src/license-header-core-stack.js vendor/underscore-1.4.2.js vendor/backbone-0.9.2.js vendor/knockout-2.1.0.js knockback-core.js > knockback-core-stack.js'
+ 'cat src/license-header-core-stack.js vendor/underscore-1.4.2.min.js vendor/backbone-0.9.2.min.js vendor/knockout-2.1.0.min.js knockback-core.min.js > knockback-core-stack.min.js'
# npm
'cp README.md packages/npm/README.md'
View
605 knockback-core-stack.js
@@ -4,13 +4,10 @@
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Dependencies: Knockout.js, Backbone.js, and Underscore.js.
*/
-// Underscore.js 1.3.3
+// Underscore.js 1.4.2
+// http://underscorejs.org
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
-// Underscore is freely distributable under the MIT license.
-// Portions of Underscore are inspired or borrowed from Prototype,
-// Oliver Steele's Functional, and John Resig's Micro-Templating.
-// For all details and documentation:
-// http://documentcloud.github.com/underscore
+// Underscore may be freely distributed under the MIT license.
(function() {
@@ -30,7 +27,9 @@
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
// Create quick reference variables for speed access to core prototypes.
- var slice = ArrayProto.slice,
+ var push = ArrayProto.push,
+ slice = ArrayProto.slice,
+ concat = ArrayProto.concat,
unshift = ArrayProto.unshift,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
@@ -52,7 +51,11 @@
nativeBind = FuncProto.bind;
// Create a safe reference to the Underscore object for use below.
- var _ = function(obj) { return new wrapper(obj); };
+ var _ = function(obj) {
+ if (obj instanceof _) return obj;
+ if (!(this instanceof _)) return new _(obj);
+ this._wrapped = obj;
+ };
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
@@ -68,7 +71,7 @@
}
// Current version.
- _.VERSION = '1.3.3';
+ _.VERSION = '1.4.2';
// Collection Functions
// --------------------
@@ -82,7 +85,7 @@
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
- if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
+ if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
@@ -102,7 +105,6 @@
each(obj, function(value, index, list) {
results[results.length] = iterator.call(context, value, index, list);
});
- if (obj.length === +obj.length) results.length = obj.length;
return results;
};
@@ -134,11 +136,24 @@
if (obj == null) obj = [];
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
- return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
+ return arguments.length > 2 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
}
- var reversed = _.toArray(obj).reverse();
- if (context && !initial) iterator = _.bind(iterator, context);
- return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
+ var length = obj.length;
+ if (length !== +length) {
+ var keys = _.keys(obj);
+ length = keys.length;
+ }
+ each(obj, function(value, index, list) {
+ index = keys ? keys[--length] : --length;
+ if (!initial) {
+ memo = obj[index];
+ initial = true;
+ } else {
+ memo = iterator.call(context, memo, obj[index], index, list);
+ }
+ });
+ if (!initial) throw new TypeError('Reduce of empty array with no initial value');
+ return memo;
};
// Return the first value which passes a truth test. Aliased as `detect`.
@@ -180,6 +195,7 @@
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
_.every = _.all = function(obj, iterator, context) {
+ iterator || (iterator = _.identity);
var result = true;
if (obj == null) return result;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
@@ -203,9 +219,9 @@
return !!result;
};
- // Determine if a given value is included in the array or object using `===`.
- // Aliased as `contains`.
- _.include = _.contains = function(obj, target) {
+ // Determine if the array or object contains a given value (using `===`).
+ // Aliased as `include`.
+ _.contains = _.include = function(obj, target) {
var found = false;
if (obj == null) return found;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
@@ -219,7 +235,7 @@
_.invoke = function(obj, method) {
var args = slice.call(arguments, 2);
return _.map(obj, function(value) {
- return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
+ return (_.isFunction(method) ? method : value[method]).apply(value, args);
});
};
@@ -228,9 +244,25 @@
return _.map(obj, function(value){ return value[key]; });
};
+ // Convenience version of a common use case of `filter`: selecting only objects
+ // with specific `key:value` pairs.
+ _.where = function(obj, attrs) {
+ if (_.isEmpty(attrs)) return [];
+ return _.filter(obj, function(value) {
+ for (var key in attrs) {
+ if (attrs[key] !== value[key]) return false;
+ }
+ return true;
+ });
+ };
+
// Return the maximum element or (element-based computation).
+ // Can't optimize arrays of integers longer than 65,535 elements.
+ // See: https://bugs.webkit.org/show_bug.cgi?id=80797
_.max = function(obj, iterator, context) {
- if (!iterator && _.isArray(obj) && obj[0] === +obj[0]) return Math.max.apply(Math, obj);
+ if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
+ return Math.max.apply(Math, obj);
+ }
if (!iterator && _.isEmpty(obj)) return -Infinity;
var result = {computed : -Infinity};
each(obj, function(value, index, list) {
@@ -242,7 +274,9 @@
// Return the minimum element (or element-based computation).
_.min = function(obj, iterator, context) {
- if (!iterator && _.isArray(obj) && obj[0] === +obj[0]) return Math.min.apply(Math, obj);
+ if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
+ return Math.min.apply(Math, obj);
+ }
if (!iterator && _.isEmpty(obj)) return Infinity;
var result = {computed : Infinity};
each(obj, function(value, index, list) {
@@ -254,67 +288,94 @@
// Shuffle an array.
_.shuffle = function(obj) {
- var shuffled = [], rand;
- each(obj, function(value, index, list) {
- rand = Math.floor(Math.random() * (index + 1));
- shuffled[index] = shuffled[rand];
+ var rand;
+ var index = 0;
+ var shuffled = [];
+ each(obj, function(value) {
+ rand = _.random(index++);
+ shuffled[index - 1] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;
};
+ // An internal function to generate lookup iterators.
+ var lookupIterator = function(value) {
+ return _.isFunction(value) ? value : function(obj){ return obj[value]; };
+ };
+
// Sort the object's values by a criterion produced by an iterator.
- _.sortBy = function(obj, val, context) {
- var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
+ _.sortBy = function(obj, value, context) {
+ var iterator = lookupIterator(value);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value : value,
+ index : index,
criteria : iterator.call(context, value, index, list)
};
}).sort(function(left, right) {
- var a = left.criteria, b = right.criteria;
- if (a === void 0) return 1;
- if (b === void 0) return -1;
- return a < b ? -1 : a > b ? 1 : 0;
+ var a = left.criteria;
+ var b = right.criteria;
+ if (a !== b) {
+ if (a > b || a === void 0) return 1;
+ if (a < b || b === void 0) return -1;
+ }
+ return left.index < right.index ? -1 : 1;
}), 'value');
};
- // Groups the object's values by a criterion. Pass either a string attribute
- // to group by, or a function that returns the criterion.
- _.groupBy = function(obj, val) {
+ // An internal function used for aggregate "group by" operations.
+ var group = function(obj, value, context, behavior) {
var result = {};
- var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
+ var iterator = lookupIterator(value);
each(obj, function(value, index) {
- var key = iterator(value, index);
- (result[key] || (result[key] = [])).push(value);
+ var key = iterator.call(context, value, index, obj);
+ behavior(result, key, value);
});
return result;
};
- // Use a comparator function to figure out at what index an object should
- // be inserted so as to maintain order. Uses binary search.
- _.sortedIndex = function(array, obj, iterator) {
- iterator || (iterator = _.identity);
+ // Groups the object's values by a criterion. Pass either a string attribute
+ // to group by, or a function that returns the criterion.
+ _.groupBy = function(obj, value, context) {
+ return group(obj, value, context, function(result, key, value) {
+ (_.has(result, key) ? result[key] : (result[key] = [])).push(value);
+ });
+ };
+
+ // Counts instances of an object that group by a certain criterion. Pass
+ // either a string attribute to count by, or a function that returns the
+ // criterion.
+ _.countBy = function(obj, value, context) {
+ return group(obj, value, context, function(result, key, value) {
+ if (!_.has(result, key)) result[key] = 0;
+ result[key]++;
+ });
+ };
+
+ // Use a comparator function to figure out the smallest index at which
+ // an object should be inserted so as to maintain order. Uses binary search.
+ _.sortedIndex = function(array, obj, iterator, context) {
+ iterator = iterator == null ? _.identity : lookupIterator(iterator);
+ var value = iterator.call(context, obj);
var low = 0, high = array.length;
while (low < high) {
- var mid = (low + high) >> 1;
- iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
+ var mid = (low + high) >>> 1;
+ iterator.call(context, array[mid]) < value ? low = mid + 1 : high = mid;
}
return low;
};
// Safely convert anything iterable into a real, live array.
_.toArray = function(obj) {
- if (!obj) return [];
- if (_.isArray(obj)) return slice.call(obj);
- if (_.isArguments(obj)) return slice.call(obj);
- if (obj.toArray && _.isFunction(obj.toArray)) return obj.toArray();
+ if (!obj) return [];
+ if (obj.length === +obj.length) return slice.call(obj);
return _.values(obj);
};
// Return the number of elements in an object.
_.size = function(obj) {
- return _.isArray(obj) ? obj.length : _.keys(obj).length;
+ return (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
};
// Array Functions
@@ -327,7 +388,7 @@
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
};
- // Returns everything but the last entry of the array. Especcialy useful on
+ // Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N. The **guard** check allows it to work with
// `_.map`.
@@ -345,12 +406,12 @@
}
};
- // Returns everything but the first entry of the array. Aliased as `tail`.
- // Especially useful on the arguments object. Passing an **index** will return
- // the rest of the values in the array from that index onward. The **guard**
+ // Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
+ // Especially useful on the arguments object. Passing an **n** will return
+ // the rest N values in the array. The **guard**
// check allows it to work with `_.map`.
- _.rest = _.tail = function(array, index, guard) {
- return slice.call(array, (index == null) || guard ? 1 : index);
+ _.rest = _.tail = _.drop = function(array, n, guard) {
+ return slice.call(array, (n == null) || guard ? 1 : n);
};
// Trim out all falsy values from an array.
@@ -358,13 +419,21 @@
return _.filter(array, function(value){ return !!value; });
};
+ // Internal implementation of a recursive `flatten` function.
+ var flatten = function(input, shallow, output) {
+ each(input, function(value) {
+ if (_.isArray(value)) {
+ shallow ? push.apply(output, value) : flatten(value, shallow, output);
+ } else {
+ output.push(value);
+ }
+ });
+ return output;
+ };
+
// Return a completely flattened version of an array.
_.flatten = function(array, shallow) {
- return _.reduce(array, function(memo, value) {
- if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
- memo[memo.length] = value;
- return memo;
- }, []);
+ return flatten(array, shallow, []);
};
// Return a version of the array that does not contain the specified value(s).
@@ -375,30 +444,28 @@
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
- _.uniq = _.unique = function(array, isSorted, iterator) {
- var initial = iterator ? _.map(array, iterator) : array;
+ _.uniq = _.unique = function(array, isSorted, iterator, context) {
+ var initial = iterator ? _.map(array, iterator, context) : array;
var results = [];
- // The `isSorted` flag is irrelevant if the array only contains two elements.
- if (array.length < 3) isSorted = true;
- _.reduce(initial, function (memo, value, index) {
- if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
- memo.push(value);
+ var seen = [];
+ each(initial, function(value, index) {
+ if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.contains(seen, value)) {
+ seen.push(value);
results.push(array[index]);
}
- return memo;
- }, []);
+ });
return results;
};
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
- return _.uniq(_.flatten(arguments, true));
+ return _.uniq(concat.apply(ArrayProto, arguments));
};
// Produce an array that contains every item shared between all the
- // passed-in arrays. (Aliased as "intersect" for back-compat.)
- _.intersection = _.intersect = function(array) {
+ // passed-in arrays.
+ _.intersection = function(array) {
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
@@ -410,8 +477,8 @@
// Take the difference between one array and a number of other arrays.
// Only the elements present in just the first array will remain.
_.difference = function(array) {
- var rest = _.flatten(slice.call(arguments, 1), true);
- return _.filter(array, function(value){ return !_.include(rest, value); });
+ var rest = concat.apply(ArrayProto, slice.call(arguments, 1));
+ return _.filter(array, function(value){ return !_.contains(rest, value); });
};
// Zip together multiple lists into a single array -- elements that share
@@ -420,10 +487,27 @@
var args = slice.call(arguments);
var length = _.max(_.pluck(args, 'length'));
var results = new Array(length);
- for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
+ for (var i = 0; i < length; i++) {
+ results[i] = _.pluck(args, "" + i);
+ }
return results;
};
+ // Converts lists into objects. Pass either a single array of `[key, value]`
+ // pairs, or two parallel arrays of the same length -- one of keys, and one of
+ // the corresponding values.
+ _.object = function(list, values) {
+ var result = {};
+ for (var i = 0, l = list.length; i < l; i++) {
+ if (values) {
+ result[list[i]] = values[i];
+ } else {
+ result[list[i][0]] = list[i][1];
+ }
+ }
+ return result;
+ };
+
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
// we need this function. Return the position of the first occurrence of an
// item in an array, or -1 if the item is not included in the array.
@@ -432,22 +516,29 @@
// for **isSorted** to use binary search.
_.indexOf = function(array, item, isSorted) {
if (array == null) return -1;
- var i, l;
+ var i = 0, l = array.length;
if (isSorted) {
- i = _.sortedIndex(array, item);
- return array[i] === item ? i : -1;
+ if (typeof isSorted == 'number') {
+ i = (isSorted < 0 ? Math.max(0, l + isSorted) : isSorted);
+ } else {
+ i = _.sortedIndex(array, item);
+ return array[i] === item ? i : -1;
+ }
}
- if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
- for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
+ if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
+ for (; i < l; i++) if (array[i] === item) return i;
return -1;
};
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
- _.lastIndexOf = function(array, item) {
+ _.lastIndexOf = function(array, item, from) {
if (array == null) return -1;
- if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
- var i = array.length;
- while (i--) if (i in array && array[i] === item) return i;
+ var hasIndex = from != null;
+ if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) {
+ return hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item);
+ }
+ var i = (hasIndex ? from : array.length);
+ while (i--) if (array[i] === item) return i;
return -1;
};
@@ -539,17 +630,19 @@
context = this; args = arguments;
var later = function() {
timeout = null;
- if (more) func.apply(context, args);
+ if (more) {
+ result = func.apply(context, args);
+ }
whenDone();
};
if (!timeout) timeout = setTimeout(later, wait);
if (throttling) {
more = true;
} else {
+ throttling = true;
result = func.apply(context, args);
}
whenDone();
- throttling = true;
return result;
};
};
@@ -559,16 +652,18 @@
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
- var timeout;
+ var timeout, result;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
- if (!immediate) func.apply(context, args);
+ if (!immediate) result = func.apply(context, args);
};
- if (immediate && !timeout) func.apply(context, args);
+ var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
+ if (callNow) result = func.apply(context, args);
+ return result;
};
};
@@ -579,7 +674,9 @@
return function() {
if (ran) return memo;
ran = true;
- return memo = func.apply(this, arguments);
+ memo = func.apply(this, arguments);
+ func = null;
+ return memo;
};
};
@@ -588,7 +685,8 @@
// conditionally execute the original function.
_.wrap = function(func, wrapper) {
return function() {
- var args = [func].concat(slice.call(arguments, 0));
+ var args = [func];
+ push.apply(args, arguments);
return wrapper.apply(this, args);
};
};
@@ -610,7 +708,9 @@
_.after = function(times, func) {
if (times <= 0) return func();
return function() {
- if (--times < 1) { return func.apply(this, arguments); }
+ if (--times < 1) {
+ return func.apply(this, arguments);
+ }
};
};
@@ -628,7 +728,23 @@
// Retrieve the values of an object's properties.
_.values = function(obj) {
- return _.map(obj, _.identity);
+ var values = [];
+ for (var key in obj) if (_.has(obj, key)) values.push(obj[key]);
+ return values;
+ };
+
+ // Convert an object into a list of `[key, value]` pairs.
+ _.pairs = function(obj) {
+ var pairs = [];
+ for (var key in obj) if (_.has(obj, key)) pairs.push([key, obj[key]]);
+ return pairs;
+ };
+
+ // Invert the keys and values of an object. The values must be serializable.
+ _.invert = function(obj) {
+ var result = {};
+ for (var key in obj) if (_.has(obj, key)) result[obj[key]] = key;
+ return result;
};
// Return a sorted list of the function names available on the object.
@@ -653,11 +769,22 @@
// Return a copy of the object only containing the whitelisted properties.
_.pick = function(obj) {
- var result = {};
- each(_.flatten(slice.call(arguments, 1)), function(key) {
- if (key in obj) result[key] = obj[key];
+ var copy = {};
+ var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
+ each(keys, function(key) {
+ if (key in obj) copy[key] = obj[key];
});
- return result;
+ return copy;
+ };
+
+ // Return a copy of the object without the blacklisted properties.
+ _.omit = function(obj) {
+ var copy = {};
+ var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
+ for (var key in obj) {
+ if (!_.contains(keys, key)) copy[key] = obj[key];
+ }
+ return copy;
};
// Fill in a given object with default properties.
@@ -684,19 +811,16 @@
return obj;
};
- // Internal recursive comparison function.
- function eq(a, b, stack) {
+ // Internal recursive comparison function for `isEqual`.
+ var eq = function(a, b, aStack, bStack) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
if (a === b) return a !== 0 || 1 / a == 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a == null || b == null) return a === b;
// Unwrap any wrapped objects.
- if (a._chain) a = a._wrapped;
- if (b._chain) b = b._wrapped;
- // Invoke a custom `isEqual` method if one is provided.
- if (a.isEqual && _.isFunction(a.isEqual)) return a.isEqual(b);
- if (b.isEqual && _.isFunction(b.isEqual)) return b.isEqual(a);
+ if (a instanceof _) a = a._wrapped;
+ if (b instanceof _) b = b._wrapped;
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className != toString.call(b)) return false;
@@ -726,14 +850,15 @@
if (typeof a != 'object' || typeof b != 'object') return false;
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
- var length = stack.length;
+ var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
- if (stack[length] == a) return true;
+ if (aStack[length] == a) return bStack[length] == b;
}
// Add the first object to the stack of traversed objects.
- stack.push(a);
+ aStack.push(a);
+ bStack.push(b);
var size = 0, result = true;
// Recursively compare objects and arrays.
if (className == '[object Array]') {
@@ -743,20 +868,24 @@
if (result) {
// Deep compare the contents, ignoring non-numeric properties.
while (size--) {
- // Ensure commutative equality for sparse arrays.
- if (!(result = size in a == size in b && eq(a[size], b[size], stack))) break;
+ if (!(result = eq(a[size], b[size], aStack, bStack))) break;
}
}
} else {
- // Objects with different constructors are not equivalent.
- if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
+ // Objects with different constructors are not equivalent, but `Object`s
+ // from different frames are.
+ var aCtor = a.constructor, bCtor = b.constructor;
+ if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
+ _.isFunction(bCtor) && (bCtor instanceof bCtor))) {
+ return false;
+ }
// Deep compare objects.
for (var key in a) {
if (_.has(a, key)) {
// Count the expected number of properties.
size++;
// Deep compare each member.
- if (!(result = _.has(b, key) && eq(a[key], b[key], stack))) break;
+ if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
}
}
// Ensure that both objects contain the same number of properties.
@@ -768,13 +897,14 @@
}
}
// Remove the first object from the stack of traversed objects.
- stack.pop();
+ aStack.pop();
+ bStack.pop();
return result;
- }
+ };
// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
- return eq(a, b, []);
+ return eq(a, b, [], []);
};
// Is a given array, string, or object empty?
@@ -788,7 +918,7 @@
// Is a given value a DOM element?
_.isElement = function(obj) {
- return !!(obj && obj.nodeType == 1);
+ return !!(obj && obj.nodeType === 1);
};
// Is a given value an array?
@@ -802,40 +932,36 @@
return obj === Object(obj);
};
- // Is a given variable an arguments object?
- _.isArguments = function(obj) {
- return toString.call(obj) == '[object Arguments]';
- };
+ // Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
+ each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
+ _['is' + name] = function(obj) {
+ return toString.call(obj) == '[object ' + name + ']';
+ };
+ });
+
+ // Define a fallback version of the method in browsers (ahem, IE), where
+ // there isn't any inspectable "Arguments" type.
if (!_.isArguments(arguments)) {
_.isArguments = function(obj) {
return !!(obj && _.has(obj, 'callee'));
};
}
- // Is a given value a function?
- _.isFunction = function(obj) {
- return toString.call(obj) == '[object Function]';
- };
-
- // Is a given value a string?
- _.isString = function(obj) {
- return toString.call(obj) == '[object String]';
- };
-
- // Is a given value a number?
- _.isNumber = function(obj) {
- return toString.call(obj) == '[object Number]';
- };
+ // Optimize `isFunction` if appropriate.
+ if (typeof (/./) !== 'function') {
+ _.isFunction = function(obj) {
+ return typeof obj === 'function';
+ };
+ }
// Is a given object a finite number?
_.isFinite = function(obj) {
return _.isNumber(obj) && isFinite(obj);
};
- // Is the given value `NaN`?
+ // Is the given value `NaN`? (NaN is the only number which does not equal itself).
_.isNaN = function(obj) {
- // `NaN` is the only value for which `===` is not reflexive.
- return obj !== obj;
+ return _.isNumber(obj) && obj != +obj;
};
// Is a given value a boolean?
@@ -843,16 +969,6 @@
return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
};
- // Is a given value a date?
- _.isDate = function(obj) {
- return toString.call(obj) == '[object Date]';
- };
-
- // Is the given value a regular expression?
- _.isRegExp = function(obj) {
- return toString.call(obj) == '[object RegExp]';
- };
-
// Is a given value equal to null?
_.isNull = function(obj) {
return obj === null;
@@ -863,7 +979,8 @@
return obj === void 0;
};
- // Has own property?
+ // Shortcut function for checking if an object has a given property directly
+ // on itself (in other words, not on a prototype).
_.has = function(obj, key) {
return hasOwnProperty.call(obj, key);
};
@@ -884,15 +1001,48 @@
};
// Run a function **n** times.
- _.times = function (n, iterator, context) {
+ _.times = function(n, iterator, context) {
for (var i = 0; i < n; i++) iterator.call(context, i);
};
- // Escape a string for HTML interpolation.
- _.escape = function(string) {
- return (''+string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
+ // Return a random integer between min and max (inclusive).
+ _.random = function(min, max) {
+ if (max == null) {
+ max = min;
+ min = 0;
+ }
+ return min + (0 | Math.random() * (max - min + 1));
};
+ // List of HTML entities for escaping.
+ var entityMap = {
+ escape: {
+ '&': '&amp;',
+ '<': '&lt;',
+ '>': '&gt;',
+ '"': '&quot;',
+ "'": '&#x27;',
+ '/': '&#x2F;'
+ }
+ };
+ entityMap.unescape = _.invert(entityMap.escape);
+
+ // Regexes containing the keys and values listed immediately above.
+ var entityRegexes = {
+ escape: new RegExp('[' + _.keys(entityMap.escape).join('') + ']', 'g'),
+ unescape: new RegExp('(' + _.keys(entityMap.unescape).join('|') + ')', 'g')
+ };
+
+ // Functions for escaping and unescaping strings to/from HTML interpolation.
+ _.each(['escape', 'unescape'], function(method) {
+ _[method] = function(string) {
+ if (string == null) return '';
+ return ('' + string).replace(entityRegexes[method], function(match) {
+ return entityMap[method][match];
+ });
+ };
+ });
+
// If the value of the named property is a function then invoke it;
// otherwise, return it.
_.result = function(object, property) {
@@ -901,11 +1051,15 @@
return _.isFunction(value) ? value.call(object) : value;
};
- // Add your own custom functions to the Underscore object, ensuring that
- // they're correctly added to the OOP wrapper as well.
+ // Add your own custom functions to the Underscore object.
_.mixin = function(obj) {
each(_.functions(obj), function(name){
- addToWrapper(name, _[name] = obj[name]);
+ var func = _[name] = obj[name];
+ _.prototype[name] = function() {
+ var args = [this._wrapped];
+ push.apply(args, arguments);
+ return result.call(this, func.apply(_, args));
+ };
});
};
@@ -928,72 +1082,70 @@
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
- var noMatch = /.^/;
+ var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
- '\\': '\\',
- "'": "'",
- 'r': '\r',
- 'n': '\n',
- 't': '\t',
- 'u2028': '\u2028',
- 'u2029': '\u2029'
+ "'": "'",
+ '\\': '\\',
+ '\r': 'r',
+ '\n': 'n',
+ '\t': 't',
+ '\u2028': 'u2028',
+ '\u2029': 'u2029'
};
- for (var p in escapes) escapes[escapes[p]] = p;
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
- var unescaper = /\\(\\|'|r|n|t|u2028|u2029)/g;
-
- // Within an interpolation, evaluation, or escaping, remove HTML escaping
- // that had been previously added.
- var unescape = function(code) {
- return code.replace(unescaper, function(match, escape) {
- return escapes[escape];
- });
- };
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(text, data, settings) {
- settings = _.defaults(settings || {}, _.templateSettings);
-
- // Compile the template source, taking care to escape characters that
- // cannot be included in a string literal and then unescape them in code
- // blocks.
- var source = "__p+='" + text
- .replace(escaper, function(match) {
- return '\\' + escapes[match];
- })
- .replace(settings.escape || noMatch, function(match, code) {
- return "'+\n_.escape(" + unescape(code) + ")+\n'";
- })
- .replace(settings.interpolate || noMatch, function(match, code) {
- return "'+\n(" + unescape(code) + ")+\n'";
- })
- .replace(settings.evaluate || noMatch, function(match, code) {
- return "';\n" + unescape(code) + "\n;__p+='";
- }) + "';\n";
+ settings = _.defaults({}, settings, _.templateSettings);
+
+ // Combine delimiters into one regular expression via alternation.
+ var matcher = new RegExp([
+ (settings.escape || noMatch).source,
+ (settings.interpolate || noMatch).source,
+ (settings.evaluate || noMatch).source
+ ].join('|') + '|$', 'g');
+
+ // Compile the template source, escaping string literals appropriately.
+ var index = 0;
+ var source = "__p+='";
+ text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
+ source += text.slice(index, offset)
+ .replace(escaper, function(match) { return '\\' + escapes[match]; });
+ source +=
+ escape ? "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'" :
+ interpolate ? "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'" :
+ evaluate ? "';\n" + evaluate + "\n__p+='" : '';
+ index = offset + match.length;
+ });
+ source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
- source = "var __p='';" +
- "var print=function(){__p+=Array.prototype.join.call(arguments, '')};\n" +
+ source = "var __t,__p='',__j=Array.prototype.join," +
+ "print=function(){__p+=__j.call(arguments,'');};\n" +
source + "return __p;\n";
- var render = new Function(settings.variable || 'obj', '_', source);
+ try {
+ var render = new Function(settings.variable || 'obj', '_', source);
+ } catch (e) {
+ e.source = source;
+ throw e;
+ }
+
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
};
- // Provide the compiled function source as a convenience for build time
- // precompilation.
- template.source = 'function(' + (settings.variable || 'obj') + '){\n' +
- source + '}';
+ // Provide the compiled function source as a convenience for precompilation.
+ template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
@@ -1003,29 +1155,15 @@
return _(obj).chain();
};
- // The OOP Wrapper
+ // OOP
// ---------------
-
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
- var wrapper = function(obj) { this._wrapped = obj; };
-
- // Expose `wrapper.prototype` as `_.prototype`
- _.prototype = wrapper.prototype;
// Helper function to continue chaining intermediate results.
- var result = function(obj, chain) {
- return chain ? _(obj).chain() : obj;
- };
-
- // A method to easily add functions to the OOP wrapper.
- var addToWrapper = function(name, func) {
- wrapper.prototype[name] = function() {
- var args = slice.call(arguments);
- unshift.call(args, this._wrapped);
- return result(func.apply(_, args), this._chain);
- };
+ var result = function(obj) {
+ return this._chain ? _(obj).chain() : obj;
};
// Add all of the Underscore functions to the wrapper object.
@@ -1034,33 +1172,36 @@
// Add all mutator Array functions to the wrapper.
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
var method = ArrayProto[name];
- wrapper.prototype[name] = function() {
- var wrapped = this._wrapped;
- method.apply(wrapped, arguments);
- var length = wrapped.length;
- if ((name == 'shift' || name == 'splice') && length === 0) delete wrapped[0];
- return result(wrapped, this._chain);
+ _.prototype[name] = function() {
+ var obj = this._wrapped;
+ method.apply(obj, arguments);
+ if ((name == 'shift' || name == 'splice') && obj.length === 0) delete obj[0];
+ return result.call(this, obj);
};
});
// Add all accessor Array functions to the wrapper.
each(['concat', 'join', 'slice'], function(name) {
var method = ArrayProto[name];
- wrapper.prototype[name] = function() {
- return result(method.apply(this._wrapped, arguments), this._chain);
+ _.prototype[name] = function() {
+ return result.call(this, method.apply(this._wrapped, arguments));
};
});
- // Start chaining a wrapped Underscore object.
- wrapper.prototype.chain = function() {
- this._chain = true;
- return this;
- };
+ _.extend(_.prototype, {
- // Extracts the result from a wrapped and chained object.
- wrapper.prototype.value = function() {
- return this._wrapped;
- };
+ // Start chaining a wrapped Underscore object.
+ chain: function() {
+ this._chain = true;
+ return this;
+ },
+
+ // Extracts the result from a wrapped and chained object.
+ value: function() {
+ return this._wrapped;
+ }
+
+ });
}).call(this);
// Backbone.js 0.9.2
@@ -6574,7 +6715,7 @@ kb.Store = (function() {
Store.prototype.findOrReplace = function(obj, creator, observable) {
var index, record;
- obj || raiseUnexpected('obj missing');
+ obj || _throwUnexpected(this, 'obj missing');
if ((index = this.findIndex(obj, creator)) < 0) {
return this.register(obj, observable, {
creator: creator
View
40 knockback-core-stack.min.js
@@ -4,39 +4,11 @@
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Dependencies: Knockout.js, Backbone.js, and Underscore.js.
*/
-// Underscore.js 1.3.3
-// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
-// Underscore is freely distributable under the MIT license.
-// Portions of Underscore are inspired or borrowed from Prototype,
-// Oliver Steele's Functional, and John Resig's Micro-Templating.
-// For all details and documentation:
-// http://documentcloud.github.com/underscore
-(function(){function r(a,c,d){if(a===c)return 0!==a||1/a==1/c;if(null==a||null==c)return a===c;a._chain&&(a=a._wrapped);c._chain&&(c=c._wrapped);if(a.isEqual&&b.isFunction(a.isEqual))return a.isEqual(c);if(c.isEqual&&b.isFunction(c.isEqual))return c.isEqual(a);var e=l.call(a);if(e!=l.call(c))return!1;switch(e){case "[object String]":return a==""+c;case "[object Number]":return a!=+a?c!=+c:0==a?1/a==1/c:a==+c;case "[object Date]":case "[object Boolean]":return+a==+c;case "[object RegExp]":return a.source==
-c.source&&a.global==c.global&&a.multiline==c.multiline&&a.ignoreCase==c.ignoreCase}if("object"!=typeof a||"object"!=typeof c)return!1;for(var f=d.length;f--;)if(d[f]==a)return!0;d.push(a);var f=0,g=!0;if("[object Array]"==e){if(f=a.length,g=f==c.length)for(;f--&&(g=f in a==f in c&&r(a[f],c[f],d)););}else{if("constructor"in a!="constructor"in c||a.constructor!=c.constructor)return!1;for(var h in a)if(b.has(a,h)&&(f++,!(g=b.has(c,h)&&r(a[h],c[h],d))))break;if(g){for(h in c)if(b.has(c,h)&&!f--)break;
-g=!f}}d.pop();return g}var s=this,I=s._,o={},k=Array.prototype,p=Object.prototype,i=k.slice,J=k.unshift,l=p.toString,K=p.hasOwnProperty,y=k.forEach,z=k.map,A=k.reduce,B=k.reduceRight,C=k.filter,D=k.every,E=k.some,q=k.indexOf,F=k.lastIndexOf,p=Array.isArray,L=Object.keys,t=Function.prototype.bind,b=function(a){return new m(a)};"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports=module.exports=b),exports._=b):s._=b;b.VERSION="1.3.3";var j=b.each=b.forEach=function(a,
-c,d){if(a!=null)if(y&&a.forEach===y)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,f=a.length;e<f;e++){if(e in a&&c.call(d,a[e],e,a)===o)break}else for(e in a)if(b.has(a,e)&&c.call(d,a[e],e,a)===o)break};b.map=b.collect=function(a,c,b){var e=[];if(a==null)return e;if(z&&a.map===z)return a.map(c,b);j(a,function(a,g,h){e[e.length]=c.call(b,a,g,h)});if(a.length===+a.length)e.length=a.length;return e};b.reduce=b.foldl=b.inject=function(a,c,d,e){var f=arguments.length>2;a==null&&(a=[]);if(A&&
-a.reduce===A){e&&(c=b.bind(c,e));return f?a.reduce(c,d):a.reduce(c)}j(a,function(a,b,i){if(f)d=c.call(e,d,a,b,i);else{d=a;f=true}});if(!f)throw new TypeError("Reduce of empty array with no initial value");return d};b.reduceRight=b.foldr=function(a,c,d,e){var f=arguments.length>2;a==null&&(a=[]);if(B&&a.reduceRight===B){e&&(c=b.bind(c,e));return f?a.reduceRight(c,d):a.reduceRight(c)}var g=b.toArray(a).reverse();e&&!f&&(c=b.bind(c,e));return f?b.reduce(g,c,d,e):b.reduce(g,c)};b.find=b.detect=function(a,
-c,b){var e;G(a,function(a,g,h){if(c.call(b,a,g,h)){e=a;return true}});return e};b.filter=b.select=function(a,c,b){var e=[];if(a==null)return e;if(C&&a.filter===C)return a.filter(c,b);j(a,function(a,g,h){c.call(b,a,g,h)&&(e[e.length]=a)});return e};b.reject=function(a,c,b){var e=[];if(a==null)return e;j(a,function(a,g,h){c.call(b,a,g,h)||(e[e.length]=a)});return e};b.every=b.all=function(a,c,b){var e=true;if(a==null)return e;if(D&&a.every===D)return a.every(c,b);j(a,function(a,g,h){if(!(e=e&&c.call(b,
-a,g,h)))return o});return!!e};var G=b.some=b.any=function(a,c,d){c||(c=b.identity);var e=false;if(a==null)return e;if(E&&a.some===E)return a.some(c,d);j(a,function(a,b,h){if(e||(e=c.call(d,a,b,h)))return o});return!!e};b.include=b.contains=function(a,c){var b=false;if(a==null)return b;if(q&&a.indexOf===q)return a.indexOf(c)!=-1;return b=G(a,function(a){return a===c})};b.invoke=function(a,c){var d=i.call(arguments,2);return b.map(a,function(a){return(b.isFunction(c)?c||a:a[c]).apply(a,d)})};b.pluck=
-function(a,c){return b.map(a,function(a){return a[c]})};b.max=function(a,c,d){if(!c&&b.isArray(a)&&a[0]===+a[0])return Math.max.apply(Math,a);if(!c&&b.isEmpty(a))return-Infinity;var e={computed:-Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b>=e.computed&&(e={value:a,computed:b})});return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a)&&a[0]===+a[0])return Math.min.apply(Math,a);if(!c&&b.isEmpty(a))return Infinity;var e={computed:Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b<e.computed&&
-(e={value:a,computed:b})});return e.value};b.shuffle=function(a){var b=[],d;j(a,function(a,f){d=Math.floor(Math.random()*(f+1));b[f]=b[d];b[d]=a});return b};b.sortBy=function(a,c,d){var e=b.isFunction(c)?c:function(a){return a[c]};return b.pluck(b.map(a,function(a,b,c){return{value:a,criteria:e.call(d,a,b,c)}}).sort(function(a,b){var c=a.criteria,d=b.criteria;return c===void 0?1:d===void 0?-1:c<d?-1:c>d?1:0}),"value")};b.groupBy=function(a,c){var d={},e=b.isFunction(c)?c:function(a){return a[c]};
-j(a,function(a,b){var c=e(a,b);(d[c]||(d[c]=[])).push(a)});return d};b.sortedIndex=function(a,c,d){d||(d=b.identity);for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(c)?e=g+1:f=g}return e};b.toArray=function(a){return!a?[]:b.isArray(a)||b.isArguments(a)?i.call(a):a.toArray&&b.isFunction(a.toArray)?a.toArray():b.values(a)};b.size=function(a){return b.isArray(a)?a.length:b.keys(a).length};b.first=b.head=b.take=function(a,b,d){return b!=null&&!d?i.call(a,0,b):a[0]};b.initial=function(a,b,d){return i.call(a,
-0,a.length-(b==null||d?1:b))};b.last=function(a,b,d){return b!=null&&!d?i.call(a,Math.max(a.length-b,0)):a[a.length-1]};b.rest=b.tail=function(a,b,d){return i.call(a,b==null||d?1:b)};b.compact=function(a){return b.filter(a,function(a){return!!a})};b.flatten=function(a,c){return b.reduce(a,function(a,e){if(b.isArray(e))return a.concat(c?e:b.flatten(e));a[a.length]=e;return a},[])};b.without=function(a){return b.difference(a,i.call(arguments,1))};b.uniq=b.unique=function(a,c,d){var d=d?b.map(a,d):a,
-e=[];a.length<3&&(c=true);b.reduce(d,function(d,g,h){if(c?b.last(d)!==g||!d.length:!b.include(d,g)){d.push(g);e.push(a[h])}return d},[]);return e};b.union=function(){return b.uniq(b.flatten(arguments,true))};b.intersection=b.intersect=function(a){var c=i.call(arguments,1);return b.filter(b.uniq(a),function(a){return b.every(c,function(c){return b.indexOf(c,a)>=0})})};b.difference=function(a){var c=b.flatten(i.call(arguments,1),true);return b.filter(a,function(a){return!b.include(c,a)})};b.zip=function(){for(var a=
-i.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c),e=0;e<c;e++)d[e]=b.pluck(a,""+e);return d};b.indexOf=function(a,c,d){if(a==null)return-1;var e;if(d){d=b.sortedIndex(a,c);return a[d]===c?d:-1}if(q&&a.indexOf===q)return a.indexOf(c);d=0;for(e=a.length;d<e;d++)if(d in a&&a[d]===c)return d;return-1};b.lastIndexOf=function(a,b){if(a==null)return-1;if(F&&a.lastIndexOf===F)return a.lastIndexOf(b);for(var d=a.length;d--;)if(d in a&&a[d]===b)return d;return-1};b.range=function(a,b,d){if(arguments.length<=
-1){b=a||0;a=0}for(var d=arguments[2]||1,e=Math.max(Math.ceil((b-a)/d),0),f=0,g=Array(e);f<e;){g[f++]=a;a=a+d}return g};var H=function(){};b.bind=function(a,c){var d,e;if(a.bind===t&&t)return t.apply(a,i.call(arguments,1));if(!b.isFunction(a))throw new TypeError;e=i.call(arguments,2);return d=function(){if(!(this instanceof d))return a.apply(c,e.concat(i.call(arguments)));H.prototype=a.prototype;var b=new H,g=a.apply(b,e.concat(i.call(arguments)));return Object(g)===g?g:b}};b.bindAll=function(a){var c=
-i.call(arguments,1);c.length==0&&(c=b.functions(a));j(c,function(c){a[c]=b.bind(a[c],a)});return a};b.memoize=function(a,c){var d={};c||(c=b.identity);return function(){var e=c.apply(this,arguments);return b.has(d,e)?d[e]:d[e]=a.apply(this,arguments)}};b.delay=function(a,b){var d=i.call(arguments,2);return setTimeout(function(){return a.apply(null,d)},b)};b.defer=function(a){return b.delay.apply(b,[a,1].concat(i.call(arguments,1)))};b.throttle=function(a,c){var d,e,f,g,h,i,j=b.debounce(function(){h=
-g=false},c);return function(){d=this;e=arguments;f||(f=setTimeout(function(){f=null;h&&a.apply(d,e);j()},c));g?h=true:i=a.apply(d,e);j();g=true;return i}};b.debounce=function(a,b,d){var e;return function(){var f=this,g=arguments;d&&!e&&a.apply(f,g);clearTimeout(e);e=setTimeout(function(){e=null;d||a.apply(f,g)},b)}};b.once=function(a){var b=false,d;return function(){if(b)return d;b=true;return d=a.apply(this,arguments)}};b.wrap=function(a,b){return function(){var d=[a].concat(i.call(arguments,0));
-return b.apply(this,d)}};b.compose=function(){var a=arguments;return function(){for(var b=arguments,d=a.length-1;d>=0;d--)b=[a[d].apply(this,b)];return b[0]}};b.after=function(a,b){return a<=0?b():function(){if(--a<1)return b.apply(this,arguments)}};b.keys=L||function(a){if(a!==Object(a))throw new TypeError("Invalid object");var c=[],d;for(d in a)b.has(a,d)&&(c[c.length]=d);return c};b.values=function(a){return b.map(a,b.identity)};b.functions=b.methods=function(a){var c=[],d;for(d in a)b.isFunction(a[d])&&
-c.push(d);return c.sort()};b.extend=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});return a};b.pick=function(a){var c={};j(b.flatten(i.call(arguments,1)),function(b){b in a&&(c[b]=a[b])});return c};b.defaults=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]==null&&(a[d]=b[d])});return a};b.clone=function(a){return!b.isObject(a)?a:b.isArray(a)?a.slice():b.extend({},a)};b.tap=function(a,b){b(a);return a};b.isEqual=function(a,b){return r(a,b,[])};b.isEmpty=
-function(a){if(a==null)return true;if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(b.has(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=p||function(a){return l.call(a)=="[object Array]"};b.isObject=function(a){return a===Object(a)};b.isArguments=function(a){return l.call(a)=="[object Arguments]"};b.isArguments(arguments)||(b.isArguments=function(a){return!(!a||!b.has(a,"callee"))});b.isFunction=function(a){return l.call(a)=="[object Function]"};
-b.isString=function(a){return l.call(a)=="[object String]"};b.isNumber=function(a){return l.call(a)=="[object Number]"};b.isFinite=function(a){return b.isNumber(a)&&isFinite(a)};b.isNaN=function(a){return a!==a};b.isBoolean=function(a){return a===true||a===false||l.call(a)=="[object Boolean]"};b.isDate=function(a){return l.call(a)=="[object Date]"};b.isRegExp=function(a){return l.call(a)=="[object RegExp]"};b.isNull=function(a){return a===null};b.isUndefined=function(a){return a===void 0};b.has=function(a,
-b){return K.call(a,b)};b.noConflict=function(){s._=I;return this};b.identity=function(a){return a};b.times=function(a,b,d){for(var e=0;e<a;e++)b.call(d,e)};b.escape=function(a){return(""+a).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;")};b.result=function(a,c){if(a==null)return null;var d=a[c];return b.isFunction(d)?d.call(a):d};b.mixin=function(a){j(b.functions(a),function(c){M(c,b[c]=a[c])})};var N=0;b.uniqueId=
-function(a){var b=N++;return a?a+b:b};b.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var u=/.^/,n={"\\":"\\","'":"'",r:"\r",n:"\n",t:"\t",u2028:"\u2028",u2029:"\u2029"},v;for(v in n)n[n[v]]=v;var O=/\\|'|\r|\n|\t|\u2028|\u2029/g,P=/\\(\\|'|r|n|t|u2028|u2029)/g,w=function(a){return a.replace(P,function(a,b){return n[b]})};b.template=function(a,c,d){d=b.defaults(d||{},b.templateSettings);a="__p+='"+a.replace(O,function(a){return"\\"+n[a]}).replace(d.escape||
-u,function(a,b){return"'+\n_.escape("+w(b)+")+\n'"}).replace(d.interpolate||u,function(a,b){return"'+\n("+w(b)+")+\n'"}).replace(d.evaluate||u,function(a,b){return"';\n"+w(b)+"\n;__p+='"})+"';\n";d.variable||(a="with(obj||{}){\n"+a+"}\n");var a="var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};\n"+a+"return __p;\n",e=new Function(d.variable||"obj","_",a);if(c)return e(c,b);c=function(a){return e.call(this,a,b)};c.source="function("+(d.variable||"obj")+"){\n"+a+"}";return c};
-b.chain=function(a){return b(a).chain()};var m=function(a){this._wrapped=a};b.prototype=m.prototype;var x=function(a,c){return c?b(a).chain():a},M=function(a,c){m.prototype[a]=function(){var a=i.call(arguments);J.call(a,this._wrapped);return x(c.apply(b,a),this._chain)}};b.mixin(b);j("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=k[a];m.prototype[a]=function(){var d=this._wrapped;b.apply(d,arguments);var e=d.length;(a=="shift"||a=="splice")&&e===0&&delete d[0];return x(d,
-this._chain)}});j(["concat","join","slice"],function(a){var b=k[a];m.prototype[a]=function(){return x(b.apply(this._wrapped,arguments),this._chain)}});m.prototype.chain=function(){this._chain=true;return this};m.prototype.value=function(){return this._wrapped}}).call(this);
-// Backbone.js 0.9.2
+// Underscore.js 1.4.2
+// http://underscorejs.org
+// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
+// Underscore may be freely distributed under the MIT license.
+(function(){var e=this,t=e._,n={},r=Array.prototype,i=Object.prototype,s=Function.prototype,o=r.push,u=r.slice,a=r.concat,f=r.unshift,l=i.toString,c=i.hasOwnProperty,h=r.forEach,p=r.map,d=r.reduce,v=r.reduceRight,m=r.filter,g=r.every,y=r.some,b=r.indexOf,w=r.lastIndexOf,E=Array.isArray,S=Object.keys,x=s.bind,T=function(e){if(e instanceof T)return e;if(!(this instanceof T))return new T(e);this._wrapped=e};typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(exports=module.exports=T),exports._=T):e._=T,T.VERSION="1.4.2";var N=T.each=T.forEach=function(e,t,r){if(e==null)return;if(h&&e.forEach===h)e.forEach(t,r);else if(e.length===+e.length){for(var i=0,s=e.length;i<s;i++)if(t.call(r,e[i],i,e)===n)return}else for(var o in e)if(T.has(e,o)&&t.call(r,e[o],o,e)===n)return};T.map=T.collect=function(e,t,n){var r=[];return e==null?r:p&&e.map===p?e.map(t,n):(N(e,function(e,i,s){r[r.length]=t.call(n,e,i,s)}),r)},T.reduce=T.foldl=T.inject=function(e,t,n,r){var i=arguments.length>2;e==null&&(e=[]);if(d&&e.reduce===d)return r&&(t=T.bind(t,r)),i?e.reduce(t,n):e.reduce(t);N(e,function(e,s,o){i?n=t.call(r,n,e,s,o):(n=e,i=!0)});if(!i)throw new TypeError("Reduce of empty array with no initial value");return n},T.reduceRight=T.foldr=function(e,t,n,r){var i=arguments.length>2;e==null&&(e=[]);if(v&&e.reduceRight===v)return r&&(t=T.bind(t,r)),arguments.length>2?e.reduceRight(t,n):e.reduceRight(t);var s=e.length;if(s!==+s){var o=T.keys(e);s=o.length}N(e,function(u,a,f){a=o?o[--s]:--s,i?n=t.call(r,n,e[a],a,f):(n=e[a],i=!0)});if(!i)throw new TypeError("Reduce of empty array with no initial value");return n},T.find=T.detect=function(e,t,n){var r;return C(e,function(e,i,s){if(t.call(n,e,i,s))return r=e,!0}),r},T.filter=T.select=function(e,t,n){var r=[];return e==null?r:m&&e.filter===m?e.filter(t,n):(N(e,function(e,i,s){t.call(n,e,i,s)&&(r[r.length]=e)}),r)},T.reject=function(e,t,n){var r=[];return e==null?r:(N(e,function(e,i,s){t.call(n,e,i,s)||(r[r.length]=e)}),r)},T.every=T.all=function(e,t,r){t||(t=T.identity);var i=!0;return e==null?i:g&&e.every===g?e.every(t,r):(N(e,function(e,s,o){if(!(i=i&&t.call(r,e,s,o)))return n}),!!i)};var C=T.some=T.any=function(e,t,r){t||(t=T.identity);var i=!1;return e==null?i:y&&e.some===y?e.some(t,r):(N(e,function(e,s,o){if(i||(i=t.call(r,e,s,o)))return n}),!!i)};T.contains=T.include=function(e,t){var n=!1;return e==null?n:b&&e.indexOf===b?e.indexOf(t)!=-1:(n=C(e,function(e){return e===t}),n)},T.invoke=function(e,t){var n=u.call(arguments,2);return T.map(e,function(e){return(T.isFunction(t)?t:e[t]).apply(e,n)})},T.pluck=function(e,t){return T.map(e,function(e){return e[t]})},T.where=function(e,t){return T.isEmpty(t)?[]:T.filter(e,function(e){for(var n in t)if(t[n]!==e[n])return!1;return!0})},T.max=function(e,t,n){if(!t&&T.isArray(e)&&e[0]===+e[0]&&e.length<65535)return Math.max.apply(Math,e);if(!t&&T.isEmpty(e))return-Infinity;var r={computed:-Infinity};return N(e,function(e,i,s){var o=t?t.call(n,e,i,s):e;o>=r.computed&&(r={value:e,computed:o})}),r.value},T.min=function(e,t,n){if(!t&&T.isArray(e)&&e[0]===+e[0]&&e.length<65535)return Math.min.apply(Math,e);if(!t&&T.isEmpty(e))return Infinity;var r={computed:Infinity};return N(e,function(e,i,s){var o=t?t.call(n,e,i,s):e;o<r.computed&&(r={value:e,computed:o})}),r.value},T.shuffle=function(e){var t,n=0,r=[];return N(e,function(e){t=T.random(n++),r[n-1]=r[t],r[t]=e}),r};var k=function(e){return T.isFunction(e)?e:function(t){return t[e]}};T.sortBy=function(e,t,n){var r=k(t);return T.pluck(T.map(e,function(e,t,i){return{value:e,index:t,criteria:r.call(n,e,t,i)}}).sort(function(e,t){var n=e.criteria,r=t.criteria;if(n!==r){if(n>r||n===void 0)return 1;if(n<r||r===void 0)return-1}return e.index<t.index?-1:1}),"value")};var L=function(e,t,n,r){var i={},s=k(t);return N(e,function(t,o){var u=s.call(n,t,o,e);r(i,u,t)}),i};T.groupBy=function(e,t,n){return L(e,t,n,function(e,t,n){(T.has(e,t)?e[t]:e[t]=[]).push(n)})},T.countBy=function(e,t,n){return L(e,t,n,function(e,t,n){T.has(e,t)||(e[t]=0),e[t]++})},T.sortedIndex=function(e,t,n,r){n=n==null?T.identity:k(n);var i=n.call(r,t),s=0,o=e.length;while(s<o){var u=s+o>>>1;n.call(r,e[u])<i?s=u+1:o=u}return s},T.toArray=function(e){return e?e.length===+e.length?u.call(e):T.values(e):[]},T.size=function(e){return e.length===+e.length?e.length:T.keys(e).length},T.first=T.head=T.take=function(e,t,n){return t!=null&&!n?u.call(e,0,t):e[0]},T.initial=function(e,t,n){return u.call(e,0,e.length-(t==null||n?1:t))},T.last=function(e,t,n){return t!=null&&!n?u.call(e,Math.max(e.length-t,0)):e[e.length-1]},T.rest=T.tail=T.drop=function(e,t,n){return u.call(e,t==null||n?1:t)},T.compact=function(e){return T.filter(e,function(e){return!!e})};var A=function(e,t,n){return N(e,function(e){T.isArray(e)?t?o.apply(n,e):A(e,t,n):n.push(e)}),n};T.flatten=function(e,t){return A(e,t,[])},T.without=function(e){return T.difference(e,u.call(arguments,1))},T.uniq=T.unique=function(e,t,n,r){var i=n?T.map(e,n,r):e,s=[],o=[];return N(i,function(n,r){if(t?!r||o[o.length-1]!==n:!T.contains(o,n))o.push(n),s.push(e[r])}),s},T.union=function(){return T.uniq(a.apply(r,arguments))},T.intersection=function(e){var t=u.call(arguments,1);return T.filter(T.uniq(e),function(e){return T.every(t,function(t){return T.indexOf(t,e)>=0})})},T.difference=function(e){var t=a.apply(r,u.call(arguments,1));return T.filter(e,function(e){return!T.contains(t,e)})},T.zip=function(){var e=u.call(arguments),t=T.max(T.pluck(e,"length")),n=new Array(t);for(var r=0;r<t;r++)n[r]=T.pluck(e,""+r);return n},T.object=function(e,t){var n={};for(var r=0,i=e.length;r<i;r++)t?n[e[r]]=t[r]:n[e[r][0]]=e[r][1];return n},T.indexOf=function(e,t,n){if(e==null)return-1;var r=0,i=e.length;if(n){if(typeof n!="number")return r=T.sortedIndex(e,t),e[r]===t?r:-1;r=n<0?Math.max(0,i+n):n}if(b&&e.indexOf===b)return e.indexOf(t,n);for(;r<i;r++)if(e[r]===t)return r;return-1},T.lastIndexOf=function(e,t,n){if(e==null)return-1;var r=n!=null;if(w&&e.lastIndexOf===w)return r?e.lastIndexOf(t,n):e.lastIndexOf(t);var i=r?n:e.length;while(i--)if(e[i]===t)return i;return-1},T.range=function(e,t,n){arguments.length<=1&&(t=e||0,e=0),n=arguments[2]||1;var r=Math.max(Math.ceil((t-e)/n),0),i=0,s=new Array(r);while(i<r)s[i++]=e,e+=n;return s};var O=function(){};T.bind=function(t,n){var r,i;if(t.bind===x&&x)return x.apply(t,u.call(arguments,1));if(!T.isFunction(t))throw new TypeError;return i=u.call(arguments,2),r=function(){if(this instanceof r){O.prototype=t.prototype;var e=new O,s=t.apply(e,i.concat(u.call(arguments)));return Object(s)===s?s:e}return t.apply(n,i.concat(u.call(arguments)))}},T.bindAll=function(e){var t=u.call(arguments,1);return t.length==0&&(t=T.functions(e)),N(t,function(t){e[t]=T.bind(e[t],e)}),e},T.memoize=function(e,t){var n={};return t||(t=T.identity),function(){var r=t.apply(this,arguments);return T.has(n,r)?n[r]:n[r]=e.apply(this,arguments)}},T.delay=function(e,t){var n=u.call(arguments,2);return setTimeout(function(){return e.apply(null,n)},t)},T.defer=function(e){return T.delay.apply(T,[e,1].concat(u.call(arguments,1)))},T.throttle=function(e,t){var n,r,i,s,o,u,a=T.debounce(function(){o=s=!1},t);return function(){n=this,r=arguments;var f=function(){i=null,o&&(u=e.apply(n,r)),a()};return i||(i=setTimeout(f,t)),s?o=!0:(s=!0,u=e.apply(n,r)),a(),u}},T.debounce=function(e,t,n){var r,i;return function(){var s=this,o=arguments,u=function(){r=null,n||(i=e.apply(s,o))},a=n&&!r;return clearTimeout(r),r=setTimeout(u,t),a&&(i=e.apply(s,o)),i}},T.once=function(e){var t=!1,n;return function(){return t?n:(t=!0,n=e.apply(this,arguments),e=null,n)}},T.wrap=function(e,t){return function(){var n=[e];return o.apply(n,arguments),t.apply(this,n)}},T.compose=function(){var e=arguments;return function(){var t=arguments;for(var n=e.length-1;n>=0;n--)t=[e[n].apply(this,t)];return t[0]}},T.after=function(e,t){return e<=0?t():function(){if(--e<1)return t.apply(this,arguments)}},T.keys=S||function(e){if(e!==Object(e))throw new TypeError("Invalid object");var t=[];for(var n in e)T.has(e,n)&&(t[t.length]=n);return t},T.values=function(e){var t=[];for(var n in e)T.has(e,n)&&t.push(e[n]);return t},T.pairs=function(e){var t=[];for(var n in e)T.has(e,n)&&t.push([n,e[n]]);return t},T.invert=function(e){var t={};for(var n in e)T.has(e,n)&&(t[e[n]]=n);return t},T.functions=T.methods=function(e){var t=[];for(var n in e)T.isFunction(e[n])&&t.push(n);return t.sort()},T.extend=function(e){return N(u.call(arguments,1),function(t){for(var n in t)e[n]=t[n]}),e},T.pick=function(e){var t={},n=a.apply(r,u.call(arguments,1));return N(n,function(n){n in e&&(t[n]=e[n])}),t},T.omit=function(e){var t={},n=a.apply(r,u.call(arguments,1));for(var i in e)T.contains(n,i)||(t[i]=e[i]);return t},T.defaults=function(e){return N(u.call(arguments,1),function(t){for(var n in t)e[n]==null&&(e[n]=t[n])}),e},T.clone=function(e){return T.isObject(e)?T.isArray(e)?e.slice():T.extend({},e):e},T.tap=function(e,t){return t(e),e};var M=function(e,t,n,r){if(e===t)return e!==0||1/e==1/t;if(e==null||t==null)return e===t;e instanceof T&&(e=e._wrapped),t instanceof T&&(t=t._wrapped);var i=l.call(e);if(i!=l.call(t))return!1;switch(i){case"[object String]":return e==String(t);case"[object Number]":return e!=+e?t!=+t:e==0?1/e==1/t:e==+t;case"[object Date]":case"[object Boolean]":return+e==+t;case"[object RegExp]":return e.source==t.source&&e.global==t.global&&e.multiline==t.multiline&&e.ignoreCase==t.ignoreCase}if(typeof e!="object"||typeof t!="object")return!1;var s=n.length;while(s--)if(n[s]==e)return r[s]==t;n.push(e),r.push(t);var o=0,u=!0;if(i=="[object Array]"){o=e.length,u=o==t.length;if(u)while(o--)if(!(u=M(e[o],t[o],n,r)))break}else{var a=e.constructor,f=t.constructor;if(a!==f&&!(T.isFunction(a)&&a instanceof a&&T.isFunction(f)&&f instanceof f))return!1;for(var c in e)if(T.has(e,c)){o++;if(!(u=T.has(t,c)&&M(e[c],t[c],n,r)))break}if(u){for(c in t)if(T.has(t,c)&&!(o--))break;u=!o}}return n.pop(),r.pop(),u};T.isEqual=function(e,t){return M(e,t,[],[])},T.isEmpty=function(e){if(e==null)return!0;if(T.isArray(e)||T.isString(e))return e.length===0;for(var t in e)if(T.has(e,t))return!1;return!0},T.isElement=function(e){return!!e&&e.nodeType===1},T.isArray=E||function(e){return l.call(e)=="[object Array]"},T.isObject=function(e){return e===Object(e)},N(["Arguments","Function","String","Number","Date","RegExp"],function(e){T["is"+e]=function(t){return l.call(t)=="[object "+e+"]"}}),T.isArguments(arguments)||(T.isArguments=function(e){return!!e&&!!T.has(e,"callee")}),typeof /./!="function"&&(T.isFunction=function(e){return typeof e=="function"}),T.isFinite=function(e){return T.isNumber(e)&&isFinite(e)},T.isNaN=function(e){return T.isNumber(e)&&e!=+e},T.isBoolean=function(e){return e===!0||e===!1||l.call(e)=="[object Boolean]"},T.isNull=function(e){return e===null},T.isUndefined=function(e){return e===void 0},T.has=function(e,t){return c.call(e,t)},T.noConflict=function(){return e._=t,this},T.identity=function(e){return e},T.times=function(e,t,n){for(var r=0;r<e;r++)t.call(n,r)},T.random=function(e,t){return t==null&&(t=e,e=0),e+(0|Math.random()*(t-e+1))};var _={escape:{"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","/":"&#x2F;"}};_.unescape=T.invert(_.escape);var D={escape:new RegExp("["+T.keys(_.escape).join("")+"]","g"),unescape:new RegExp("("+T.keys(_.unescape).join("|")+")","g")};T.each(["escape","unescape"],function(e){T[e]=function(t){return t==null?"":(""+t).replace(D[e],function(t){return _[e][t]})}}),T.result=function(e,t){if(e==null)return null;var n=e[t];return T.isFunction(n)?n.call(e):n},T.mixin=function(e){N(T.functions(e),function(t){var n=T[t]=e[t];T.prototype[t]=function(){var e=[this._wrapped];return o.apply(e,arguments),F.call(this,n.apply(T,e))}})};var P=0;T.uniqueId=function(e){var t=P++;return e?e+t:t},T.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var H=/(.)^/,B={"'":"'","\\":"\\","\r":"r","\n":"n"," ":"t","\u2028":"u2028","\u2029":"u2029"},j=/\\|'|\r|\n|\t|\u2028|\u2029/g;T.template=function(e,t,n){n=T.defaults({},n,T.templateSettings);var r=new RegExp([(n.escape||H).source,(n.interpolate||H).source,(n.evaluate||H).source].join("|")+"|$","g"),i=0,s="__p+='";e.replace(r,function(t,n,r,o,u){s+=e.slice(i,u).replace(j,function(e){return"\\"+B[e]}),s+=n?"'+\n((__t=("+n+"))==null?'':_.escape(__t))+\n'":r?"'+\n((__t=("+r+"))==null?'':__t)+\n'":o?"';\n"+o+"\n__p+='":"",i=u+t.length}),s+="';\n",n.variable||(s="with(obj||{}){\n"+s+"}\n"),s="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+s+"return __p;\n";try{var o=new Function(n.variable||"obj","_",s)}catch(u){throw u.source=s,u}if(t)return o(t,T);var a=function(e){return o.call(this,e,T)};return a.source="function("+(n.variable||"obj")+"){\n"+s+"}",a},T.chain=function(e){return T(e).chain()};var F=function(e){return this._chain?T(e).chain():e};T.mixin(T),N(["pop","push","reverse","shift","sort","splice","unshift"],function(e){var t=r[e];T.prototype[e]=function(){var n=this._wrapped;return t.apply(n,arguments),(e=="shift"||e=="splice")&&n.length===0&&delete n[0],F.call(this,n)}}),N(["concat","join","slice"],function(e){var t=r[e];T.prototype[e]=function(){return F.call(this,t.apply(this._wrapped,arguments))}}),T.extend(T.prototype,{chain:function(){return this._chain=!0,this},value:function(){return this._wrapped}})}).call(this);// Backbone.js 0.9.2
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
@@ -165,4 +137,4 @@ e||(e=b.text()||"",e=jQuery.template(s,"{{ko_with $item.koBindingContext}}"+e+"{
(c) 2011, 2012 Kevin Malakoff - http://kmalakoff.github.com/knockback/
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Dependencies: Knockout.js, Backbone.js, and Underscore.js.
-*/(function(){return function(e){return typeof define=="function"&&define.amd?define("knockback",["underscore","backbone","knockout"],e):e.call(this)}(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x=function(e,t){return function(){return e.apply(t,arguments)}};c=function(){function t(){}return t.VERSION="0.16.7",t.TYPE_UNKNOWN=0,t.TYPE_SIMPLE=1,t.TYPE_ARRAY=2,t.TYPE_MODEL=3,t.TYPE_COLLECTION=4,t.release=function(n,r){var i,o,u,a,f,l,c,p;if(!n||n!==Object(n)||typeof n=="function"&&!h.isObservable(n)||n.__kb_destroyed||n instanceof e.Model||n instanceof e.Collection)return this;if(d.isArray(n)){i=n.splice(0,n.length);for(f=0,c=i.length;f<c;f++)o=i[f],t.release(o);return this}n.__kb_destroyed=!0,!r||r();if(h.isObservable(n)||typeof n.dispose=="function"||typeof n.destroy=="function"||typeof n.release=="function")if(h.isObservable(n)&&d.isArray(i=n())){if(n.__kb_is_co||n.__kb_is_o&&n.valueType()===s)n.destroy?n.destroy():n.dispose&&n.dispose();else if(i.length){a=i.slice(0),i.splice(0,i.length);for(l=0,p=a.length;l<p;l++)u=a[l],t.release(u)}}else n.release?n.release():n.destroy?n.destroy():n.dispose&&n.dispose();else this.releaseKeys(n);return this},t.releaseKeys=function(e){var n,r;for(n in e)r=e[n],n==="__kb"||t.release(r,function(){return e[n]=null});return this},t.releaseOnNodeRemove=function(e,n){return e||b(this,"missing view model"),n||b(this,"missing node"),h.utils.domNodeDisposal.addDisposeCallback(n,function(){return t.release(e)})},t.renderTemplate=function(e,n,r){var i,s;return r==null&&(r={}),i=document.createElement("div"),s=h.renderTemplate(e,n,r,i,"replaceChildren"),i.children.length===1&&(i=i.children[0]),t.releaseOnNodeRemove(n,i),s.dispose(),i},t.renderAutoReleasedTemplate=function(e,t,n){return n==null&&(n={}),g("kb.renderAutoReleasedTemplate","0.16.3","Please use kb.renderTemplate instead"),this.renderTemplate(e,t,n={})},t.applyBindings=function(e,n){return h.applyBindings(e,n),t.releaseOnNodeRemove(e,n)},t}(),this.Knockback=this.kb=c,typeof exports!="undefined"&&(module.exports=c);if(!this._&&typeof require!="undefined")try{d=require("lodash")}catch(T){d=require("underscore")}else d=this._;return c._=d=d.hasOwnProperty("_")?d._:d,c.Backbone=e=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,c.ko=h=!this.ko&&typeof require!="undefined"?require("knockout"):this.ko,y=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is missing"},b=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is unexpected"},g=function(e,t,n){var r;return this._legacy_warnings||(this._legacy_warnings={}),(r=this._legacy_warnings)[e]||(r[e]=0),this._legacy_warnings[e]++,console.warn("warning: '"+e+"' has been deprecated (will be removed in Knockback after "+t+"). "+n+".")},m=Array.prototype.splice,E=h.utils.unwrapObservable,l=function(e){var t;t=d.clone(e);while(e.options)d.defaults(t,e.options),e=e.options;return delete t.options,t},a=c.TYPE_UNKNOWN,u=c.TYPE_SIMPLE,i=c.TYPE_ARRAY,o=c.TYPE_MODEL,s=c.TYPE_COLLECTION,S=function(e,t,n){return arguments.length===2?e&&e.__kb&&e.__kb.hasOwnProperty(t)?e.__kb[t]:void 0:(e||b(this,"no obj for wrapping "+t),e.__kb||(e.__kb={}),e.__kb[t]=n,n)},v=function(e,t){return m.call(e,1,0,t),e},w=function(e){var t,n,r;if(!e)return e;if(e.__kb)return"object"in e.__kb?e.__kb.object:e;if(d.isArray(e))return d.map(e,function(e){return w(e)});if(d.isObject(e)&&e.constructor==={}.constructor){n={};for(t in e)r=e[t],n[t]=w(r);return n}return e},c.utils=function(){function t(){}return t.wrappedObservable=function(e,t){return S.apply(this,v(arguments,"observable"))},t.wrappedObject=function(e,t){return S.apply(this,v(arguments,"object"))},t.wrappedModel=function(e,t){return arguments.length===1?(t=S(e,"object"),d.isUndefined(t)?e:t):S(e,"object",t)},t.wrappedStore=function(e,t){return S.apply(this,v(arguments,"store"))},t.wrappedStoreIsOwned=function(e,t){return S.apply(this,v(arguments,"store_is_owned"))},t.wrappedFactory=function(e,t){return S.apply(this,v(arguments,"factory"))},t.wrappedEventWatcher=function(e,t){return S.apply(this,v(arguments,"event_watcher"))},t.wrappedEventWatcherIsOwned=function(e,t){return S.apply(this,v(arguments,"event_watcher_is_owned"))},t.wrappedDestroy=function(e){var t;if(!e.__kb)return;return e.__kb.event_watcher&&e.__kb.event_watcher.releaseCallbacks(e),t=e.__kb,e.__kb=null,t.observable&&(t.observable.destroy=t.observable.release=null,this.wrappedDestroy(t.observable),t.observable=null),t.factory=null,t.event_watcher_is_owned&&t.event_watcher.destroy(),t.event_watcher=null,t.store_is_owned&&t.store.destroy(),t.store=null},t.valueType=function(t){return t?t.__kb_is_o?t.valueType():t.__kb_is_co||t instanceof e.Collection?s:t instanceof c.ViewModel||t instanceof e.Model?o:d.isArray(t)?i:u:a},t.pathJoin=function(e,t){return(e?e[e.length-1]!=="."?""+e+".":e:"")+t},t.optionsPathJoin=function(e,t){return d.defaults({path:this.pathJoin(e.path,t)},e)},t.inferCreator=function(t,n,r,i,s){var o,u;n&&(o=n.creatorForPath(t,r));if(o)return o;if(i&&e.RelationalModel&&i instanceof e.RelationalModel){s=E(s),u=d.find(i.getRelations(),function(e){return e.key===s});if(u)return u.collectionType||d.isArray(u.keyContents)?c.CollectionObservable:c.ViewModel}return t?t instanceof e.Model?c.ViewModel:t instanceof e.Collection?c.CollectionObservable:null:null},t.createFromDefaultCreator=function(t,n){return t instanceof e.Model?c.viewModel(t,n):t instanceof e.Collection?c.collectionObservable(t,n):d.isArray(t)?h.observableArray(t):h.observable(t)},t.hasModelSignature=function(e){return e&&e.attributes&&!e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.hasCollectionSignature=function(e){return e&&e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.release=function(e){return g("kb.utils.release","0.16.0","Please use kb.release instead"),c.release(e)},t}(),c.Factory=function(){function e(e){this.parent_factory=e,this.paths={}}return e.useOptionsOrCreate=function(e,t,n){var r;return e.factory&&(!e.factories||e.factories&&e.factory.hasPathMappings(e.factories,n))?c.utils.wrappedFactory(t,e.factory):(r=c.utils.wrappedFactory(t,new c.Factory(e.factory)),e.factories&&r.addPathMappings(e.factories,n),r)},e.prototype.hasPath=function(e){return this.paths.hasOwnProperty(e)||this.parent_factory&&this.parent_factory.hasPath(e)},e.prototype.addPathMapping=function(e,t){return this.paths[e]=t},e.prototype.addPathMappings=function(e,t){var n,r;for(r in e)n=e[r],this.paths[c.utils.pathJoin(t,r)]=n},e.prototype.hasPathMappings=function(e,t){var n,r,i,s;n=!0;for(s in e)r=e[s],n&=(i=this.creatorForPath(null,c.utils.pathJoin(t,s)))&&r===i;return n},e.prototype.creatorForPath=function(e,t){var n;if(n=this.paths[t])return n.view_model?n.view_model:n;if(this.parent_factory)if(n=this.parent_factory.creatorForPath(e,t))return n;return null},e}(),c.Store=function(){function t(){this.observable_records=[],this.replaced_observables=[]}return t.useOptionsOrCreate=function(e,t,n){return e.store?(e.store.register(t,n,e),c.utils.wrappedStore(n,e.store)):(c.utils.wrappedStoreIsOwned(n,!0),c.utils.wrappedStore(n,new c.Store))},t.prototype.destroy=function(){return this.clear()},t.prototype.clear=function(){var e,t,n,r;r=this.observable_records.splice(0,this.observable_records.length);for(t=0,n=r.length;t<n;t++)e=r[t],c.release(e.observable);c.release(this.replaced_observables)},t.prototype.register=function(e,t,n){var r;if(!t)return;if(h.isObservable(t)||t.__kb_is_co)return;return c.utils.wrappedObject(t,e),e||(t.__kb_null=!0),r=n.creator?n.creator:n.path&&n.factory?n.factory.creatorForPath(e,n.path):null,r||(r=t.constructor),this.observable_records.push({obj:e,observable:t,creator:r}),t},t.prototype.findIndex=function(t,n){var r,i,s;if(!t||t instanceof e.Model){s=this.observable_records;for(r in s){i=s[r];if(!i.observable)continue;if(i.observable.__kb_destroyed){i.obj=null,i.observable=null;continue}if(!t&&!i.observable.__kb_null||t&&(i.observable.__kb_null||i.obj!==t))continue;if(i.creator===n||i.creator.create&&i.creator.create===n.create)return r}}return-1},t.prototype.find=function(e,t){var n;return(n=this.findIndex(e,t))<0?null:this.observable_records[n].observable},t.prototype.isRegistered=function(e){var t,n,r,i;i=this.observable_records;for(n=0,r=i.length;n<r;n++){t=i[n];if(t.observable===e)return!0}return!1},t.prototype.findOrCreate=function(t,n){var r,i;return n.store=this,n.creator||(n.creator=c.utils.inferCreator(t,n.factory,n.path)),!n.creator&&t instanceof e.Model&&(n.creator=kv.ViewModel),r=n.creator,r?r.models_only?t:(r&&(i=this.find(t,r)),i?i:(r.create?i=r.create(t,n):i=new r(t,n),i||(i=h.observable(null)),h.isObservable(i)||this.isRegistered(i)||this.register(t,i,n),i)):c.utils.createFromDefaultCreator(t,n)},t.prototype.findOrReplace=function(e,t,n){var r,i;return e||raiseUnexpected("obj missing"),(r=this.findIndex(e,t))<0?this.register(e,n,{creator:t}):(i=this.observable_records[r],c.utils.wrappedObject(i.observable)===e||b(this,"different object"),i.observable!==n&&(i.observable.constructor===n.constructor||b(this,"replacing different type"),this.replaced_observables.push(i.observable),i.observable=n),n)},t}(),f=function(e,t,n){return!c.statistics||c.statistics.addModelEvent({name:t,emitter:e,key:n.key,path:n.path})},c.EventWatcher=function(){function t(e,t,n){this._onModelUnloaded=x(this._onModelUnloaded,this),this._onModelLoaded=x(this._onModelLoaded,this),this.__kb||(this.__kb={}),this.__kb.callbacks={},this.__kb._onModelLoaded=d.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=d.bind(this._onModelUnloaded,this),n&&this.registerCallbacks(t,n),e?this.emitter(e):this.ee=null}return t.useOptionsOrCreate=function(e,t,n,r){return e.event_watcher?(e.event_watcher.emitter()!==t&&e.event_watcher.model_ref!==t&&b(this,"emitter not matching"),c.utils.wrappedEventWatcher(n,e.event_watcher).registerCallbacks(n,r)):(c.utils.wrappedEventWatcherIsOwned(n,!0),c.utils.wrappedEventWatcher(n,new c.EventWatcher(t)).registerCallbacks(n,r))},t.prototype.destroy=function(){return this.emitter(null),this.__kb.callbacks=null,c.utils.wrappedDestroy(this)},t.prototype.emitter=function(t){var n,r,i,s,o,u,a,f;if(arguments.length===0||this.ee===t)return this.ee;this.model_ref&&(this.model_ref.unbind("loaded",this.__kb._onModelLoaded),this.model_ref.unbind("unloaded",this.__kb._onModelUnloaded),this.model_ref.release(),this.model_ref=null),e.ModelRef&&t instanceof e.ModelRef?(this.model_ref=t,this.model_ref.retain(),this.model_ref.bind("loaded",this.__kb._onModelLoaded),this.model_ref.bind("unloaded",this.__kb._onModelUnloaded),t=this.model_ref.model()):delete this.model_ref,o=this.ee,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],o&&o.unbind(r,n.fn),t&&this.ee.bind(r,n.fn),s=n.list;for(u=0,a=s.length;u<a;u++)i=s[u],i.emitter&&i.emitter(this.ee)}return t},t.prototype.registerCallbacks=function(t,n){var r,i,s,o,u,a,l,h,p=this;t||y(this,"obj"),n||y(this,"info"),o=n.event_selector?n.event_selector:"change",s=o.split(" ");for(l=0,h=s.length;l<h;l++){i=s[l];if(!i)continue;r=this.__kb.callbacks[i],r||(a=[],r={list:a,fn:function(e){var t,n,r;for(n=0,r=a.length;n<r;n++){t=a[n];if(t.update&&!t.rel_fn){if(e&&t.key&&e.hasChanged&&!e.hasChanged(E(t.key)))continue;!c.statistics||f(e,i,t),t.update()}}return null}},this.__kb.callbacks[i]=r,this.ee&&this.ee.bind(i,r.fn)),u=d.defaults({obj:t},n),r.list.push(u)}this.ee&&(e.RelationalModel&&this.ee instanceof e.RelationalModel&&d.contains(s,"change")&&this._modelBindRelatationalInfo("change",u),u.emitter(this.ee)&&u.emitter)},t.prototype.releaseCallbacks=function(e){var t,n,r,i,s,o;if(!this.__kb.callbacks)return;s=this.__kb.callbacks;for(n in s){t=s[n],o=t.list;for(r in o){i=o[r];if(i.obj!==e)continue;t.list.splice(r,1),i.rel_fn&&this._modelUnbindRelatationalInfo(n,i),i.emitter&&i.emitter(null);return}}},t.prototype._onModelLoaded=function(t){var n,r,i,s,o,u,a,f;s=e.RelationalModel&&t instanceof e.RelationalModel,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],this.ee.bind(r,n.fn),o=n.list;for(u=0,a=o.length;u<a;u++)i=o[u],s&&this._modelBindRelatationalInfo(r,i),i.emitter&&i.emitter(this.ee)}},t.prototype._onModelUnloaded=function(e){var t,n,r,i,s,o,u;this.ee=null,u=this.__kb.callbacks;for(n in u){t=u[n],e.unbind(n,t.fn),i=t.list;for(s=0,o=i.length;s<o;s++)r=i[s],r.rel_fn&&this._modelUnbindRelatationalInfo(n,r),r.emitter&&r.emitter(null)}},t.prototype._modelBindRelatationalInfo=function(e,t){var n,r;if(e==="change"&&t.key&&t.update){n=E(t.key),r=d.find(this.ee.getRelations(),function(e){return e.key===n});if(!r)return;t.rel_fn=function(n){return!c.statistics||f(n,""+e+" (relational)",t),t.update()},r.collectionType||d.isArray(r.keyContents)?(t.is_collection=!0,this.ee.bind("add:"+t.key,t.rel_fn),this.ee.bind("remove:"+t.key,t.rel_fn)):this.ee.bind("update:"+t.key,t.rel_fn)}},t.prototype._modelUnbindRelatationalInfo=function(e,t){if(!t.rel_fn)return;t.is_collection?(this.ee.unbind("add:"+t.key,t.rel_fn),this.ee.unbind("remove:"+t.key,t.rel_fn)):this.ee.unbind("update:"+t.key,t.rel_fn),t.rel_fn=null},t}(),c.emitterObservable=function(e,t){return new c.EventWatcher(e,t)},c.Observable=function(){function e(e,t,n){var r,i,s,o=this;return this.vm=n,t||y(this,"options"),this.vm||(this.vm={}),d.isString(t)||h.isObservable(t)?r=this.create_options={key:t}:r=this.create_options=l(t),this.key=r.key,delete r.key,this.key||y(this,"key"),!r.args||(this.args=r.args,delete r.args),!r.read||(this.read=r.read,delete r.read),!r.write||(this.write=r.write,delete r.write),i=r.event_watcher,delete r.event_watcher,this.vo=h.observable(null),this._model=h.observable(),s=c.utils.wrappedObservable(this,h.dependentObservable({read:function(){var e,t,n,r,i,s;t=[E(o.key)];if(o.args)if(d.isArray(o.args)){s=o.args;for(r=0,i=s.length;r<i;r++)e=s[r],t.push(E(e))}else t.push(E(o.args));return o._mdl===o._model()&&o._mdl&&(n=o.read?o.read.apply(o.vm,t):o._mdl.get.apply(o._mdl,t),o.update(n)),E(o.vo())},write:function(e){var t,n,r,i,s,u,a;i=w(e),r={},r[E(o.key)]=i,n=o.write?[i]:[r];if(o.args)if(d.isArray(o.args)){a=o.args;for(s=0,u=a.length;s<u;s++)t=a[s],n.push(E(t))}else n.push(E(o.args));return o._mdl&&(o.write?o.write.apply(o.vm,n):o._mdl.set.apply(o._mdl,n)),o.update(e)},owner:this.vm})),s.__kb_is_o=!0,r.store=c.utils.wrappedStore(s,r.store),r.path=c.utils.pathJoin(r.path,this.key),r.factories&&(typeof r.factories=="function"||r.factories.create)?(r.factory=c.utils.wrappedFactory(s,new c.Factory(r.factory)),r.factory.addPathMapping(r.path,r.factories)):r.factory=c.Factory.useOptionsOrCreate(r,s,r.path),delete r.factories,s.value=d.bind(this.value,this),s.valueType=d.bind(this.valueType,this),s.destroy=d.bind(this.destroy,this),s.model=this.model=h.dependentObservable({read:function(){return o._model(),o._mdl},write:function(e){if(o.__kb_destroyed||o._mdl===e)return;return o._mdl=e,o.update(null),o._model(e)}}),c.EventWatcher.useOptionsOrCreate({event_watcher:i},e,this,{emitter:this.model,update:d.bind(this.update,this),key:this.key,path:r.path}),this.__kb_value||this.update(),c.LocalizedObservable&&r.localizer&&(s=new r.localizer(s),delete r.localizer),c.DefaultObservable&&r.hasOwnProperty("default")&&(s=c.defaultObservable(s,r["default"]),delete r["default"]),s}return e.prototype.destroy=function(){var e;return e=c.utils.wrappedObservable(this),this.__kb_destroyed=!0,c.release(this.__kb_value),this.__kb_value=null,this.model.dispose(),this._mdl=this.model=e.model=null,c.utils.wrappedDestroy(this)},e.prototype.value=function(){return this.__kb_value},e.prototype.valueType=function(){var e;return e=this._mdl?this._mdl.get(this.key):null,this.value_type||this._updateValueObservable(e),this.value_type},e.prototype.update=function(e){var t,n;if(this.__kb_destroyed)return;this._mdl&&!arguments.length&&(e=this._mdl.get(E(this.key))),e!==void 0||(e=null),t=c.utils.valueType(e);if(!this.__kb_value||this.__kb_value.__kb_destroyed||this.__kb_value.__kb_null&&e)this.__kb_value=void 0,this.value_type=void 0;n=this.__kb_value;if(d.isUndefined(this.value_type)||this.value_type!==t&&t!==a)return this.value_type===s&&t===i?n(e):this._updateValueObservable(e);if(this.value_type===o){if(typeof n.model=="function"){if(n.model()!==e)return n.model(e)}else if(c.utils.wrappedObject(n)!==e)return this._updateValueObservable(e)}else if(this.value_type===s){if(n.collection()!==e)return n.collection(e)}else if(n()!==e)return n(e)},e.prototype._updateValueObservable=function(e){var t,n,r,f;return t=this.create_options,t.creator=c.utils.inferCreator(e,t.factory,t.path,this._mdl,this.key),this.value_type=a,n=t.creator,r=this.__kb_value,this.__kb_value=void 0,r&&c.release(r),n?t.store?f=t.store.findOrCreate(e,t):n.models_only?(f=e,this.value_type=u):n.create?f=n.create(e,t):f=new n(e,t):d.isArray(e)?(this.value_type=i,f=h.observableArray(e)):(this.value_type=u,f=h.observable(e)),this.value_type===a&&(h.isObservable(f)?f.__kb_is_co?this.value_type=s:this.value_type=u:(this.value_type=o,typeof f.model!="function"&&c.utils.wrappedObject(f,e))),this.__kb_value=f,this.vo(f)},e}(),c.observable=function(e,t,n){return new c.Observable(e,t,n)},c.ViewModel=function(){function t(t,n,r){var i,s,o,u,a,f,p,v,m,g=this;!t||t instanceof e.Model||typeof t.get=="function"&&typeof t.bind=="function"||b(this,"not a model"),n||(n={}),r||(r={}),d.isArray(n)?n={keys:n}:n=l(n),this.__kb||(this.__kb={}),this.__kb.vm_keys={},this.__kb.model_keys={},this.__kb.view_model=d.isUndefined(r)?this:r,!n.internals||(this.__kb.internals=n.internals),!n.excludes||(this.__kb.excludes=n.excludes),c.Store.useOptionsOrCreate(n,t,this),this.__kb.path=n.path,c.Factory.useOptionsOrCreate(n,this,n.path),v=S(this,"_mdl",h.observable()),this.model=h.dependentObservable({read:function(){return v(),c.utils.wrappedObject(g)},write:function(e){var t,n;if(c.utils.wrappedObject(g)===e)return;if(g.__kb_null){!e||b(g,"model set on shared null");return}c.utils.wrappedObject(g,e),t=c.utils.wrappedEventWatcher(g);if(!t){v(e);return}t.emitter(e),g.__kb.keys||!e||!e.attributes||(n=d.difference(d.keys(e.attributes),d.keys(g.__kb.model_keys)),n&&g._createObservables(e,n)),v(e)}}),o=c.utils.wrappedEventWatcher(this,new c.EventWatcher(t,this,{emitter:this.model})),n.requires&&d.isArray(n.requires)&&(u=d.clone(n.requires)),this.__kb.internals&&(u=u?d.union(u,this.__kb.internals):d.clone(this.__kb.internals));if(n.keys)if(d.isArray(n.keys))this.__kb.keys=n.keys,u=u?d.union(u,n.keys):d.clone(n.keys);else{a={},m=n.keys;for(p in m)f=m[p],a[d.isString(f)?f:f.key?f.key:p]=!0;this.__kb.keys=d.keys(a)}else s=o.emitter(),s&&s.attributes&&(i=d.keys(s.attributes),u=u?d.union(u,i):i);u&&this.__kb.excludes&&(u=d.difference(u,this.__kb.excludes)),d.isObject(n.keys)&&!d.isArray(n.keys)&&this._mapObservables(t,n.keys),d.isObject(n.requires)&&!d.isArray(n.requires)&&this._mapObservables(t,n.requires),!n.mappings||this._mapObservables(t,n.mappings),!u||this._createObservables(t,u),!c.statistics||c.statistics.register("ViewModel",this)}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e;if(this.__kb.view_model!==this)for(e in this.__kb.vm_keys)this.__kb.view_model[e]=null;return this.__kb.view_model=null,c.releaseKeys(this),c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("ViewModel",this)},t.prototype.shareOptions=function(){return{store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this)}},t.prototype._createObservables=function(e,t){var n,r,i,s,o;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(s=0,o=t.length;s<o;s++){r=t[s],i=this.__kb.internals&&d.contains(this.__kb.internals,r)?"_"+r:r;if(this[i])continue;this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r]=!0,n.key=r,this[i]=this.__kb.view_model[i]=c.observable(e,n,this)}},t.prototype._mapObservables=function(e,t){var n,r,i;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(i in t){r=t[i];if(this[i])continue;r=d.isString(r)?{key:r}:d.clone(r),r.key||(r.key=i),this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r.key]=!0,this[i]=this.__kb.view_model[i]=c.observable(e,d.defaults(r,n),this)}},t}(),c.viewModel=function(e,t,n){return new c.ViewModel(e,t,n)},c.observables=function(e,t,n){return g("kb.observables","0.16.0","Please use kb.viewModel instead"),new c.ViewModel(e,t,n)},r=0,t=-1,n=1,c.compare=function(e,i){return d.isString(e)?e.localeCompare(i):d.isString(i)?i.localeCompare(e):typeof e!="object"?e===i?r:e<i?t:n:e===i?r:e<i?t:n},c.CollectionObservable=function(){function t(t,n){var r,i,s=this;return!t||t instanceof e.Collection||b(this,"not a collection"),n||(n={}),i=c.utils.wrappedObservable(this,h.observableArray([])),i.__kb_is_co=!0,this.in_edit=0,this.__kb||(this.__kb={}),this.__kb._onCollectionChange=d.bind(this._onCollectionChange,this),n=l(n),n.sort_attribute?this._comparator=h.observable(this._attributeComparator(n.sort_attribute)):(n.sorted_index&&g("sortedIndex no longer supported","0.16.7","please use comparator instead"),this._comparator=h.observable(n.comparator)),n.filters?this._filters=h.observableArray(d.isArray(n.filters)?n.filters:n.filters?[n.filters]:void 0):this._filters=h.observableArray([]),r=this.create_options={store:c.Store.useOptionsOrCreate(n,t,i)},this.path=n.path,r.factory=c.utils.wrappedFactory(i,this._shareOrCreateFactory(n)),r.path=c.utils.pathJoin(n.path,"models"),r.creator=r.factory.creatorForPath(null,r.path),r.creator&&(this.models_only=r.creator.models_only),i.destroy=d.bind(this.destroy,this),i.shareOptions=d.bind(this.shareOptions,this),i.filters=d.bind(this.filters,this),i.comparator=d.bind(this.comparator,this),i.sortAttribute=d.bind(this.sortAttribute,this),i.viewModelByModel=d.bind(this.viewModelByModel,this),i.hasViewModels=d.bind(this.hasViewModels,this),this._collection=h.observable(t),i.collection=this.collection=h.dependentObservable({read:function(){return s._collection()},write:function(e){var t;if((t=s._collection())===e)return;return t&&t.unbind("all",s.__kb._onCollectionChange),e&&e.bind("all",s.__kb._onCollectionChange),s._collection(e)}}),t&&t.bind("all",this.__kb._onCollectionChange),this._mapper=h.dependentObservable(function(){var e,t,n,r,o;e=s._comparator(),n=s._filters(),t=s._collection();if(s.in_edit)return;return i=c.utils.wrappedObservable(s),t&&(r=t.models),!r||t.models.length===0?o=[]:(n.length&&(r=d.filter(r,function(e){return!s._modelIsFiltered(e)})),e?o=d.map(r,function(e){return s._createViewModel(e)}).sort(e):s.models_only?o=n.length?r:r.slice():o=d.map(r,function(e){return s._createViewModel(e)})),s.in_edit++,i(o),s.in_edit--}),i.subscribe(d.bind(this._onObservableArrayChange,this)),!c.statistics||c.statistics.register("CollectionObservable",this),i}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e,t,n;return n=c.utils.wrappedObservable(this),t=this._collection(),t&&(t.unbind("all",this.__kb._onCollectionChange),e=n(),e.splice(0,e.length)),this._mapper.dispose(),this._mapper=null,c.release(this._filters),this._comparator(null),this.collection.dispose(),n.collection=this.collection=null,n.collection=null,c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("CollectionObservable",this)},t.prototype.shareOptions=function(){var e;return e=c.utils.wrappedObservable(this),{store:c.utils.wrappedStore(e),factory:c.utils.wrappedFactory(e)}},t.prototype.filters=function(e){return e?this._filters(d.isArray(e)?e:[e]):this._filters([])},t.prototype.comparator=function(e){return this._comparator(e)},t.prototype.sortedIndex=function(){return g("sortedIndex no longer supported","0.16.7","please use comparator instead")},t.prototype.sortAttribute=function(e){return this._comparator(e?this._attributeComparator(e):null)},t.prototype.viewModelByModel=function(e){var t;return this.models_only?null:(t=e.hasOwnProperty(e.idAttribute)?e.idAttribute:"cid",d.find(c.utils.wrappedObservable(this)(),function(n){return n.__kb.object[t]===e[t]}))},t.prototype.hasViewModels=function(){return!this.models_only},t.prototype._shareOrCreateFactory=function(e){var t,n,r,i;t=c.utils.pathJoin(e.path,"models"),r=e.factories;if(i=e.factory)if((n=i.creatorForPath(null,t))&&(!r||r.models===n)){if(!r)return i;if(i.hasPathMappings(r,e.path))return i}return i=new c.Factory(e.factory),r&&i.addPathMappings(r,e.path),i.creatorForPath(null,t)||(e.hasOwnProperty("models_only")?e.models_only?i.addPathMapping(t,{models_only:!0}):i.addPathMapping(t,c.ViewModel):e.view_model?i.addPathMapping(t,e.view_model):e.create?i.addPathMapping(t,{create:e.create}):i.addPathMapping(t,c.ViewModel)),i},t.prototype._onCollectionChange=function(e,t){var n,r,i,s;if(this.in_edit)return;switch(e){case"reset":case"resort":this._collection.notifySubscribers(this._collection());break;case"new":case"add":if(this._modelIsFiltered(t))return;i=c.utils.wrappedObservable(this),n=this._collection();if(s=this.viewModelByModel(t))return;s=this._createViewModel(t),this.in_edit++,(r=this._comparator())?(i().push(s),i.sort(r)):i.splice(n.indexOf(t),0,s),this.in_edit--;break;case"remove":case"destroy":this._onModelRemove(t);break;case"change":if(this._modelIsFiltered(t))this._onModelRemove(t);else{s=this.viewModelByModel(t);if(s){if(r=this._comparator())i=c.utils.wrappedObservable(this),this.in_edit++,i.sort(r),this.in_edit--}else this._onCollectionChange("add",t)}}},t.prototype._onModelRemove=function(e){var t,n;n=this.models_only?e:this.viewModelByModel(e);if(!n)return;return t=c.utils.wrappedObservable(this),this.in_edit++,t.remove(n),this.in_edit--},t.prototype._onObservableArrayChange=function(e){var t,n,r,i,s,o,u,a,f,l=this;if(this.in_edit)return;this.models_only&&(!e.length||c.utils.hasModelSignature(e[0]))||!this.models_only&&(!e.length||d.isObject(e[0])&&!c.utils.hasModelSignature(e[0]))||b(this,"incorrect type passed"),s=c.utils.wrappedObservable(this),t=this._collection(),n=this._filters().length;if(!t)return;u=e;if(this.models_only)n&&(i=d.filter(e,function(e){return!l._modelIsFiltered(e)}));else{!n||(u=[]),i=[];for(a=0,f=e.length;a<f;a++){o=e[a],r=c.utils.wrappedObject(o);if(n){if(this._modelIsFiltered(r))continue;u.push(o)}this.create_options.store.findOrReplace(r,this.create_options.creator,o),i.push(r)}}this.in_edit++,e.length===u.length||s(u),d.isEqual(t.models,i)||t.reset(i),this.in_edit--},t.prototype._attributeComparator=function(e){var t;return t=function(t,n){var r;return r=E(e),c.compare(t.get(r),n.get(r))},this.models_only?t:function(e,n){return t(c.utils.wrappedModel(e),c.utils.wrappedModel(n))}},t.prototype._createViewModel=function(e){return this.models_only?e:this.create_options.store.findOrCreate(e,this.create_options)},t.prototype._modelIsFiltered=function(e){var t,n,r,i;n=this._filters();for(r=0,i=n.length;r<i;r++){t=n[r],t=E(t);if(typeof t=="function"&&t(e)||e&&e.id===t)return!0}return!1},t}(),c.collectionObservable=function(e,t){return new c.CollectionObservable(e,t)},h.bindingHandlers.inject={init:function(e,t,n,r){return c.Inject.inject(E(t()),r,e,t,n)}},c.Inject=function(){function e(){}return e.inject=function(e,t,n,r,i,s){var o,u,a;return o=function(e){var o,u,a;if(d.isFunction(e))t=new e(t,n,r,i),c.releaseOnNodeRemove(t,n);else{e.view_model&&(t=new e.view_model(t,n,r,i),c.releaseOnNodeRemove(t,n));for(o in e){a=e[o];if(o==="view_model")continue;o==="create"?a(t,n,r,i):d.isObject(a)&&!d.isFunction(a)?(u=s||a&&a.create?{}:t,t[o]=c.Inject.inject(a,u,n,r,i,!0)):t[o]=a}}return t},s?o(e):(u=(a=h.dependentObservable(function(){return o(e)}))(),a.dispose(),u)},e.injectViewModels=function(e){var t,n,r,i,s,o,u,a,f,l;a=[],o=function(e){var t,n,r,i,s;e.__kb_injected||e.attributes&&(t=d.find(e.attributes,function(e){return e.name==="kb-inject"}))&&(e.__kb_injected=!0,a.push({el:e,view_model:{},binding:t.value})),s=e.childNodes;for(r=0,i=s.length;r<i;r++)n=s[r],o(n)},o(e||document);for(f=0,l=a.length;f<l;f++){n=a[f];if(s=n.binding)s.search(/[:]/)<0||(s="{"+s+"}"),i=(new Function("","return ( "+s+" )"))(),i||(i={}),!i.options||(u=i.options,delete i.options),u||(u={}),n.view_model=c.Inject.inject(i,n.view_model,n.el,null,null,!0),t=n.view_model.afterBinding||u.afterBinding,r=n.view_model.beforeBinding||u.beforeBinding;r&&r(n.view_model,n.el,u),c.applyBindings(n.view_model,n.el,u),t&&t(n.view_model,n.el,u)}return a},e}(),c.injectViewModels=c.Inject.injectViewModels,this.$?this.$(function(){return c.injectViewModels()}):(p=function(){return document.readyState!=="complete"?setTimeout(p,0):c.injectViewModels()})(),c})}).call(this);
+*/(function(){return function(e){return typeof define=="function"&&define.amd?define("knockback",["underscore","backbone","knockout"],e):e.call(this)}(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x=function(e,t){return function(){return e.apply(t,arguments)}};c=function(){function t(){}return t.VERSION="0.16.7",t.TYPE_UNKNOWN=0,t.TYPE_SIMPLE=1,t.TYPE_ARRAY=2,t.TYPE_MODEL=3,t.TYPE_COLLECTION=4,t.release=function(n,r){var i,o,u,a,f,l,c,p;if(!n||n!==Object(n)||typeof n=="function"&&!h.isObservable(n)||n.__kb_destroyed||n instanceof e.Model||n instanceof e.Collection)return this;if(d.isArray(n)){i=n.splice(0,n.length);for(f=0,c=i.length;f<c;f++)o=i[f],t.release(o);return this}n.__kb_destroyed=!0,!r||r();if(h.isObservable(n)||typeof n.dispose=="function"||typeof n.destroy=="function"||typeof n.release=="function")if(h.isObservable(n)&&d.isArray(i=n())){if(n.__kb_is_co||n.__kb_is_o&&n.valueType()===s)n.destroy?n.destroy():n.dispose&&n.dispose();else if(i.length){a=i.slice(0),i.splice(0,i.length);for(l=0,p=a.length;l<p;l++)u=a[l],t.release(u)}}else n.release?n.release():n.destroy?n.destroy():n.dispose&&n.dispose();else this.releaseKeys(n);return this},t.releaseKeys=function(e){var n,r;for(n in e)r=e[n],n==="__kb"||t.release(r,function(){return e[n]=null});return this},t.releaseOnNodeRemove=function(e,n){return e||b(this,"missing view model"),n||b(this,"missing node"),h.utils.domNodeDisposal.addDisposeCallback(n,function(){return t.release(e)})},t.renderTemplate=function(e,n,r){var i,s;return r==null&&(r={}),i=document.createElement("div"),s=h.renderTemplate(e,n,r,i,"replaceChildren"),i.children.length===1&&(i=i.children[0]),t.releaseOnNodeRemove(n,i),s.dispose(),i},t.renderAutoReleasedTemplate=function(e,t,n){return n==null&&(n={}),g("kb.renderAutoReleasedTemplate","0.16.3","Please use kb.renderTemplate instead"),this.renderTemplate(e,t,n={})},t.applyBindings=function(e,n){return h.applyBindings(e,n),t.releaseOnNodeRemove(e,n)},t}(),this.Knockback=this.kb=c,typeof exports!="undefined"&&(module.exports=c);if(!this._&&typeof require!="undefined")try{d=require("lodash")}catch(T){d=require("underscore")}else d=this._;return c._=d=d.hasOwnProperty("_")?d._:d,c.Backbone=e=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,c.ko=h=!this.ko&&typeof require!="undefined"?require("knockout"):this.ko,y=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is missing"},b=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is unexpected"},g=function(e,t,n){var r;return this._legacy_warnings||(this._legacy_warnings={}),(r=this._legacy_warnings)[e]||(r[e]=0),this._legacy_warnings[e]++,console.warn("warning: '"+e+"' has been deprecated (will be removed in Knockback after "+t+"). "+n+".")},m=Array.prototype.splice,E=h.utils.unwrapObservable,l=function(e){var t;t=d.clone(e);while(e.options)d.defaults(t,e.options),e=e.options;return delete t.options,t},a=c.TYPE_UNKNOWN,u=c.TYPE_SIMPLE,i=c.TYPE_ARRAY,o=c.TYPE_MODEL,s=c.TYPE_COLLECTION,S=function(e,t,n){return arguments.length===2?e&&e.__kb&&e.__kb.hasOwnProperty(t)?e.__kb[t]:void 0:(e||b(this,"no obj for wrapping "+t),e.__kb||(e.__kb={}),e.__kb[t]=n,n)},v=function(e,t){return m.call(e,1,0,t),e},w=function(e){var t,n,r;if(!e)return e;if(e.__kb)return"object"in e.__kb?e.__kb.object:e;if(d.isArray(e))return d.map(e,function(e){return w(e)});if(d.isObject(e)&&e.constructor==={}.constructor){n={};for(t in e)r=e[t],n[t]=w(r);return n}return e},c.utils=function(){function t(){}return t.wrappedObservable=function(e,t){return S.apply(this,v(arguments,"observable"))},t.wrappedObject=function(e,t){return S.apply(this,v(arguments,"object"))},t.wrappedModel=function(e,t){return arguments.length===1?(t=S(e,"object"),d.isUndefined(t)?e:t):S(e,"object",t)},t.wrappedStore=function(e,t){return S.apply(this,v(arguments,"store"))},t.wrappedStoreIsOwned=function(e,t){return S.apply(this,v(arguments,"store_is_owned"))},t.wrappedFactory=function(e,t){return S.apply(this,v(arguments,"factory"))},t.wrappedEventWatcher=function(e,t){return S.apply(this,v(arguments,"event_watcher"))},t.wrappedEventWatcherIsOwned=function(e,t){return S.apply(this,v(arguments,"event_watcher_is_owned"))},t.wrappedDestroy=function(e){var t;if(!e.__kb)return;return e.__kb.event_watcher&&e.__kb.event_watcher.releaseCallbacks(e),t=e.__kb,e.__kb=null,t.observable&&(t.observable.destroy=t.observable.release=null,this.wrappedDestroy(t.observable),t.observable=null),t.factory=null,t.event_watcher_is_owned&&t.event_watcher.destroy(),t.event_watcher=null,t.store_is_owned&&t.store.destroy(),t.store=null},t.valueType=function(t){return t?t.__kb_is_o?t.valueType():t.__kb_is_co||t instanceof e.Collection?s:t instanceof c.ViewModel||t instanceof e.Model?o:d.isArray(t)?i:u:a},t.pathJoin=function(e,t){return(e?e[e.length-1]!=="."?""+e+".":e:"")+t},t.optionsPathJoin=function(e,t){return d.defaults({path:this.pathJoin(e.path,t)},e)},t.inferCreator=function(t,n,r,i,s){var o,u;n&&(o=n.creatorForPath(t,r));if(o)return o;if(i&&e.RelationalModel&&i instanceof e.RelationalModel){s=E(s),u=d.find(i.getRelations(),function(e){return e.key===s});if(u)return u.collectionType||d.isArray(u.keyContents)?c.CollectionObservable:c.ViewModel}return t?t instanceof e.Model?c.ViewModel:t instanceof e.Collection?c.CollectionObservable:null:null},t.createFromDefaultCreator=function(t,n){return t instanceof e.Model?c.viewModel(t,n):t instanceof e.Collection?c.collectionObservable(t,n):d.isArray(t)?h.observableArray(t):h.observable(t)},t.hasModelSignature=function(e){return e&&e.attributes&&!e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.hasCollectionSignature=function(e){return e&&e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.release=function(e){return g("kb.utils.release","0.16.0","Please use kb.release instead"),c.release(e)},t}(),c.Factory=function(){function e(e){this.parent_factory=e,this.paths={}}return e.useOptionsOrCreate=function(e,t,n){var r;return e.factory&&(!e.factories||e.factories&&e.factory.hasPathMappings(e.factories,n))?c.utils.wrappedFactory(t,e.factory):(r=c.utils.wrappedFactory(t,new c.Factory(e.factory)),e.factories&&r.addPathMappings(e.factories,n),r)},e.prototype.hasPath=function(e){return this.paths.hasOwnProperty(e)||this.parent_factory&&this.parent_factory.hasPath(e)},e.prototype.addPathMapping=function(e,t){return this.paths[e]=t},e.prototype.addPathMappings=function(e,t){var n,r;for(r in e)n=e[r],this.paths[c.utils.pathJoin(t,r)]=n},e.prototype.hasPathMappings=function(e,t){var n,r,i,s;n=!0;for(s in e)r=e[s],n&=(i=this.creatorForPath(null,c.utils.pathJoin(t,s)))&&r===i;return n},e.prototype.creatorForPath=function(e,t){var n;if(n=this.paths[t])return n.view_model?n.view_model:n;if(this.parent_factory)if(n=this.parent_factory.creatorForPath(e,t))return n;return null},e}(),c.Store=function(){function t(){this.observable_records=[],this.replaced_observables=[]}return t.useOptionsOrCreate=function(e,t,n){return e.store?(e.store.register(t,n,e),c.utils.wrappedStore(n,e.store)):(c.utils.wrappedStoreIsOwned(n,!0),c.utils.wrappedStore(n,new c.Store))},t.prototype.destroy=function(){return this.clear()},t.prototype.clear=function(){var e,t,n,r;r=this.observable_records.splice(0,this.observable_records.length);for(t=0,n=r.length;t<n;t++)e=r[t],c.release(e.observable);c.release(this.replaced_observables)},t.prototype.register=function(e,t,n){var r;if(!t)return;if(h.isObservable(t)||t.__kb_is_co)return;return c.utils.wrappedObject(t,e),e||(t.__kb_null=!0),r=n.creator?n.creator:n.path&&n.factory?n.factory.creatorForPath(e,n.path):null,r||(r=t.constructor),this.observable_records.push({obj:e,observable:t,creator:r}),t},t.prototype.findIndex=function(t,n){var r,i,s;if(!t||t instanceof e.Model){s=this.observable_records;for(r in s){i=s[r];if(!i.observable)continue;if(i.observable.__kb_destroyed){i.obj=null,i.observable=null;continue}if(!t&&!i.observable.__kb_null||t&&(i.observable.__kb_null||i.obj!==t))continue;if(i.creator===n||i.creator.create&&i.creator.create===n.create)return r}}return-1},t.prototype.find=function(e,t){var n;return(n=this.findIndex(e,t))<0?null:this.observable_records[n].observable},t.prototype.isRegistered=function(e){var t,n,r,i;i=this.observable_records;for(n=0,r=i.length;n<r;n++){t=i[n];if(t.observable===e)return!0}return!1},t.prototype.findOrCreate=function(t,n){var r,i;return n.store=this,n.creator||(n.creator=c.utils.inferCreator(t,n.factory,n.path)),!n.creator&&t instanceof e.Model&&(n.creator=kv.ViewModel),r=n.creator,r?r.models_only?t:(r&&(i=this.find(t,r)),i?i:(r.create?i=r.create(t,n):i=new r(t,n),i||(i=h.observable(null)),h.isObservable(i)||this.isRegistered(i)||this.register(t,i,n),i)):c.utils.createFromDefaultCreator(t,n)},t.prototype.findOrReplace=function(e,t,n){var r,i;return e||b(this,"obj missing"),(r=this.findIndex(e,t))<0?this.register(e,n,{creator:t}):(i=this.observable_records[r],c.utils.wrappedObject(i.observable)===e||b(this,"different object"),i.observable!==n&&(i.observable.constructor===n.constructor||b(this,"replacing different type"),this.replaced_observables.push(i.observable),i.observable=n),n)},t}(),f=function(e,t,n){return!c.statistics||c.statistics.addModelEvent({name:t,emitter:e,key:n.key,path:n.path})},c.EventWatcher=function(){function t(e,t,n){this._onModelUnloaded=x(this._onModelUnloaded,this),this._onModelLoaded=x(this._onModelLoaded,this),this.__kb||(this.__kb={}),this.__kb.callbacks={},this.__kb._onModelLoaded=d.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=d.bind(this._onModelUnloaded,this),n&&this.registerCallbacks(t,n),e?this.emitter(e):this.ee=null}return t.useOptionsOrCreate=function(e,t,n,r){return e.event_watcher?(e.event_watcher.emitter()!==t&&e.event_watcher.model_ref!==t&&b(this,"emitter not matching"),c.utils.wrappedEventWatcher(n,e.event_watcher).registerCallbacks(n,r)):(c.utils.wrappedEventWatcherIsOwned(n,!0),c.utils.wrappedEventWatcher(n,new c.EventWatcher(t)).registerCallbacks(n,r))},t.prototype.destroy=function(){return this.emitter(null),this.__kb.callbacks=null,c.utils.wrappedDestroy(this)},t.prototype.emitter=function(t){var n,r,i,s,o,u,a,f;if(arguments.length===0||this.ee===t)return this.ee;this.model_ref&&(this.model_ref.unbind("loaded",this.__kb._onModelLoaded),this.model_ref.unbind("unloaded",this.__kb._onModelUnloaded),this.model_ref.release(),this.model_ref=null),e.ModelRef&&t instanceof e.ModelRef?(this.model_ref=t,this.model_ref.retain(),this.model_ref.bind("loaded",this.__kb._onModelLoaded),this.model_ref.bind("unloaded",this.__kb._onModelUnloaded),t=this.model_ref.model()):delete this.model_ref,o=this.ee,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],o&&o.unbind(r,n.fn),t&&this.ee.bind(r,n.fn),s=n.list;for(u=0,a=s.length;u<a;u++)i=s[u],i.emitter&&i.emitter(this.ee)}return t},t.prototype.registerCallbacks=function(t,n){var r,i,s,o,u,a,l,h,p=this;t||y(this,"obj"),n||y(this,"info"),o=n.event_selector?n.event_selector:"change",s=o.split(" ");for(l=0,h=s.length;l<h;l++){i=s[l];if(!i)continue;r=this.__kb.callbacks[i],r||(a=[],r={list:a,fn:function(e){var t,n,r;for(n=0,r=a.length;n<r;n++){t=a[n];if(t.update&&!t.rel_fn){if(e&&t.key&&e.hasChanged&&!e.hasChanged(E(t.key)))continue;!c.statistics||f(e,i,t),t.update()}}return null}},this.__kb.callbacks[i]=r,this.ee&&this.ee.bind(i,r.fn)),u=d.defaults({obj:t},n),r.list.push(u)}this.ee&&(e.RelationalModel&&this.ee instanceof e.RelationalModel&&d.contains(s,"change")&&this._modelBindRelatationalInfo("change",u),u.emitter(this.ee)&&u.emitter)},t.prototype.releaseCallbacks=function(e){var t,n,r,i,s,o;if(!this.__kb.callbacks)return;s=this.__kb.callbacks;for(n in s){t=s[n],o=t.list;for(r in o){i=o[r];if(i.obj!==e)continue;t.list.splice(r,1),i.rel_fn&&this._modelUnbindRelatationalInfo(n,i),i.emitter&&i.emitter(null);return}}},t.prototype._onModelLoaded=function(t){var n,r,i,s,o,u,a,f;s=e.RelationalModel&&t instanceof e.RelationalModel,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],this.ee.bind(r,n.fn),o=n.list;for(u=0,a=o.length;u<a;u++)i=o[u],s&&this._modelBindRelatationalInfo(r,i),i.emitter&&i.emitter(this.ee)}},t.prototype._onModelUnloaded=function(e){var t,n,r,i,s,o,u;this.ee=null,u=this.__kb.callbacks;for(n in u){t=u[n],e.unbind(n,t.fn),i=t.list;for(s=0,o=i.length;s<o;s++)r=i[s],r.rel_fn&&this._modelUnbindRelatationalInfo(n,r),r.emitter&&r.emitter(null)}},t.prototype._modelBindRelatationalInfo=function(e,t){var n,r;if(e==="change"&&t.key&&t.update){n=E(t.key),r=d.find(this.ee.getRelations(),function(e){return e.key===n});if(!r)return;t.rel_fn=function(n){return!c.statistics||f(n,""+e+" (relational)",t),t.update()},r.collectionType||d.isArray(r.keyContents)?(t.is_collection=!0,this.ee.bind("add:"+t.key,t.rel_fn),this.ee.bind("remove:"+t.key,t.rel_fn)):this.ee.bind("update:"+t.key,t.rel_fn)}},t.prototype._modelUnbindRelatationalInfo=function(e,t){if(!t.rel_fn)return;t.is_collection?(this.ee.unbind("add:"+t.key,t.rel_fn),this.ee.unbind("remove:"+t.key,t.rel_fn)):this.ee.unbind("update:"+t.key,t.rel_fn),t.rel_fn=null},t}(),c.emitterObservable=function(e,t){return new c.EventWatcher(e,t)},c.Observable=function(){function e(e,t,n){var r,i,s,o=this;return this.vm=n,t||y(this,"options"),this.vm||(this.vm={}),d.isString(t)||h.isObservable(t)?r=this.create_options={key:t}:r=this.create_options=l(t),this.key=r.key,delete r.key,this.key||y(this,"key"),!r.args||(this.args=r.args,delete r.args),!r.read||(this.read=r.read,delete r.read),!r.write||(this.write=r.write,delete r.write),i=r.event_watcher,delete r.event_watcher,this.vo=h.observable(null),this._model=h.observable(),s=c.utils.wrappedObservable(this,h.dependentObservable({read:function(){var e,t,n,r,i,s;t=[E(o.key)];if(o.args)if(d.isArray(o.args)){s=o.args;for(r=0,i=s.length;r<i;r++)e=s[r],t.push(E(e))}else t.push(E(o.args));return o._mdl===o._model()&&o._mdl&&(n=o.read?o.read.apply(o.vm,t):o._mdl.get.apply(o._mdl,t),o.update(n)),E(o.vo())},write:function(e){var t,n,r,i,s,u,a;i=w(e),r={},r[E(o.key)]=i,n=o.write?[i]:[r];if(o.args)if(d.isArray(o.args)){a=o.args;for(s=0,u=a.length;s<u;s++)t=a[s],n.push(E(t))}else n.push(E(o.args));return o._mdl&&(o.write?o.write.apply(o.vm,n):o._mdl.set.apply(o._mdl,n)),o.update(e)},owner:this.vm})),s.__kb_is_o=!0,r.store=c.utils.wrappedStore(s,r.store),r.path=c.utils.pathJoin(r.path,this.key),r.factories&&(typeof r.factories=="function"||r.factories.create)?(r.factory=c.utils.wrappedFactory(s,new c.Factory(r.factory)),r.factory.addPathMapping(r.path,r.factories)):r.factory=c.Factory.useOptionsOrCreate(r,s,r.path),delete r.factories,s.value=d.bind(this.value,this),s.valueType=d.bind(this.valueType,this),s.destroy=d.bind(this.destroy,this),s.model=this.model=h.dependentObservable({read:function(){return o._model(),o._mdl},write:function(e){if(o.__kb_destroyed||o._mdl===e)return;return o._mdl=e,o.update(null),o._model(e)}}),c.EventWatcher.useOptionsOrCreate({event_watcher:i},e,this,{emitter:this.model,update:d.bind(this.update,this),key:this.key,path:r.path}),this.__kb_value||this.update(),c.LocalizedObservable&&r.localizer&&(s=new r.localizer(s),delete r.localizer),c.DefaultObservable&&r.hasOwnProperty("default")&&(s=c.defaultObservable(s,r["default"]),delete r["default"]),s}return e.prototype.destroy=function(){var e;return e=c.utils.wrappedObservable(this),this.__kb_destroyed=!0,c.release(this.__kb_value),this.__kb_value=null,this.model.dispose(),this._mdl=this.model=e.model=null,c.utils.wrappedDestroy(this)},e.prototype.value=function(){return this.__kb_value},e.prototype.valueType=function(){var e;return e=this._mdl?this._mdl.get(this.key):null,this.value_type||this._updateValueObservable(e),this.value_type},e.prototype.update=function(e){var t,n;if(this.__kb_destroyed)return;this._mdl&&!arguments.length&&(e=this._mdl.get(E(this.key))),e!==void 0||(e=null),t=c.utils.valueType(e);if(!this.__kb_value||this.__kb_value.__kb_destroyed||this.__kb_value.__kb_null&&e)this.__kb_value=void 0,this.value_type=void 0;n=this.__kb_value;if(d.isUndefined(this.value_type)||this.value_type!==t&&t!==a)return this.value_type===s&&t===i?n(e):this._updateValueObservable(e);if(this.value_type===o){if(typeof n.model=="function"){if(n.model()!==e)return n.model(e)}else if(c.utils.wrappedObject(n)!==e)return this._updateValueObservable(e)}else if(this.value_type===s){if(n.collection()!==e)return n.collection(e)}else if(n()!==e)return n(e)},e.prototype._updateValueObservable=function(e){var t,n,r,f;return t=this.create_options,t.creator=c.utils.inferCreator(e,t.factory,t.path,this._mdl,this.key),this.value_type=a,n=t.creator,r=this.__kb_value,this.__kb_value=void 0,r&&c.release(r),n?t.store?f=t.store.findOrCreate(e,t):n.models_only?(f=e,this.value_type=u):n.create?f=n.create(e,t):f=new n(e,t):d.isArray(e)?(this.value_type=i,f=h.observableArray(e)):(this.value_type=u,f=h.observable(e)),this.value_type===a&&(h.isObservable(f)?f.__kb_is_co?this.value_type=s:this.value_type=u:(this.value_type=o,typeof f.model!="function"&&c.utils.wrappedObject(f,e))),this.__kb_value=f,this.vo(f)},e}(),c.observable=function(e,t,n){return new c.Observable(e,t,n)},c.ViewModel=function(){function t(t,n,r){var i,s,o,u,a,f,p,v,m,g=this;!t||t instanceof e.Model||typeof t.get=="function"&&typeof t.bind=="function"||b(this,"not a model"),n||(n={}),r||(r={}),d.isArray(n)?n={keys:n}:n=l(n),this.__kb||(this.__kb={}),this.__kb.vm_keys={},this.__kb.model_keys={},this.__kb.view_model=d.isUndefined(r)?this:r,!n.internals||(this.__kb.internals=n.internals),!n.excludes||(this.__kb.excludes=n.excludes),c.Store.useOptionsOrCreate(n,t,this),this.__kb.path=n.path,c.Factory.useOptionsOrCreate(n,this,n.path),v=S(this,"_mdl",h.observable()),this.model=h.dependentObservable({read:function(){return v(),c.utils.wrappedObject(g)},write:function(e){var t,n;if(c.utils.wrappedObject(g)===e)return;if(g.__kb_null){!e||b(g,"model set on shared null");return}c.utils.wrappedObject(g,e),t=c.utils.wrappedEventWatcher(g);if(!t){v(e);return}t.emitter(e),g.__kb.keys||!e||!e.attributes||(n=d.difference(d.keys(e.attributes),d.keys(g.__kb.model_keys)),n&&g._createObservables(e,n)),v(e)}}),o=c.utils.wrappedEventWatcher(this,new c.EventWatcher(t,this,{emitter:this.model})),n.requires&&d.isArray(n.requires)&&(u=d.clone(n.requires)),this.__kb.internals&&(u=u?d.union(u,this.__kb.internals):d.clone(this.__kb.internals));if(n.keys)if(d.isArray(n.keys))this.__kb.keys=n.keys,u=u?d.union(u,n.keys):d.clone(n.keys);else{a={},m=n.keys;for(p in m)f=m[p],a[d.isString(f)?f:f.key?f.key:p]=!0;this.__kb.keys=d.keys(a)}else s=o.emitter(),s&&s.attributes&&(i=d.keys(s.attributes),u=u?d.union(u,i):i);u&&this.__kb.excludes&&(u=d.difference(u,this.__kb.excludes)),d.isObject(n.keys)&&!d.isArray(n.keys)&&this._mapObservables(t,n.keys),d.isObject(n.requires)&&!d.isArray(n.requires)&&this._mapObservables(t,n.requires),!n.mappings||this._mapObservables(t,n.mappings),!u||this._createObservables(t,u),!c.statistics||c.statistics.register("ViewModel",this)}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e;if(this.__kb.view_model!==this)for(e in this.__kb.vm_keys)this.__kb.view_model[e]=null;return this.__kb.view_model=null,c.releaseKeys(this),c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("ViewModel",this)},t.prototype.shareOptions=function(){return{store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this)}},t.prototype._createObservables=function(e,t){var n,r,i,s,o;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(s=0,o=t.length;s<o;s++){r=t[s],i=this.__kb.internals&&d.contains(this.__kb.internals,r)?"_"+r:r;if(this[i])continue;this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r]=!0,n.key=r,this[i]=this.__kb.view_model[i]=c.observable(e,n,this)}},t.prototype._mapObservables=function(e,t){var n,r,i;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(i in t){r=t[i];if(this[i])continue;r=d.isString(r)?{key:r}:d.clone(r),r.key||(r.key=i),this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r.key]=!0,this[i]=this.__kb.view_model[i]=c.observable(e,d.defaults(r,n),this)}},t}(),c.viewModel=function(e,t,n){return new c.ViewModel(e,t,n)},c.observables=function(e,t,n){return g("kb.observables","0.16.0","Please use kb.viewModel instead"),new c.ViewModel(e,t,n)},r=0,t=-1,n=1,c.compare=function(e,i){return d.isString(e)?e.localeCompare(i):d.isString(i)?i.localeCompare(e):typeof e!="object"?e===i?r:e<i?t:n:e===i?r:e<i?t:n},c.CollectionObservable=function(){function t(t,n){var r,i,s=this;return!t||t instanceof e.Collection||b(this,"not a collection"),n||(n={}),i=c.utils.wrappedObservable(this,h.observableArray([])),i.__kb_is_co=!0,this.in_edit=0,this.__kb||(this.__kb={}),this.__kb._onCollectionChange=d.bind(this._onCollectionChange,this),n=l(n),n.sort_attribute?this._comparator=h.observable(this._attributeComparator(n.sort_attribute)):(n.sorted_index&&g("sortedIndex no longer supported","0.16.7","please use comparator instead"),this._comparator=h.observable(n.comparator)),n.filters?this._filters=h.observableArray(d.isArray(n.filters)?n.filters:n.filters?[n.filters]:void 0):this._filters=h.observableArray([]),r=this.create_options={store:c.Store.useOptionsOrCreate(n,t,i)},this.path=n.path,r.factory=c.utils.wrappedFactory(i,this._shareOrCreateFactory(n)),r.path=c.utils.pathJoin(n.path,"models"),r.creator=r.factory.creatorForPath(null,r.path),r.creator&&(this.models_only=r.creator.models_only),i.destroy=d.bind(this.destroy,this),i.shareOptions=d.bind(this.shareOptions,this),i.filters=d.bind(this.filters,this),i.comparator=d.bind(this.comparator,this),i.sortAttribute=d.bind(this.sortAttribute,this),i.viewModelByModel=d.bind(this.viewModelByModel,this),i.hasViewModels=d.bind(this.hasViewModels,this),this._collection=h.observable(t),i.collection=this.collection=h.dependentObservable({read:function(){return s._collection()},write:function(e){var t;if((t=s._collection())===e)return;return t&&t.unbind("all",s.__kb._onCollectionChange),e&&e.bind("all",s.__kb._onCollectionChange),s._collection(e)}}),t&&t.bind("all",this.__kb._onCollectionChange),this._mapper=h.dependentObservable(function(){var e,t,n,r,o;e=s._comparator(),n=s._filters(),t=s._collection();if(s.in_edit)return;return i=c.utils.wrappedObservable(s),t&&(r=t.models),!r||t.models.length===0?o=[]:(n.length&&(r=d.filter(r,function(e){return!s._modelIsFiltered(e)})),e?o=d.map(r,function(e){return s._createViewModel(e)}).sort(e):s.models_only?o=n.length?r:r.slice():o=d.map(r,function(e){return s._createViewModel(e)})),s.in_edit++,i(o),s.in_edit--}),i.subscribe(d.bind(this._onObservableArrayChange,this)),!c.statistics||c.statistics.register("CollectionObservable",this),i}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e,t,n;return n=c.utils.wrappedObservable(this),t=this._collection(),t&&(t.unbind("all",this.__kb._onCollectionChange),e=n(),e.splice(0,e.length)),this._mapper.dispose(),this._mapper=null,c.release(this._filters),this._comparator(null),this.collection.dispose(),n.collection=this.collection=null,n.collection=null,c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("CollectionObservable",this)},t.prototype.shareOptions=function(){var e;return e=c.utils.wrappedObservable(this),{store:c.utils.wrappedStore(e),factory:c.utils.wrappedFactory(e)}},t.prototype.filters=function(e){return e?this._filters(d.isArray(e)?e:[e]):this._filters([])},t.prototype.comparator=function(e){return this._comparator(e)},t.prototype.sortedIndex=function(){return g("sortedIndex no longer supported","0.16.7","please use comparator instead")},t.prototype.sortAttribute=function(e){return this._comparator(e?this._attributeComparator(e):null)},t.prototype.viewModelByModel=function(e){var t;return this.models_only?null:(t=e.hasOwnProperty(e.idAttribute)?e.idAttribute:"cid",d.find(c.utils.wrappedObservable(this)(),function(n){return n.__kb.object[t]===e[t]}))},t.prototype.hasViewModels=function(){return!this.models_only},t.prototype._shareOrCreateFactory=function(e){var t,n,r,i;t=c.utils.pathJoin(e.path,"models"),r=e.factories;if(i=e.factory)if((n=i.creatorForPath(null,t))&&(!r||r.models===n)){if(!r)return i;if(i.hasPathMappings(r,e.path))return i}return i=new c.Factory(e.factory),r&&i.addPathMappings(r,e.path),i.creatorForPath(null,t)||(e.hasOwnProperty("models_only")?e.models_only?i.addPathMapping(t,{models_only:!0}):i.addPathMapping(t,c.ViewModel):e.view_model?i.addPathMapping(t,e.view_model):e.create?i.addPathMapping(t,{create:e.create}):i.addPathMapping(t,c.ViewModel)),i},t.prototype._onCollectionChange=function(e,t){var n,r,i,s;if(this.in_edit)return;switch(e){case"reset":case"resort":this._collection.notifySubscribers(this._collection());break;case"new":case"add":if(this._modelIsFiltered(t))return;i=c.utils.wrappedObservable(this),n=this._collection();if(s=this.viewModelByModel(t))return;s=this._createViewModel(t),this.in_edit++,(r=this._comparator())?(i().push(s),i.sort(r)):i.splice(n.indexOf(t),0,s),this.in_edit--;break;case"remove":case"destroy":this._onModelRemove(t);break;case"change":if(this._modelIsFiltered(t))this._onModelRemove(t);else{s=this.viewModelByModel(t);if(s){if(r=this._comparator())i=c.utils.wrappedObservable(this),this.in_edit++,i.sort(r),this.in_edit--}else this._onCollectionChange("add",t)}}},t.prototype._onModelRemove=function(e){var t,n;n=this.models_only?e:this.viewModelByModel(e);if(!n)return;return t=c.utils.wrappedObservable(this),this.in_edit++,t.remove(n),this.in_edit--},t.prototype._onObservableArrayChange=function(e){var t,n,r,i,s,o,u,a,f,l=this;if(this.in_edit)return;this.models_only&&(!e.length||c.utils.hasModelSignature(e[0]))||!this.models_only&&(!e.length||d.isObject(e[0])&&!c.utils.hasModelSignature(e[0]))||b(this,"incorrect type passed"),s=c.utils.wrappedObservable(this),t=this._collection(),n=this._filters().length;if(!t)return;u=e;if(this.models_only)n&&(i=d.filter(e,function(e){return!l._modelIsFiltered(e)}));else{!n||(u=[]),i=[];for(a=0,f=e.length;a<f;a++){o=e[a],r=c.utils.wrappedObject(o);if(n){if(this._modelIsFiltered(r))continue;u.push(o)}this.create_options.store.findOrReplace(r,this.create_options.creator,o),i.push(r)}}this.in_edit++,e.length===u.length||s(u),d.isEqual(t.models,i)||t.reset(i),this.in_edit--},t.prototype._attributeComparator=function(e){var t;return t=function(t,n){var r;return r=E(e),c.compare(t.get(r),n.get(r))},this.models_only?t:function(e,n){return t(c.utils.wrappedModel(e),c.utils.wrappedModel(n))}},t.prototype._createViewModel=function(e){return this.models_only?e:this.create_options.store.findOrCreate(e,this.create_options)},t.prototype._modelIsFiltered=function(e){var t,n,r,i;n=this._filters();for(r=0,i=n.length;r<i;r++){t=n[r],t=E(t);if(typeof t=="function"&&t(e)||e&&e.id===t)return!0}return!1},t}(),c.collectionObservable=function(e,t){return new c.CollectionObservable(e,t)},h.bindingHandlers.inject={init:function(e,t,n,r){return c.Inject.inject(E(t()),r,e,t,n)}},c.Inject=function(){function e(){}return e.inject=function(e,t,n,r,i,s){var o,u,a;return o=function(e){var o,u,a;if(d.isFunction(e))t=new e(t,n,r,i),c.releaseOnNodeRemove(t,n);else{e.view_model&&(t=new e.view_model(t,n,r,i),c.releaseOnNodeRemove(t,n));for(o in e){a=e[o];if(o==="view_model")continue;o==="create"?a(t,n,r,i):d.isObject(a)&&!d.isFunction(a)?(u=s||a&&a.create?{}:t,t[o]=c.Inject.inject(a,u,n,r,i,!0)):t[o]=a}}return t},s?o(e):(u=(a=h.dependentObservable(function(){return o(e)}))(),a.dispose(),u)},e.injectViewModels=function(e){var t,n,r,i,s,o,u,a,f,l;a=[],o=function(e){var t,n,r,i,s;e.__kb_injected||e.attributes&&(t=d.find(e.attributes,function(e){return e.name==="kb-inject"}))&&(e.__kb_injected=!0,a.push({el:e,view_model:{},binding:t.value})),s=e.childNodes;for(r=0,i=s.length;r<i;r++)n=s[r],o(n)},o(e||document);for(f=0,l=a.length;f<l;f++){n=a[f];if(s=n.binding)s.search(/[:]/)<0||(s="{"+s+"}"),i=(new Function("","return ( "+s+" )"))(),i||(i={}),!i.options||(u=i.options,delete i.options),u||(u={}),n.view_model=c.Inject.inject(i,n.view_model,n.el,null,null,!0),t=n.view_model.afterBinding||u.afterBinding,r=n.view_model.beforeBinding||u.beforeBinding;r&&r(n.view_model,n.el,u),c.applyBindings(n.view_model,n.el,u),t&&t(n.view_model,n.el,u)}return a},e}(),c.injectViewModels=c.Inject.injectViewModels,this.$?this.$(function(){return c.injectViewModels()}):(p=function(){return document.readyState!=="complete"?setTimeout(p,0):c.injectViewModels()})(),c})}).call(this);
View
2  knockback-core.js
@@ -635,7 +635,7 @@ kb.Store = (function() {
Store.prototype.findOrReplace = function(obj, creator, observable) {
var index, record;
- obj || raiseUnexpected('obj missing');
+ obj || _throwUnexpected(this, 'obj missing');
if ((index = this.findIndex(obj, creator)) < 0) {
return this.register(obj, observable, {
creator: creator
View
2  knockback-core.min.js
@@ -3,4 +3,4 @@
(c) 2011, 2012 Kevin Malakoff - http://kmalakoff.github.com/knockback/
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Dependencies: Knockout.js, Backbone.js, and Underscore.js.
-*/(function(){return function(e){return typeof define=="function"&&define.amd?define("knockback",["underscore","backbone","knockout"],e):e.call(this)}(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x=function(e,t){return function(){return e.apply(t,arguments)}};c=function(){function t(){}return t.VERSION="0.16.7",t.TYPE_UNKNOWN=0,t.TYPE_SIMPLE=1,t.TYPE_ARRAY=2,t.TYPE_MODEL=3,t.TYPE_COLLECTION=4,t.release=function(n,r){var i,o,u,a,f,l,c,p;if(!n||n!==Object(n)||typeof n=="function"&&!h.isObservable(n)||n.__kb_destroyed||n instanceof e.Model||n instanceof e.Collection)return this;if(d.isArray(n)){i=n.splice(0,n.length);for(f=0,c=i.length;f<c;f++)o=i[f],t.release(o);return this}n.__kb_destroyed=!0,!r||r();if(h.isObservable(n)||typeof n.dispose=="function"||typeof n.destroy=="function"||typeof n.release=="function")if(h.isObservable(n)&&d.isArray(i=n())){if(n.__kb_is_co||n.__kb_is_o&&n.valueType()===s)n.destroy?n.destroy():n.dispose&&n.dispose();else if(i.length){a=i.slice(0),i.splice(0,i.length);for(l=0,p=a.length;l<p;l++)u=a[l],t.release(u)}}else n.release?n.release():n.destroy?n.destroy():n.dispose&&n.dispose();else this.releaseKeys(n);return this},t.releaseKeys=function(e){var n,r;for(n in e)r=e[n],n==="__kb"||t.release(r,function(){return e[n]=null});return this},t.releaseOnNodeRemove=function(e,n){return e||b(this,"missing view model"),n||b(this,"missing node"),h.utils.domNodeDisposal.addDisposeCallback(n,function(){return t.release(e)})},t.renderTemplate=function(e,n,r){var i,s;return r==null&&(r={}),i=document.createElement("div"),s=h.renderTemplate(e,n,r,i,"replaceChildren"),i.children.length===1&&(i=i.children[0]),t.releaseOnNodeRemove(n,i),s.dispose(),i},t.renderAutoReleasedTemplate=function(e,t,n){return n==null&&(n={}),g("kb.renderAutoReleasedTemplate","0.16.3","Please use kb.renderTemplate instead"),this.renderTemplate(e,t,n={})},t.applyBindings=function(e,n){return h.applyBindings(e,n),t.releaseOnNodeRemove(e,n)},t}(),this.Knockback=this.kb=c,typeof exports!="undefined"&&(module.exports=c);if(!this._&&typeof require!="undefined")try{d=require("lodash")}catch(T){d=require("underscore")}else d=this._;return c._=d=d.hasOwnProperty("_")?d._:d,c.Backbone=e=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,c.ko=h=!this.ko&&typeof require!="undefined"?require("knockout"):this.ko,y=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is missing"},b=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is unexpected"},g=function(e,t,n){var r;return this._legacy_warnings||(this._legacy_warnings={}),(r=this._legacy_warnings)[e]||(r[e]=0),this._legacy_warnings[e]++,console.warn("warning: '"+e+"' has been deprecated (will be removed in Knockback after "+t+"). "+n+".")},m=Array.prototype.splice,E=h.utils.unwrapObservable,l=function(e){var t;t=d.clone(e);while(e.options)d.defaults(t,e.options),e=e.options;return delete t.options,t},a=c.TYPE_UNKNOWN,u=c.TYPE_SIMPLE,i=c.TYPE_ARRAY,o=c.TYPE_MODEL,s=c.TYPE_COLLECTION,S=function(e,t,n){return arguments.length===2?e&&e.__kb&&e.__kb.hasOwnProperty(t)?e.__kb[t]:void 0:(e||b(this,"no obj for wrapping "+t),e.__kb||(e.__kb={}),e.__kb[t]=n,n)},v=function(e,t){return m.call(e,1,0,t),e},w=function(e){var t,n,r;if(!e)return e;if(e.__kb)return"object"in e.__kb?e.__kb.object:e;if(d.isArray(e))return d.map(e,function(e){return w(e)});if(d.isObject(e)&&e.constructor==={}.constructor){n={};for(t in e)r=e[t],n[t]=w(r);return n}return e},c.utils=function(){function t(){}return t.wrappedObservable=function(e,t){return S.apply(this,v(arguments,"observable"))},t.wrappedObject=function(e,t){return S.apply(this,v(arguments,"object"))},t.wrappedModel=function(e,t){return arguments.length===1?(t=S(e,"object"),d.isUndefined(t)?e:t):S(e,"object",t)},t.wrappedStore=function(e,t){return S.apply(this,v(arguments,"store"))},t.wrappedStoreIsOwned=function(e,t){return S.apply(this,v(arguments,"store_is_owned"))},t.wrappedFactory=function(e,t){return S.apply(this,v(arguments,"factory"))},t.wrappedEventWatcher=function(e,t){return S.apply(this,v(arguments,"event_watcher"))},t.wrappedEventWatcherIsOwned=function(e,t){return S.apply(this,v(arguments,"event_watcher_is_owned"))},t.wrappedDestroy=function(e){var t;if(!e.__kb)return;return e.__kb.event_watcher&&e.__kb.event_watcher.releaseCallbacks(e),t=e.__kb,e.__kb=null,t.observable&&(t.observable.destroy=t.observable.release=null,this.wrappedDestroy(t.observable),t.observable=null),t.factory=null,t.event_watcher_is_owned&&t.event_watcher.destroy(),t.event_watcher=null,t.store_is_owned&&t.store.destroy(),t.store=null},t.valueType=function(t){return t?t.__kb_is_o?t.valueType():t.__kb_is_co||t instanceof e.Collection?s:t instanceof c.ViewModel||t instanceof e.Model?o:d.isArray(t)?i:u:a},t.pathJoin=function(e,t){return(e?e[e.length-1]!=="."?""+e+".":e:"")+t},t.optionsPathJoin=function(e,t){return d.defaults({path:this.pathJoin(e.path,t)},e)},t.inferCreator=function(t,n,r,i,s){var o,u;n&&(o=n.creatorForPath(t,r));if(o)return o;if(i&&e.RelationalModel&&i instanceof e.RelationalModel){s=E(s),u=d.find(i.getRelations(),function(e){return e.key===s});if(u)return u.collectionType||d.isArray(u.keyContents)?c.CollectionObservable:c.ViewModel}return t?t instanceof e.Model?c.ViewModel:t instanceof e.Collection?c.CollectionObservable:null:null},t.createFromDefaultCreator=function(t,n){return t instanceof e.Model?c.viewModel(t,n):t instanceof e.Collection?c.collectionObservable(t,n):d.isArray(t)?h.observableArray(t):h.observable(t)},t.hasModelSignature=function(e){return e&&e.attributes&&!e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.hasCollectionSignature=function(e){return e&&e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.release=function(e){return g("kb.utils.release","0.16.0","Please use kb.release instead"),c.release(e)},t}(),c.Factory=function(){function e(e){this.parent_factory=e,this.paths={}}return e.useOptionsOrCreate=function(e,t,n){var r;return e.factory&&(!e.factories||e.factories&&e.factory.hasPathMappings(e.factories,n))?c.utils.wrappedFactory(t,e.factory):(r=c.utils.wrappedFactory(t,new c.Factory(e.factory)),e.factories&&r.addPathMappings(e.factories,n),r)},e.prototype.hasPath=function(e){return this.paths.hasOwnProperty(e)||this.parent_factory&&this.parent_factory.hasPath(e)},e.prototype.addPathMapping=function(e,t){return this.paths[e]=t},e.prototype.addPathMappings=function(e,t){var n,r;for(r in e)n=e[r],this.paths[c.utils.pathJoin(t,r)]=n},e.prototype.hasPathMappings=function(e,t){var n,r,i,s;n=!0;for(s in e)r=e[s],n&=(i=this.creatorForPath(null,c.utils.pathJoin(t,s)))&&r===i;return n},e.prototype.creatorForPath=function(e,t){var n;if(n=this.paths[t])return n.view_model?n.view_model:n;if(this.parent_factory)if(n=this.parent_factory.creatorForPath(e,t))return n;return null},e}(),c.Store=function(){function t(){this.observable_records=[],this.replaced_observables=[]}return t.useOptionsOrCreate=function(e,t,n){return e.store?(e.store.register(t,n,e),c.utils.wrappedStore(n,e.store)):(c.utils.wrappedStoreIsOwned(n,!0),c.utils.wrappedStore(n,new c.Store))},t.prototype.destroy=function(){return this.clear()},t.prototype.clear=function(){var e,t,n,r;r=this.observable_records.splice(0,this.observable_records.length);for(t=0,n=r.length;t<n;t++)e=r[t],c.release(e.observable);c.release(this.replaced_observables)},t.prototype.register=function(e,t,n){var r;if(!t)return;if(h.isObservable(t)||t.__kb_is_co)return;return c.utils.wrappedObject(t,e),e||(t.__kb_null=!0),r=n.creator?n.creator:n.path&&n.factory?n.factory.creatorForPath(e,n.path):null,r||(r=t.constructor),this.observable_records.push({obj:e,observable:t,creator:r}),t},t.prototype.findIndex=function(t,n){var r,i,s;if(!t||t instanceof e.Model){s=this.observable_records;for(r in s){i=s[r];if(!i.observable)continue;if(i.observable.__kb_destroyed){i.obj=null,i.observable=null;continue}if(!t&&!i.observable.__kb_null||t&&(i.observable.__kb_null||i.obj!==t))continue;if(i.creator===n||i.creator.create&&i.creator.create===n.create)return r}}return-1},t.prototype.find=function(e,t){var n;return(n=this.findIndex(e,t))<0?null:this.observable_records[n].observable},t.prototype.isRegistered=function(e){var t,n,r,i;i=this.observable_records;for(n=0,r=i.length;n<r;n++){t=i[n];if(t.observable===e)return!0}return!1},t.prototype.findOrCreate=function(t,n){var r,i;return n.store=this,n.creator||(n.creator=c.utils.inferCreator(t,n.factory,n.path)),!n.creator&&t instanceof e.Model&&(n.creator=kv.ViewModel),r=n.creator,r?r.models_only?t:(r&&(i=this.find(t,r)),i?i:(r.create?i=r.create(t,n):i=new r(t,n),i||(i=h.observable(null)),h.isObservable(i)||this.isRegistered(i)||this.register(t,i,n),i)):c.utils.createFromDefaultCreator(t,n)},t.prototype.findOrReplace=function(e,t,n){var r,i;return e||raiseUnexpected("obj missing"),(r=this.findIndex(e,t))<0?this.register(e,n,{creator:t}):(i=this.observable_records[r],c.utils.wrappedObject(i.observable)===e||b(this,"different object"),i.observable!==n&&(i.observable.constructor===n.constructor||b(this,"replacing different type"),this.replaced_observables.push(i.observable),i.observable=n),n)},t}(),f=function(e,t,n){return!c.statistics||c.statistics.addModelEvent({name:t,emitter:e,key:n.key,path:n.path})},c.EventWatcher=function(){function t(e,t,n){this._onModelUnloaded=x(this._onModelUnloaded,this),this._onModelLoaded=x(this._onModelLoaded,this),this.__kb||(this.__kb={}),this.__kb.callbacks={},this.__kb._onModelLoaded=d.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=d.bind(this._onModelUnloaded,this),n&&this.registerCallbacks(t,n),e?this.emitter(e):this.ee=null}return t.useOptionsOrCreate=function(e,t,n,r){return e.event_watcher?(e.event_watcher.emitter()!==t&&e.event_watcher.model_ref!==t&&b(this,"emitter not matching"),c.utils.wrappedEventWatcher(n,e.event_watcher).registerCallbacks(n,r)):(c.utils.wrappedEventWatcherIsOwned(n,!0),c.utils.wrappedEventWatcher(n,new c.EventWatcher(t)).registerCallbacks(n,r))},t.prototype.destroy=function(){return this.emitter(null),this.__kb.callbacks=null,c.utils.wrappedDestroy(this)},t.prototype.emitter=function(t){var n,r,i,s,o,u,a,f;if(arguments.length===0||this.ee===t)return this.ee;this.model_ref&&(this.model_ref.unbind("loaded",this.__kb._onModelLoaded),this.model_ref.unbind("unloaded",this.__kb._onModelUnloaded),this.model_ref.release(),this.model_ref=null),e.ModelRef&&t instanceof e.ModelRef?(this.model_ref=t,this.model_ref.retain(),this.model_ref.bind("loaded",this.__kb._onModelLoaded),this.model_ref.bind("unloaded",this.__kb._onModelUnloaded),t=this.model_ref.model()):delete this.model_ref,o=this.ee,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],o&&o.unbind(r,n.fn),t&&this.ee.bind(r,n.fn),s=n.list;for(u=0,a=s.length;u<a;u++)i=s[u],i.emitter&&i.emitter(this.ee)}return t},t.prototype.registerCallbacks=function(t,n){var r,i,s,o,u,a,l,h,p=this;t||y(this,"obj"),n||y(this,"info"),o=n.event_selector?n.event_selector:"change",s=o.split(" ");for(l=0,h=s.length;l<h;l++){i=s[l];if(!i)continue;r=this.__kb.callbacks[i],r||(a=[],r={list:a,fn:function(e){var t,n,r;for(n=0,r=a.length;n<r;n++){t=a[n];if(t.update&&!t.rel_fn){if(e&&t.key&&e.hasChanged&&!e.hasChanged(E(t.key)))continue;!c.statistics||f(e,i,t),t.update()}}return null}},this.__kb.callbacks[i]=r,this.ee&&this.ee.bind(i,r.fn)),u=d.defaults({obj:t},n),r.list.push(u)}this.ee&&(e.RelationalModel&&this.ee instanceof e.RelationalModel&&d.contains(s,"change")&&this._modelBindRelatationalInfo("change",u),u.emitter(this.ee)&&u.emitter)},t.prototype.releaseCallbacks=function(e){var t,n,r,i,s,o;if(!this.__kb.callbacks)return;s=this.__kb.callbacks;for(n in s){t=s[n],o=t.list;for(r in o){i=o[r];if(i.obj!==e)continue;t.list.splice(r,1),i.rel_fn&&this._modelUnbindRelatationalInfo(n,i),i.emitter&&i.emitter(null);return}}},t.prototype._onModelLoaded=function(t){var n,r,i,s,o,u,a,f;s=e.RelationalModel&&t instanceof e.RelationalModel,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],this.ee.bind(r,n.fn),o=n.list;for(u=0,a=o.length;u<a;u++)i=o[u],s&&this._modelBindRelatationalInfo(r,i),i.emitter&&i.emitter(this.ee)}},t.prototype._onModelUnloaded=function(e){var t,n,r,i,s,o,u;this.ee=null,u=this.__kb.callbacks;for(n in u){t=u[n],e.unbind(n,t.fn),i=t.list;for(s=0,o=i.length;s<o;s++)r=i[s],r.rel_fn&&this._modelUnbindRelatationalInfo(n,r),r.emitter&&r.emitter(null)}},t.prototype._modelBindRelatationalInfo=function(e,t){var n,r;if(e==="change"&&t.key&&t.update){n=E(t.key),r=d.find(this.ee.getRelations(),function(e){return e.key===n});if(!r)return;t.rel_fn=function(n){return!c.statistics||f(n,""+e+" (relational)",t),t.update()},r.collectionType||d.isArray(r.keyContents)?(t.is_collection=!0,this.ee.bind("add:"+t.key,t.rel_fn),this.ee.bind("remove:"+t.key,t.rel_fn)):this.ee.bind("update:"+t.key,t.rel_fn)}},t.prototype._modelUnbindRelatationalInfo=function(e,t){if(!t.rel_fn)return;t.is_collection?(this.ee.unbind("add:"+t.key,t.rel_fn),this.ee.unbind("remove:"+t.key,t.rel_fn)):this.ee.unbind("update:"+t.key,t.rel_fn),t.rel_fn=null},t}(),c.emitterObservable=function(e,t){return new c.EventWatcher(e,t)},c.Observable=function(){function e(e,t,n){var r,i,s,o=this;return this.vm=n,t||y(this,"options"),this.vm||(this.vm={}),d.isString(t)||h.isObservable(t)?r=this.create_options={key:t}:r=this.create_options=l(t),this.key=r.key,delete r.key,this.key||y(this,"key"),!r.args||(this.args=r.args,delete r.args),!r.read||(this.read=r.read,delete r.read),!r.write||(this.write=r.write,delete r.write),i=r.event_watcher,delete r.event_watcher,this.vo=h.observable(null),this._model=h.observable(),s=c.utils.wrappedObservable(this,h.dependentObservable({read:function(){var e,t,n,r,i,s;t=[E(o.key)];if(o.args)if(d.isArray(o.args)){s=o.args;for(r=0,i=s.length;r<i;r++)e=s[r],t.push(E(e))}else t.push(E(o.args));return o._mdl===o._model()&&o._mdl&&(n=o.read?o.read.apply(o.vm,t):o._mdl.get.apply(o._mdl,t),o.update(n)),E(o.vo())},write:function(e){var t,n,r,i,s,u,a;i=w(e),r={},r[E(o.key)]=i,n=o.write?[i]:[r];if(o.args)if(d.isArray(o.args)){a=o.args;for(s=0,u=a.length;s<u;s++)t=a[s],n.push(E(t))}else n.push(E(o.args));return o._mdl&&(o.write?o.write.apply(o.vm,n):o._mdl.set.apply(o._mdl,n)),o.update(e)},owner:this.vm})),s.__kb_is_o=!0,r.store=c.utils.wrappedStore(s,r.store),r.path=c.utils.pathJoin(r.path,this.key),r.factories&&(typeof r.factories=="function"||r.factories.create)?(r.factory=c.utils.wrappedFactory(s,new c.Factory(r.factory)),r.factory.addPathMapping(r.path,r.factories)):r.factory=c.Factory.useOptionsOrCreate(r,s,r.path),delete r.factories,s.value=d.bind(this.value,this),s.valueType=d.bind(this.valueType,this),s.destroy=d.bind(this.destroy,this),s.model=this.model=h.dependentObservable({read:function(){return o._model(),o._mdl},write:function(e){if(o.__kb_destroyed||o._mdl===e)return;return o._mdl=e,o.update(null),o._model(e)}}),c.EventWatcher.useOptionsOrCreate({event_watcher:i},e,this,{emitter:this.model,update:d.bind(this.update,this),key:this.key,path:r.path}),this.__kb_value||this.update(),c.LocalizedObservable&&r.localizer&&(s=new r.localizer(s),delete r.localizer),c.DefaultObservable&&r.hasOwnProperty("default")&&(s=c.defaultObservable(s,r["default"]),delete r["default"]),s}return e.prototype.destroy=function(){var e;return e=c.utils.wrappedObservable(this),this.__kb_destroyed=!0,c.release(this.__kb_value),this.__kb_value=null,this.model.dispose(),this._mdl=this.model=e.model=null,c.utils.wrappedDestroy(this)},e.prototype.value=function(){return this.__kb_value},e.prototype.valueType=function(){var e;return e=this._mdl?this._mdl.get(this.key):null,this.value_type||this._updateValueObservable(e),this.value_type},e.prototype.update=function(e){var t,n;if(this.__kb_destroyed)return;this._mdl&&!arguments.length&&(e=this._mdl.get(E(this.key))),e!==void 0||(e=null),t=c.utils.valueType(e);if(!this.__kb_value||this.__kb_value.__kb_destroyed||this.__kb_value.__kb_null&&e)this.__kb_value=void 0,this.value_type=void 0;n=this.__kb_value;if(d.isUndefined(this.value_type)||this.value_type!==t&&t!==a)return this.value_type===s&&t===i?n(e):this._updateValueObservable(e);if(this.value_type===o){if(typeof n.model=="function"){if(n.model()!==e)return n.model(e)}else if(c.utils.wrappedObject(n)!==e)return this._updateValueObservable(e)}else if(this.value_type===s){if(n.collection()!==e)return n.collection(e)}else if(n()!==e)return n(e)},e.prototype._updateValueObservable=function(e){var t,n,r,f;return t=this.create_options,t.creator=c.utils.inferCreator(e,t.factory,t.path,this._mdl,this.key),this.value_type=a,n=t.creator,r=this.__kb_value,this.__kb_value=void 0,r&&c.release(r),n?t.store?f=t.store.findOrCreate(e,t):n.models_only?(f=e,this.value_type=u):n.create?f=n.create(e,t):f=new n(e,t):d.isArray(e)?(this.value_type=i,f=h.observableArray(e)):(this.value_type=u,f=h.observable(e)),this.value_type===a&&(h.isObservable(f)?f.__kb_is_co?this.value_type=s:this.value_type=u:(this.value_type=o,typeof f.model!="function"&&c.utils.wrappedObject(f,e))),this.__kb_value=f,this.vo(f)},e}(),c.observable=function(e,t,n){return new c.Observable(e,t,n)},c.ViewModel=function(){function t(t,n,r){var i,s,o,u,a,f,p,v,m,g=this;!t||t instanceof e.Model||typeof t.get=="function"&&typeof t.bind=="function"||b(this,"not a model"),n||(n={}),r||(r={}),d.isArray(n)?n={keys:n}:n=l(n),this.__kb||(this.__kb={}),this.__kb.vm_keys={},this.__kb.model_keys={},this.__kb.view_model=d.isUndefined(r)?this:r,!n.internals||(this.__kb.internals=n.internals),!n.excludes||(this.__kb.excludes=n.excludes),c.Store.useOptionsOrCreate(n,t,this),this.__kb.path=n.path,c.Factory.useOptionsOrCreate(n,this,n.path),v=S(this,"_mdl",h.observable()),this.model=h.dependentObservable({read:function(){return v(),c.utils.wrappedObject(g)},write:function(e){var t,n;if(c.utils.wrappedObject(g)===e)return;if(g.__kb_null){!e||b(g,"model set on shared null");return}c.utils.wrappedObject(g,e),t=c.utils.wrappedEventWatcher(g);if(!t){v(e);return}t.emitter(e),g.__kb.keys||!e||!e.attributes||(n=d.difference(d.keys(e.attributes),d.keys(g.__kb.model_keys)),n&&g._createObservables(e,n)),v(e)}}),o=c.utils.wrappedEventWatcher(this,new c.EventWatcher(t,this,{emitter:this.model})),n.requires&&d.isArray(n.requires)&&(u=d.clone(n.requires)),this.__kb.internals&&(u=u?d.union(u,this.__kb.internals):d.clone(this.__kb.internals));if(n.keys)if(d.isArray(n.keys))this.__kb.keys=n.keys,u=u?d.union(u,n.keys):d.clone(n.keys);else{a={},m=n.keys;for(p in m)f=m[p],a[d.isString(f)?f:f.key?f.key:p]=!0;this.__kb.keys=d.keys(a)}else s=o.emitter(),s&&s.attributes&&(i=d.keys(s.attributes),u=u?d.union(u,i):i);u&&this.__kb.excludes&&(u=d.difference(u,this.__kb.excludes)),d.isObject(n.keys)&&!d.isArray(n.keys)&&this._mapObservables(t,n.keys),d.isObject(n.requires)&&!d.isArray(n.requires)&&this._mapObservables(t,n.requires),!n.mappings||this._mapObservables(t,n.mappings),!u||this._createObservables(t,u),!c.statistics||c.statistics.register("ViewModel",this)}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e;if(this.__kb.view_model!==this)for(e in this.__kb.vm_keys)this.__kb.view_model[e]=null;return this.__kb.view_model=null,c.releaseKeys(this),c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("ViewModel",this)},t.prototype.shareOptions=function(){return{store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this)}},t.prototype._createObservables=function(e,t){var n,r,i,s,o;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(s=0,o=t.length;s<o;s++){r=t[s],i=this.__kb.internals&&d.contains(this.__kb.internals,r)?"_"+r:r;if(this[i])continue;this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r]=!0,n.key=r,this[i]=this.__kb.view_model[i]=c.observable(e,n,this)}},t.prototype._mapObservables=function(e,t){var n,r,i;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(i in t){r=t[i];if(this[i])continue;r=d.isString(r)?{key:r}:d.clone(r),r.key||(r.key=i),this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r.key]=!0,this[i]=this.__kb.view_model[i]=c.observable(e,d.defaults(r,n),this)}},t}(),c.viewModel=function(e,t,n){return new c.ViewModel(e,t,n)},c.observables=function(e,t,n){return g("kb.observables","0.16.0","Please use kb.viewModel instead"),new c.ViewModel(e,t,n)},r=0,t=-1,n=1,c.compare=function(e,i){return d.isString(e)?e.localeCompare(i):d.isString(i)?i.localeCompare(e):typeof e!="object"?e===i?r:e<i?t:n:e===i?r:e<i?t:n},c.CollectionObservable=function(){function t(t,n){var r,i,s=this;return!t||t instanceof e.Collection||b(this,"not a collection"),n||(n={}),i=c.utils.wrappedObservable(this,h.observableArray([])),i.__kb_is_co=!0,this.in_edit=0,this.__kb||(this.__kb={}),this.__kb._onCollectionChange=d.bind(this._onCollectionChange,this),n=l(n),n.sort_attribute?this._comparator=h.observable(this._attributeComparator(n.sort_attribute)):(n.sorted_index&&g("sortedIndex no longer supported","0.16.7","please use comparator instead"),this._comparator=h.observable(n.comparator)),n.filters?this._filters=h.observableArray(d.isArray(n.filters)?n.filters:n.filters?[n.filters]:void 0):this._filters=h.observableArray([]),r=this.create_options={store:c.Store.useOptionsOrCreate(n,t,i)},this.path=n.path,r.factory=c.utils.wrappedFactory(i,this._shareOrCreateFactory(n)),r.path=c.utils.pathJoin(n.path,"models"),r.creator=r.factory.creatorForPath(null,r.path),r.creator&&(this.models_only=r.creator.models_only),i.destroy=d.bind(this.destroy,this),i.shareOptions=d.bind(this.shareOptions,this),i.filters=d.bind(this.filters,this),i.comparator=d.bind(this.comparator,this),i.sortAttribute=d.bind(this.sortAttribute,this),i.viewModelByModel=d.bind(this.viewModelByModel,this),i.hasViewModels=d.bind(this.hasViewModels,this),this._collection=h.observable(t),i.collection=this.collection=h.dependentObservable({read:function(){return s._collection()},write:function(e){var t;if((t=s._collection())===e)return;return t&&t.unbind("all",s.__kb._onCollectionChange),e&&e.bind("all",s.__kb._onCollectionChange),s._collection(e)}}),t&&t.bind("all",this.__kb._onCollectionChange),this._mapper=h.dependentObservable(function(){var e,t,n,r,o;e=s._comparator(),n=s._filters(),t=s._collection();if(s.in_edit)return;return i=c.utils.wrappedObservable(s),t&&(r=t.models),!r||t.models.length===0?o=[]:(n.length&&(r=d.filter(r,function(e){return!s._modelIsFiltered(e)})),e?o=d.map(r,function(e){return s._createViewModel(e)}).sort(e):s.models_only?o=n.length?r:r.slice():o=d.map(r,function(e){return s._createViewModel(e)})),s.in_edit++,i(o),s.in_edit--}),i.subscribe(d.bind(this._onObservableArrayChange,this)),!c.statistics||c.statistics.register("CollectionObservable",this),i}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e,t,n;return n=c.utils.wrappedObservable(this),t=this._collection(),t&&(t.unbind("all",this.__kb._onCollectionChange),e=n(),e.splice(0,e.length)),this._mapper.dispose(),this._mapper=null,c.release(this._filters),this._comparator(null),this.collection.dispose(),n.collection=this.collection=null,n.collection=null,c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("CollectionObservable",this)},t.prototype.shareOptions=function(){var e;return e=c.utils.wrappedObservable(this),{store:c.utils.wrappedStore(e),factory:c.utils.wrappedFactory(e)}},t.prototype.filters=function(e){return e?this._filters(d.isArray(e)?e:[e]):this._filters([])},t.prototype.comparator=function(e){return this._comparator(e)},t.prototype.sortedIndex=function(){return g("sortedIndex no longer supported","0.16.7","please use comparator instead")},t.prototype.sortAttribute=function(e){return this._comparator(e?this._attributeComparator(e):null)},t.prototype.viewModelByModel=function(e){var t;return this.models_only?null:(t=e.hasOwnProperty(e.idAttribute)?e.idAttribute:"cid",d.find(c.utils.wrappedObservable(this)(),function(n){return n.__kb.object[t]===e[t]}))},t.prototype.hasViewModels=function(){return!this.models_only},t.prototype._shareOrCreateFactory=function(e){var t,n,r,i;t=c.utils.pathJoin(e.path,"models"),r=e.factories;if(i=e.factory)if((n=i.creatorForPath(null,t))&&(!r||r.models===n)){if(!r)return i;if(i.hasPathMappings(r,e.path))return i}return i=new c.Factory(e.factory),r&&i.addPathMappings(r,e.path),i.creatorForPath(null,t)||(e.hasOwnProperty("models_only")?e.models_only?i.addPathMapping(t,{models_only:!0}):i.addPathMapping(t,c.ViewModel):e.view_model?i.addPathMapping(t,e.view_model):e.create?i.addPathMapping(t,{create:e.create}):i.addPathMapping(t,c.ViewModel)),i},t.prototype._onCollectionChange=function(e,t){var n,r,i,s;if(this.in_edit)return;switch(e){case"reset":case"resort":this._collection.notifySubscribers(this._collection());break;case"new":case"add":if(this._modelIsFiltered(t))return;i=c.utils.wrappedObservable(this),n=this._collection();if(s=this.viewModelByModel(t))return;s=this._createViewModel(t),this.in_edit++,(r=this._comparator())?(i().push(s),i.sort(r)):i.splice(n.indexOf(t),0,s),this.in_edit--;break;case"remove":case"destroy":this._onModelRemove(t);break;case"change":if(this._modelIsFiltered(t))this._onModelRemove(t);else{s=this.viewModelByModel(t);if(s){if(r=this._comparator())i=c.utils.wrappedObservable(this),this.in_edit++,i.sort(r),this.in_edit--}else this._onCollectionChange("add",t)}}},t.prototype._onModelRemove=function(e){var t,n;n=this.models_only?e:this.viewModelByModel(e);if(!n)return;return t=c.utils.wrappedObservable(this),this.in_edit++,t.remove(n),this.in_edit--},t.prototype._onObservableArrayChange=function(e){var t,n,r,i,s,o,u,a,f,l=this;if(this.in_edit)return;this.models_only&&(!e.length||c.utils.hasModelSignature(e[0]))||!this.models_only&&(!e.length||d.isObject(e[0])&&!c.utils.hasModelSignature(e[0]))||b(this,"incorrect type passed"),s=c.utils.wrappedObservable(this),t=this._collection(),n=this._filters().length;if(!t)return;u=e;if(this.models_only)n&&(i=d.filter(e,function(e){return!l._modelIsFiltered(e)}));else{!n||(u=[]),i=[];for(a=0,f=e.length;a<f;a++){o=e[a],r=c.utils.wrappedObject(o);if(n){if(this._modelIsFiltered(r))continue;u.push(o)}this.create_options.store.findOrReplace(r,this.create_options.creator,o),i.push(r)}}this.in_edit++,e.length===u.length||s(u),d.isEqual(t.models,i)||t.reset(i),this.in_edit--},t.prototype._attributeComparator=function(e){var t;return t=function(t,n){var r;return r=E(e),c.compare(t.get(r),n.get(r))},this.models_only?t:function(e,n){return t(c.utils.wrappedModel(e),c.utils.wrappedModel(n))}},t.prototype._createViewModel=function(e){return this.models_only?e:this.create_options.store.findOrCreate(e,this.create_options)},t.prototype._modelIsFiltered=function(e){var t,n,r,i;n=this._filters();for(r=0,i=n.length;r<i;r++){t=n[r],t=E(t);if(typeof t=="function"&&t(e)||e&&e.id===t)return!0}return!1},t}(),c.collectionObservable=function(e,t){return new c.CollectionObservable(e,t)},h.bindingHandlers.inject={init:function(e,t,n,r){return c.Inject.inject(E(t()),r,e,t,n)}},c.Inject=function(){function e(){}return e.inject=function(e,t,n,r,i,s){var o,u,a;return o=function(e){var o,u,a;if(d.isFunction(e))t=new e(t,n,r,i),c.releaseOnNodeRemove(t,n);else{e.view_model&&(t=new e.view_model(t,n,r,i),c.releaseOnNodeRemove(t,n));for(o in e){a=e[o];if(o==="view_model")continue;o==="create"?a(t,n,r,i):d.isObject(a)&&!d.isFunction(a)?(u=s||a&&a.create?{}:t,t[o]=c.Inject.inject(a,u,n,r,i,!0)):t[o]=a}}return t},s?o(e):(u=(a=h.dependentObservable(function(){return o(e)}))(),a.dispose(),u)},e.injectViewModels=function(e){var t,n,r,i,s,o,u,a,f,l;a=[],o=function(e){var t,n,r,i,s;e.__kb_injected||e.attributes&&(t=d.find(e.attributes,function(e){return e.name==="kb-inject"}))&&(e.__kb_injected=!0,a.push({el:e,view_model:{},binding:t.value})),s=e.childNodes;for(r=0,i=s.length;r<i;r++)n=s[r],o(n)},o(e||document);for(f=0,l=a.length;f<l;f++){n=a[f];if(s=n.binding)s.search(/[:]/)<0||(s="{"+s+"}"),i=(new Function("","return ( "+s+" )"))(),i||(i={}),!i.options||(u=i.options,delete i.options),u||(u={}),n.view_model=c.Inject.inject(i,n.view_model,n.el,null,null,!0),t=n.view_model.afterBinding||u.afterBinding,r=n.view_model.beforeBinding||u.beforeBinding;r&&r(n.view_model,n.el,u),c.applyBindings(n.view_model,n.el,u),t&&t(n.view_model,n.el,u)}return a},e}(),c.injectViewModels=c.Inject.injectViewModels,this.$?this.$(function(){return c.injectViewModels()}):(p=function(){return document.readyState!=="complete"?setTimeout(p,0):c.injectViewModels()})(),c})}).call(this);
+*/(function(){return function(e){return typeof define=="function"&&define.amd?define("knockback",["underscore","backbone","knockout"],e):e.call(this)}(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x=function(e,t){return function(){return e.apply(t,arguments)}};c=function(){function t(){}return t.VERSION="0.16.7",t.TYPE_UNKNOWN=0,t.TYPE_SIMPLE=1,t.TYPE_ARRAY=2,t.TYPE_MODEL=3,t.TYPE_COLLECTION=4,t.release=function(n,r){var i,o,u,a,f,l,c,p;if(!n||n!==Object(n)||typeof n=="function"&&!h.isObservable(n)||n.__kb_destroyed||n instanceof e.Model||n instanceof e.Collection)return this;if(d.isArray(n)){i=n.splice(0,n.length);for(f=0,c=i.length;f<c;f++)o=i[f],t.release(o);return this}n.__kb_destroyed=!0,!r||r();if(h.isObservable(n)||typeof n.dispose=="function"||typeof n.destroy=="function"||typeof n.release=="function")if(h.isObservable(n)&&d.isArray(i=n())){if(n.__kb_is_co||n.__kb_is_o&&n.valueType()===s)n.destroy?n.destroy():n.dispose&&n.dispose();else if(i.length){a=i.slice(0),i.splice(0,i.length);for(l=0,p=a.length;l<p;l++)u=a[l],t.release(u)}}else n.release?n.release():n.destroy?n.destroy():n.dispose&&n.dispose();else this.releaseKeys(n);return this},t.releaseKeys=function(e){var n,r;for(n in e)r=e[n],n==="__kb"||t.release(r,function(){return e[n]=null});return this},t.releaseOnNodeRemove=function(e,n){return e||b(this,"missing view model"),n||b(this,"missing node"),h.utils.domNodeDisposal.addDisposeCallback(n,function(){return t.release(e)})},t.renderTemplate=function(e,n,r){var i,s;return r==null&&(r={}),i=document.createElement("div"),s=h.renderTemplate(e,n,r,i,"replaceChildren"),i.children.length===1&&(i=i.children[0]),t.releaseOnNodeRemove(n,i),s.dispose(),i},t.renderAutoReleasedTemplate=function(e,t,n){return n==null&&(n={}),g("kb.renderAutoReleasedTemplate","0.16.3","Please use kb.renderTemplate instead"),this.renderTemplate(e,t,n={})},t.applyBindings=function(e,n){return h.applyBindings(e,n),t.releaseOnNodeRemove(e,n)},t}(),this.Knockback=this.kb=c,typeof exports!="undefined"&&(module.exports=c);if(!this._&&typeof require!="undefined")try{d=require("lodash")}catch(T){d=require("underscore")}else d=this._;return c._=d=d.hasOwnProperty("_")?d._:d,c.Backbone=e=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,c.ko=h=!this.ko&&typeof require!="undefined"?require("knockout"):this.ko,y=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is missing"},b=function(e,t){throw""+(d.isString(e)?e:e.constructor.name)+": "+t+" is unexpected"},g=function(e,t,n){var r;return this._legacy_warnings||(this._legacy_warnings={}),(r=this._legacy_warnings)[e]||(r[e]=0),this._legacy_warnings[e]++,console.warn("warning: '"+e+"' has been deprecated (will be removed in Knockback after "+t+"). "+n+".")},m=Array.prototype.splice,E=h.utils.unwrapObservable,l=function(e){var t;t=d.clone(e);while(e.options)d.defaults(t,e.options),e=e.options;return delete t.options,t},a=c.TYPE_UNKNOWN,u=c.TYPE_SIMPLE,i=c.TYPE_ARRAY,o=c.TYPE_MODEL,s=c.TYPE_COLLECTION,S=function(e,t,n){return arguments.length===2?e&&e.__kb&&e.__kb.hasOwnProperty(t)?e.__kb[t]:void 0:(e||b(this,"no obj for wrapping "+t),e.__kb||(e.__kb={}),e.__kb[t]=n,n)},v=function(e,t){return m.call(e,1,0,t),e},w=function(e){var t,n,r;if(!e)return e;if(e.__kb)return"object"in e.__kb?e.__kb.object:e;if(d.isArray(e))return d.map(e,function(e){return w(e)});if(d.isObject(e)&&e.constructor==={}.constructor){n={};for(t in e)r=e[t],n[t]=w(r);return n}return e},c.utils=function(){function t(){}return t.wrappedObservable=function(e,t){return S.apply(this,v(arguments,"observable"))},t.wrappedObject=function(e,t){return S.apply(this,v(arguments,"object"))},t.wrappedModel=function(e,t){return arguments.length===1?(t=S(e,"object"),d.isUndefined(t)?e:t):S(e,"object",t)},t.wrappedStore=function(e,t){return S.apply(this,v(arguments,"store"))},t.wrappedStoreIsOwned=function(e,t){return S.apply(this,v(arguments,"store_is_owned"))},t.wrappedFactory=function(e,t){return S.apply(this,v(arguments,"factory"))},t.wrappedEventWatcher=function(e,t){return S.apply(this,v(arguments,"event_watcher"))},t.wrappedEventWatcherIsOwned=function(e,t){return S.apply(this,v(arguments,"event_watcher_is_owned"))},t.wrappedDestroy=function(e){var t;if(!e.__kb)return;return e.__kb.event_watcher&&e.__kb.event_watcher.releaseCallbacks(e),t=e.__kb,e.__kb=null,t.observable&&(t.observable.destroy=t.observable.release=null,this.wrappedDestroy(t.observable),t.observable=null),t.factory=null,t.event_watcher_is_owned&&t.event_watcher.destroy(),t.event_watcher=null,t.store_is_owned&&t.store.destroy(),t.store=null},t.valueType=function(t){return t?t.__kb_is_o?t.valueType():t.__kb_is_co||t instanceof e.Collection?s:t instanceof c.ViewModel||t instanceof e.Model?o:d.isArray(t)?i:u:a},t.pathJoin=function(e,t){return(e?e[e.length-1]!=="."?""+e+".":e:"")+t},t.optionsPathJoin=function(e,t){return d.defaults({path:this.pathJoin(e.path,t)},e)},t.inferCreator=function(t,n,r,i,s){var o,u;n&&(o=n.creatorForPath(t,r));if(o)return o;if(i&&e.RelationalModel&&i instanceof e.RelationalModel){s=E(s),u=d.find(i.getRelations(),function(e){return e.key===s});if(u)return u.collectionType||d.isArray(u.keyContents)?c.CollectionObservable:c.ViewModel}return t?t instanceof e.Model?c.ViewModel:t instanceof e.Collection?c.CollectionObservable:null:null},t.createFromDefaultCreator=function(t,n){return t instanceof e.Model?c.viewModel(t,n):t instanceof e.Collection?c.collectionObservable(t,n):d.isArray(t)?h.observableArray(t):h.observable(t)},t.hasModelSignature=function(e){return e&&e.attributes&&!e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.hasCollectionSignature=function(e){return e&&e.models&&typeof e.get=="function"&&typeof e.trigger=="function"},t.release=function(e){return g("kb.utils.release","0.16.0","Please use kb.release instead"),c.release(e)},t}(),c.Factory=function(){function e(e){this.parent_factory=e,this.paths={}}return e.useOptionsOrCreate=function(e,t,n){var r;return e.factory&&(!e.factories||e.factories&&e.factory.hasPathMappings(e.factories,n))?c.utils.wrappedFactory(t,e.factory):(r=c.utils.wrappedFactory(t,new c.Factory(e.factory)),e.factories&&r.addPathMappings(e.factories,n),r)},e.prototype.hasPath=function(e){return this.paths.hasOwnProperty(e)||this.parent_factory&&this.parent_factory.hasPath(e)},e.prototype.addPathMapping=function(e,t){return this.paths[e]=t},e.prototype.addPathMappings=function(e,t){var n,r;for(r in e)n=e[r],this.paths[c.utils.pathJoin(t,r)]=n},e.prototype.hasPathMappings=function(e,t){var n,r,i,s;n=!0;for(s in e)r=e[s],n&=(i=this.creatorForPath(null,c.utils.pathJoin(t,s)))&&r===i;return n},e.prototype.creatorForPath=function(e,t){var n;if(n=this.paths[t])return n.view_model?n.view_model:n;if(this.parent_factory)if(n=this.parent_factory.creatorForPath(e,t))return n;return null},e}(),c.Store=function(){function t(){this.observable_records=[],this.replaced_observables=[]}return t.useOptionsOrCreate=function(e,t,n){return e.store?(e.store.register(t,n,e),c.utils.wrappedStore(n,e.store)):(c.utils.wrappedStoreIsOwned(n,!0),c.utils.wrappedStore(n,new c.Store))},t.prototype.destroy=function(){return this.clear()},t.prototype.clear=function(){var e,t,n,r;r=this.observable_records.splice(0,this.observable_records.length);for(t=0,n=r.length;t<n;t++)e=r[t],c.release(e.observable);c.release(this.replaced_observables)},t.prototype.register=function(e,t,n){var r;if(!t)return;if(h.isObservable(t)||t.__kb_is_co)return;return c.utils.wrappedObject(t,e),e||(t.__kb_null=!0),r=n.creator?n.creator:n.path&&n.factory?n.factory.creatorForPath(e,n.path):null,r||(r=t.constructor),this.observable_records.push({obj:e,observable:t,creator:r}),t},t.prototype.findIndex=function(t,n){var r,i,s;if(!t||t instanceof e.Model){s=this.observable_records;for(r in s){i=s[r];if(!i.observable)continue;if(i.observable.__kb_destroyed){i.obj=null,i.observable=null;continue}if(!t&&!i.observable.__kb_null||t&&(i.observable.__kb_null||i.obj!==t))continue;if(i.creator===n||i.creator.create&&i.creator.create===n.create)return r}}return-1},t.prototype.find=function(e,t){var n;return(n=this.findIndex(e,t))<0?null:this.observable_records[n].observable},t.prototype.isRegistered=function(e){var t,n,r,i;i=this.observable_records;for(n=0,r=i.length;n<r;n++){t=i[n];if(t.observable===e)return!0}return!1},t.prototype.findOrCreate=function(t,n){var r,i;return n.store=this,n.creator||(n.creator=c.utils.inferCreator(t,n.factory,n.path)),!n.creator&&t instanceof e.Model&&(n.creator=kv.ViewModel),r=n.creator,r?r.models_only?t:(r&&(i=this.find(t,r)),i?i:(r.create?i=r.create(t,n):i=new r(t,n),i||(i=h.observable(null)),h.isObservable(i)||this.isRegistered(i)||this.register(t,i,n),i)):c.utils.createFromDefaultCreator(t,n)},t.prototype.findOrReplace=function(e,t,n){var r,i;return e||b(this,"obj missing"),(r=this.findIndex(e,t))<0?this.register(e,n,{creator:t}):(i=this.observable_records[r],c.utils.wrappedObject(i.observable)===e||b(this,"different object"),i.observable!==n&&(i.observable.constructor===n.constructor||b(this,"replacing different type"),this.replaced_observables.push(i.observable),i.observable=n),n)},t}(),f=function(e,t,n){return!c.statistics||c.statistics.addModelEvent({name:t,emitter:e,key:n.key,path:n.path})},c.EventWatcher=function(){function t(e,t,n){this._onModelUnloaded=x(this._onModelUnloaded,this),this._onModelLoaded=x(this._onModelLoaded,this),this.__kb||(this.__kb={}),this.__kb.callbacks={},this.__kb._onModelLoaded=d.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=d.bind(this._onModelUnloaded,this),n&&this.registerCallbacks(t,n),e?this.emitter(e):this.ee=null}return t.useOptionsOrCreate=function(e,t,n,r){return e.event_watcher?(e.event_watcher.emitter()!==t&&e.event_watcher.model_ref!==t&&b(this,"emitter not matching"),c.utils.wrappedEventWatcher(n,e.event_watcher).registerCallbacks(n,r)):(c.utils.wrappedEventWatcherIsOwned(n,!0),c.utils.wrappedEventWatcher(n,new c.EventWatcher(t)).registerCallbacks(n,r))},t.prototype.destroy=function(){return this.emitter(null),this.__kb.callbacks=null,c.utils.wrappedDestroy(this)},t.prototype.emitter=function(t){var n,r,i,s,o,u,a,f;if(arguments.length===0||this.ee===t)return this.ee;this.model_ref&&(this.model_ref.unbind("loaded",this.__kb._onModelLoaded),this.model_ref.unbind("unloaded",this.__kb._onModelUnloaded),this.model_ref.release(),this.model_ref=null),e.ModelRef&&t instanceof e.ModelRef?(this.model_ref=t,this.model_ref.retain(),this.model_ref.bind("loaded",this.__kb._onModelLoaded),this.model_ref.bind("unloaded",this.__kb._onModelUnloaded),t=this.model_ref.model()):delete this.model_ref,o=this.ee,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],o&&o.unbind(r,n.fn),t&&this.ee.bind(r,n.fn),s=n.list;for(u=0,a=s.length;u<a;u++)i=s[u],i.emitter&&i.emitter(this.ee)}return t},t.prototype.registerCallbacks=function(t,n){var r,i,s,o,u,a,l,h,p=this;t||y(this,"obj"),n||y(this,"info"),o=n.event_selector?n.event_selector:"change",s=o.split(" ");for(l=0,h=s.length;l<h;l++){i=s[l];if(!i)continue;r=this.__kb.callbacks[i],r||(a=[],r={list:a,fn:function(e){var t,n,r;for(n=0,r=a.length;n<r;n++){t=a[n];if(t.update&&!t.rel_fn){if(e&&t.key&&e.hasChanged&&!e.hasChanged(E(t.key)))continue;!c.statistics||f(e,i,t),t.update()}}return null}},this.__kb.callbacks[i]=r,this.ee&&this.ee.bind(i,r.fn)),u=d.defaults({obj:t},n),r.list.push(u)}this.ee&&(e.RelationalModel&&this.ee instanceof e.RelationalModel&&d.contains(s,"change")&&this._modelBindRelatationalInfo("change",u),u.emitter(this.ee)&&u.emitter)},t.prototype.releaseCallbacks=function(e){var t,n,r,i,s,o;if(!this.__kb.callbacks)return;s=this.__kb.callbacks;for(n in s){t=s[n],o=t.list;for(r in o){i=o[r];if(i.obj!==e)continue;t.list.splice(r,1),i.rel_fn&&this._modelUnbindRelatationalInfo(n,i),i.emitter&&i.emitter(null);return}}},t.prototype._onModelLoaded=function(t){var n,r,i,s,o,u,a,f;s=e.RelationalModel&&t instanceof e.RelationalModel,this.ee=t,f=this.__kb.callbacks;for(r in f){n=f[r],this.ee.bind(r,n.fn),o=n.list;for(u=0,a=o.length;u<a;u++)i=o[u],s&&this._modelBindRelatationalInfo(r,i),i.emitter&&i.emitter(this.ee)}},t.prototype._onModelUnloaded=function(e){var t,n,r,i,s,o,u;this.ee=null,u=this.__kb.callbacks;for(n in u){t=u[n],e.unbind(n,t.fn),i=t.list;for(s=0,o=i.length;s<o;s++)r=i[s],r.rel_fn&&this._modelUnbindRelatationalInfo(n,r),r.emitter&&r.emitter(null)}},t.prototype._modelBindRelatationalInfo=function(e,t){var n,r;if(e==="change"&&t.key&&t.update){n=E(t.key),r=d.find(this.ee.getRelations(),function(e){return e.key===n});if(!r)return;t.rel_fn=function(n){return!c.statistics||f(n,""+e+" (relational)",t),t.update()},r.collectionType||d.isArray(r.keyContents)?(t.is_collection=!0,this.ee.bind("add:"+t.key,t.rel_fn),this.ee.bind("remove:"+t.key,t.rel_fn)):this.ee.bind("update:"+t.key,t.rel_fn)}},t.prototype._modelUnbindRelatationalInfo=function(e,t){if(!t.rel_fn)return;t.is_collection?(this.ee.unbind("add:"+t.key,t.rel_fn),this.ee.unbind("remove:"+t.key,t.rel_fn)):this.ee.unbind("update:"+t.key,t.rel_fn),t.rel_fn=null},t}(),c.emitterObservable=function(e,t){return new c.EventWatcher(e,t)},c.Observable=function(){function e(e,t,n){var r,i,s,o=this;return this.vm=n,t||y(this,"options"),this.vm||(this.vm={}),d.isString(t)||h.isObservable(t)?r=this.create_options={key:t}:r=this.create_options=l(t),this.key=r.key,delete r.key,this.key||y(this,"key"),!r.args||(this.args=r.args,delete r.args),!r.read||(this.read=r.read,delete r.read),!r.write||(this.write=r.write,delete r.write),i=r.event_watcher,delete r.event_watcher,this.vo=h.observable(null),this._model=h.observable(),s=c.utils.wrappedObservable(this,h.dependentObservable({read:function(){var e,t,n,r,i,s;t=[E(o.key)];if(o.args)if(d.isArray(o.args)){s=o.args;for(r=0,i=s.length;r<i;r++)e=s[r],t.push(E(e))}else t.push(E(o.args));return o._mdl===o._model()&&o._mdl&&(n=o.read?o.read.apply(o.vm,t):o._mdl.get.apply(o._mdl,t),o.update(n)),E(o.vo())},write:function(e){var t,n,r,i,s,u,a;i=w(e),r={},r[E(o.key)]=i,n=o.write?[i]:[r];if(o.args)if(d.isArray(o.args)){a=o.args;for(s=0,u=a.length;s<u;s++)t=a[s],n.push(E(t))}else n.push(E(o.args));return o._mdl&&(o.write?o.write.apply(o.vm,n):o._mdl.set.apply(o._mdl,n)),o.update(e)},owner:this.vm})),s.__kb_is_o=!0,r.store=c.utils.wrappedStore(s,r.store),r.path=c.utils.pathJoin(r.path,this.key),r.factories&&(typeof r.factories=="function"||r.factories.create)?(r.factory=c.utils.wrappedFactory(s,new c.Factory(r.factory)),r.factory.addPathMapping(r.path,r.factories)):r.factory=c.Factory.useOptionsOrCreate(r,s,r.path),delete r.factories,s.value=d.bind(this.value,this),s.valueType=d.bind(this.valueType,this),s.destroy=d.bind(this.destroy,this),s.model=this.model=h.dependentObservable({read:function(){return o._model(),o._mdl},write:function(e){if(o.__kb_destroyed||o._mdl===e)return;return o._mdl=e,o.update(null),o._model(e)}}),c.EventWatcher.useOptionsOrCreate({event_watcher:i},e,this,{emitter:this.model,update:d.bind(this.update,this),key:this.key,path:r.path}),this.__kb_value||this.update(),c.LocalizedObservable&&r.localizer&&(s=new r.localizer(s),delete r.localizer),c.DefaultObservable&&r.hasOwnProperty("default")&&(s=c.defaultObservable(s,r["default"]),delete r["default"]),s}return e.prototype.destroy=function(){var e;return e=c.utils.wrappedObservable(this),this.__kb_destroyed=!0,c.release(this.__kb_value),this.__kb_value=null,this.model.dispose(),this._mdl=this.model=e.model=null,c.utils.wrappedDestroy(this)},e.prototype.value=function(){return this.__kb_value},e.prototype.valueType=function(){var e;return e=this._mdl?this._mdl.get(this.key):null,this.value_type||this._updateValueObservable(e),this.value_type},e.prototype.update=function(e){var t,n;if(this.__kb_destroyed)return;this._mdl&&!arguments.length&&(e=this._mdl.get(E(this.key))),e!==void 0||(e=null),t=c.utils.valueType(e);if(!this.__kb_value||this.__kb_value.__kb_destroyed||this.__kb_value.__kb_null&&e)this.__kb_value=void 0,this.value_type=void 0;n=this.__kb_value;if(d.isUndefined(this.value_type)||this.value_type!==t&&t!==a)return this.value_type===s&&t===i?n(e):this._updateValueObservable(e);if(this.value_type===o){if(typeof n.model=="function"){if(n.model()!==e)return n.model(e)}else if(c.utils.wrappedObject(n)!==e)return this._updateValueObservable(e)}else if(this.value_type===s){if(n.collection()!==e)return n.collection(e)}else if(n()!==e)return n(e)},e.prototype._updateValueObservable=function(e){var t,n,r,f;return t=this.create_options,t.creator=c.utils.inferCreator(e,t.factory,t.path,this._mdl,this.key),this.value_type=a,n=t.creator,r=this.__kb_value,this.__kb_value=void 0,r&&c.release(r),n?t.store?f=t.store.findOrCreate(e,t):n.models_only?(f=e,this.value_type=u):n.create?f=n.create(e,t):f=new n(e,t):d.isArray(e)?(this.value_type=i,f=h.observableArray(e)):(this.value_type=u,f=h.observable(e)),this.value_type===a&&(h.isObservable(f)?f.__kb_is_co?this.value_type=s:this.value_type=u:(this.value_type=o,typeof f.model!="function"&&c.utils.wrappedObject(f,e))),this.__kb_value=f,this.vo(f)},e}(),c.observable=function(e,t,n){return new c.Observable(e,t,n)},c.ViewModel=function(){function t(t,n,r){var i,s,o,u,a,f,p,v,m,g=this;!t||t instanceof e.Model||typeof t.get=="function"&&typeof t.bind=="function"||b(this,"not a model"),n||(n={}),r||(r={}),d.isArray(n)?n={keys:n}:n=l(n),this.__kb||(this.__kb={}),this.__kb.vm_keys={},this.__kb.model_keys={},this.__kb.view_model=d.isUndefined(r)?this:r,!n.internals||(this.__kb.internals=n.internals),!n.excludes||(this.__kb.excludes=n.excludes),c.Store.useOptionsOrCreate(n,t,this),this.__kb.path=n.path,c.Factory.useOptionsOrCreate(n,this,n.path),v=S(this,"_mdl",h.observable()),this.model=h.dependentObservable({read:function(){return v(),c.utils.wrappedObject(g)},write:function(e){var t,n;if(c.utils.wrappedObject(g)===e)return;if(g.__kb_null){!e||b(g,"model set on shared null");return}c.utils.wrappedObject(g,e),t=c.utils.wrappedEventWatcher(g);if(!t){v(e);return}t.emitter(e),g.__kb.keys||!e||!e.attributes||(n=d.difference(d.keys(e.attributes),d.keys(g.__kb.model_keys)),n&&g._createObservables(e,n)),v(e)}}),o=c.utils.wrappedEventWatcher(this,new c.EventWatcher(t,this,{emitter:this.model})),n.requires&&d.isArray(n.requires)&&(u=d.clone(n.requires)),this.__kb.internals&&(u=u?d.union(u,this.__kb.internals):d.clone(this.__kb.internals));if(n.keys)if(d.isArray(n.keys))this.__kb.keys=n.keys,u=u?d.union(u,n.keys):d.clone(n.keys);else{a={},m=n.keys;for(p in m)f=m[p],a[d.isString(f)?f:f.key?f.key:p]=!0;this.__kb.keys=d.keys(a)}else s=o.emitter(),s&&s.attributes&&(i=d.keys(s.attributes),u=u?d.union(u,i):i);u&&this.__kb.excludes&&(u=d.difference(u,this.__kb.excludes)),d.isObject(n.keys)&&!d.isArray(n.keys)&&this._mapObservables(t,n.keys),d.isObject(n.requires)&&!d.isArray(n.requires)&&this._mapObservables(t,n.requires),!n.mappings||this._mapObservables(t,n.mappings),!u||this._createObservables(t,u),!c.statistics||c.statistics.register("ViewModel",this)}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e;if(this.__kb.view_model!==this)for(e in this.__kb.vm_keys)this.__kb.view_model[e]=null;return this.__kb.view_model=null,c.releaseKeys(this),c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("ViewModel",this)},t.prototype.shareOptions=function(){return{store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this)}},t.prototype._createObservables=function(e,t){var n,r,i,s,o;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(s=0,o=t.length;s<o;s++){r=t[s],i=this.__kb.internals&&d.contains(this.__kb.internals,r)?"_"+r:r;if(this[i])continue;this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r]=!0,n.key=r,this[i]=this.__kb.view_model[i]=c.observable(e,n,this)}},t.prototype._mapObservables=function(e,t){var n,r,i;n={store:c.utils.wrappedStore(this),factory:c.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:c.utils.wrappedEventWatcher(this)};for(i in t){r=t[i];if(this[i])continue;r=d.isString(r)?{key:r}:d.clone(r),r.key||(r.key=i),this.__kb.vm_keys[i]=!0,this.__kb.model_keys[r.key]=!0,this[i]=this.__kb.view_model[i]=c.observable(e,d.defaults(r,n),this)}},t}(),c.viewModel=function(e,t,n){return new c.ViewModel(e,t,n)},c.observables=function(e,t,n){return g("kb.observables","0.16.0","Please use kb.viewModel instead"),new c.ViewModel(e,t,n)},r=0,t=-1,n=1,c.compare=function(e,i){return d.isString(e)?e.localeCompare(i):d.isString(i)?i.localeCompare(e):typeof e!="object"?e===i?r:e<i?t:n:e===i?r:e<i?t:n},c.CollectionObservable=function(){function t(t,n){var r,i,s=this;return!t||t instanceof e.Collection||b(this,"not a collection"),n||(n={}),i=c.utils.wrappedObservable(this,h.observableArray([])),i.__kb_is_co=!0,this.in_edit=0,this.__kb||(this.__kb={}),this.__kb._onCollectionChange=d.bind(this._onCollectionChange,this),n=l(n),n.sort_attribute?this._comparator=h.observable(this._attributeComparator(n.sort_attribute)):(n.sorted_index&&g("sortedIndex no longer supported","0.16.7","please use comparator instead"),this._comparator=h.observable(n.comparator)),n.filters?this._filters=h.observableArray(d.isArray(n.filters)?n.filters:n.filters?[n.filters]:void 0):this._filters=h.observableArray([]),r=this.create_options={store:c.Store.useOptionsOrCreate(n,t,i)},this.path=n.path,r.factory=c.utils.wrappedFactory(i,this._shareOrCreateFactory(n)),r.path=c.utils.pathJoin(n.path,"models"),r.creator=r.factory.creatorForPath(null,r.path),r.creator&&(this.models_only=r.creator.models_only),i.destroy=d.bind(this.destroy,this),i.shareOptions=d.bind(this.shareOptions,this),i.filters=d.bind(this.filters,this),i.comparator=d.bind(this.comparator,this),i.sortAttribute=d.bind(this.sortAttribute,this),i.viewModelByModel=d.bind(this.viewModelByModel,this),i.hasViewModels=d.bind(this.hasViewModels,this),this._collection=h.observable(t),i.collection=this.collection=h.dependentObservable({read:function(){return s._collection()},write:function(e){var t;if((t=s._collection())===e)return;return t&&t.unbind("all",s.__kb._onCollectionChange),e&&e.bind("all",s.__kb._onCollectionChange),s._collection(e)}}),t&&t.bind("all",this.__kb._onCollectionChange),this._mapper=h.dependentObservable(function(){var e,t,n,r,o;e=s._comparator(),n=s._filters(),t=s._collection();if(s.in_edit)return;return i=c.utils.wrappedObservable(s),t&&(r=t.models),!r||t.models.length===0?o=[]:(n.length&&(r=d.filter(r,function(e){return!s._modelIsFiltered(e)})),e?o=d.map(r,function(e){return s._createViewModel(e)}).sort(e):s.models_only?o=n.length?r:r.slice():o=d.map(r,function(e){return s._createViewModel(e)})),s.in_edit++,i(o),s.in_edit--}),i.subscribe(d.bind(this._onObservableArrayChange,this)),!c.statistics||c.statistics.register("CollectionObservable",this),i}return t.extend=e.Model.extend,t.prototype.destroy=function(){var e,t,n;return n=c.utils.wrappedObservable(this),t=this._collection(),t&&(t.unbind("all",this.__kb._onCollectionChange),e=n(),e.splice(0,e.length)),this._mapper.dispose(),this._mapper=null,c.release(this._filters),this._comparator(null),this.collection.dispose(),n.collection=this.collection=null,n.collection=null,c.utils.wrappedDestroy(this),!c.statistics||c.statistics.unregister("CollectionObservable",this)},t.prototype.shareOptions=function(){var e;return e=c.utils.wrappedObservable(this),{store:c.utils.wrappedStore(e),factory:c.utils.wrappedFactory(e)}},t.prototype.filters=function(e){return e?this._filters(d.isArray(e)?e:[e]):this._filters([])},t.prototype.comparator=function(e){return this._comparator(e)},t.prototype.sortedIndex=function(){return g("sortedIndex no longer supported","0.16.7","please use comparator instead")},t.prototype.sortAttribute=function(e){return this._comparator(e?this._attributeComparator(e):null)},t.prototype.viewModelByModel=function(e){var t;return this.models_only?null:(t=e.hasOwnProperty(e.idAttribute)?e.idAttribute:"cid",d.find(c.utils.wrappedObservable(this)(),function(n){return n.__kb.object[t]===e[t]}))},t.prototype.hasViewModels=function(){return!this.models_only},t.prototype._shareOrCreateFactory=function(e){var t,n,r,i;t=c.utils.pathJoin(e.path,"models"),r=e.factories;if(i=e.factory)if((n=i.creatorForPath(null,t))&&(!r||r.models===n)){if(!r)return i;if(i.hasPathMappings(r,e.path))return i}return i=new c.Factory(e.factory),r&&i.addPathMappings(r,e.path),i.creatorForPath(null,t)||(e.hasOwnProperty("models_only")?e.models_only?i.addPathMapping(t,{models_only:!0}):i.addPathMapping(t,c.ViewModel):e.view_model?i.addPathMapping(t,e.view_model):e.create?i.addPathMapping(t,{create:e.create}):i.addPathMapping(t,c.ViewModel)),i},t.prototype._onCollectionChange=function(e,t){var n,r,i,s;if(this.in_edit)return;switch(e){case"reset":case"resort":this._collection.notifySubscribers(this._collection());break;case"new":case"add":if(this._modelIsFiltered(t))return;i=c.utils.wrappedObservable(this),n=this._collection();if(s=this.viewModelByModel(t))return;s=this._createViewModel(t),this.in_edit++,(r=this._comparator())?(i().push(s),i.sort(r)):i.splice(n.indexOf(t),0,s),this.in_edit--;break;case"remove":case"destroy":this._onModelRemove(t);break;case"change":if(this._modelIsFiltered(t))this._onModelRemove(t);else{s=this.viewModelByModel(t);if(s){if(r=this._comparator())i=c.utils.wrappedObservable(this),this.in_edit++,i.sort(r),this.in_edit--}else this._onCollectionChange("add",t)}}},t.prototype._onModelRemove=function(e){var t,n;n=this.models_only?e:this.viewModelByModel(e);if(!n)return;return t=c.utils.wrappedObservable(this),this.in_edit++,t.remove(n),this.in_edit--},t.prototype._onObservableArrayChange=function(e){var t,n,r,i,s,o,u,a,f,l=this;if(this.in_edit)return;this.models_only&&(!e.length||c.utils.hasModelSignature(e[0]))||!this.models_only&&(!e.length||d.isObject(e[0])&&!c.utils.hasModelSignature(e[0]))||b(this,"incorrect type passed"),s=c.utils.wrappedObservable(this),t=this._collection(),n=this._filters().length;if(!t)return;u=e;if(this.models_only)n&&(i=d.filter(e,function(e){return!l._modelIsFiltered(e)}));else{!n||(u=[]),i=[];for(a=0,f=e.length;a<f;a++){o=e[a],r=c.utils.wrappedObject(o);if(n){if(this._modelIsFiltered(r))continue;u.push(o)}this.create_options.store.findOrReplace(r,this.create_options.creator,o),i.push(r)}}this.in_edit++,e.length===u.length||s(u),d.isEqual(t.models,i)||t.reset(i),this.in_edit--},t.prototype._attributeComparator=function(e){var t;return t=function(t,n){var r;return r=E(e),c.compare(t.get(r),n.get(r))},this.models_only?t:function(e,n){return t(c.utils.wrappedModel(e),c.utils.wrappedModel(n))}},t.prototype._createViewModel=function(e){return this.models_only?e:this.create_options.store.findOrCreate(e,this.create_options)},t.prototype._modelIsFiltered=function(e){var t,n,r,i;n=this._filters();for(r=0,i=n.length;r<i;r++){t=n[r],t=E(t);if(typeof t=="function"&&t(e)||e&&e.id===t)return!0}return!1},t}(),c.collectionObservable=function(e,t){return new c.CollectionObservable(e,t)},h.bindingHandlers.inject={init:function(e,t,n,r){return c.Inject.inject(E(t()),r,e,t,n)}},c.Inject=function(){function e(){}return e.inject=function(e,t,n,r,i,s){var o,u,a;return o=function(e){var o,u,a;if(d.isFunction(e))t=new e(t,n,r,i),c.releaseOnNodeRemove(t,n);else{e.view_model&&(t=new e.view_model(t,n,r,i),c.releaseOnNodeRemove(t,n));for(o in e){a=e[o];if(o==="view_model")continue;o==="create"?a(t,n,r,i):d.isObject(a)&&!d.isFunction(a)?(u=s||a&&a.create?{}:t,t[o]=c.Inject.inject(a,u,n,r,i,!0)):t[o]=a}}return t},s?o(e):(u=(a=h.dependentObservable(function(){return o(e)}))(),a.dispose(),u)},e.injectViewModels=function(e){var t,n,r,i,s,o,u,a,f,l;a=[],o=function(e){var t,n,r,i,s;e.__kb_injected||e.attributes&&(t=d.find(e.attributes,function(e){return e.name==="kb-inject"}))&&(e.__kb_injected=!0,a.push({el:e,view_model:{},binding:t.value})),s=e.childNodes;for(r=0,i=s.length;r<i;r++)n=s[r],o(n)},o(e||document);for(f=0,l=a.length;f<l;f++){n=a[f];if(s=n.binding)s.search(/[:]/)<0||(s="{"+s+"}"),i=(new Function("","return ( "+s+" )"))(),i||(i={}),!i.options||(u=i.options,delete i.options),u||(u={}),n.view_model=c.Inject.inject(i,n.view_model,n.el,null,null,!0),t=n.view_model.afterBinding||u.afterBinding,r=n.view_model.beforeBinding||u.beforeBinding;r&&r(n.view_model,n.el,u),c.applyBindings(n.view_model,n.el,u),t&&t(n.view_model,n.el,u)}return a},e}(),c.injectViewModels=c.Inject.injectViewModels,this.$?this.$(function(){return c.injectViewModels()}):(p=function(){return document.readyState!=="complete"?setTimeout(p,0):c.injectViewModels()})(),c})}).call(this);
View
605 knockback-full-stack.js
@@ -4,13 +4,10 @@
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Dependencies: Knockout.js, Backbone.js, and Underscore.js.
*/
-// Underscore.js 1.3.3
+// Underscore.js 1.4.2
+// http://underscorejs.org
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
-// Underscore is freely distributable under the MIT license.
-// Portions of Underscore are inspired or borrowed from Prototype,
-// Oliver Steele's Functional, and John Resig's Micro-Templating.
-// For all details and documentation:
-// http://documentcloud.github.com/underscore
+// Underscore may be freely distributed under the MIT license.
(function() {
@@ -30,7 +27,9 @@
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
// Create quick reference variables for speed access to core prototypes.
- var slice = ArrayProto.slice,
+ var push = ArrayProto.push,
+ slice = ArrayProto.slice,
+ concat = ArrayProto.concat,
unshift = ArrayProto.unshift,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
@@ -52,7 +51,11 @@
nativeBind = FuncProto.bind;
// Create a safe reference to the Underscore object for use below.
- var _ = function(obj) { return new wrapper(obj); };
+ var _ = function(obj) {
+ if (obj instanceof _) return obj;
+ if (!(this instanceof _)) return new _(obj);
+ this._wrapped = obj;
+ };
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
@@ -68,7 +71,7 @@
}
// Current version.
- _.VERSION = '1.3.3';
+ _.VERSION = '1.4.2';
// Collection Functions
// --------------------
@@ -82,7 +85,7 @@
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
- if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
+ if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
@@ -102,7 +105,6 @@
each(obj, function(value, index, list) {
results[results.length] = iterator.call(context, value, index, list);
});
- if (obj.length === +obj.length) results.length = obj.length;
return results;
};
@@ -134,11 +136,24 @@
if (obj == null) obj = [];
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
- return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
+ return arguments.length > 2 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
}
- var reversed = _.toArray(obj).reverse();
- if (context && !initial) iterator = _.bind(iterator, context);
- return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
+ var length = obj.length;
+ if (length !== +length) {
+ var keys = _.keys(obj);
+ length = keys.length;
+ }
+ each(obj, function(value, index, list) {
+ index = keys ? keys[--length] : --length;
+ if (!initial) {
+ memo = obj[index];
+ initial = true;
+ } else {
+ memo = iterator.call(context, memo, obj[index], index, list);
+ }
+ });
+ if (!initial) throw new TypeError('Reduce of empty array with no initial value');
+ return memo;
};
// Return the first value which passes a truth test. Aliased as `detect`.
@@ -180,6 +195,7 @@
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
_.every = _.all = function(obj, iterator, context) {
+ iterator || (iterator = _.identity);
var result = true;
if (obj == null) return result;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
@@ -203,9 +219,9 @@
return !!result;
};
- // Determine if a given value is included in the array or object using `===`.
- // Aliased as `contains`.
- _.include = _.contains = function(obj, target) {
+ // Determine if the array or object contains a given value (using `===`).
+ // Aliased as `include`.
+ _.contains = _.include = function(obj, target) {
var found = false;
if (obj == null) return found;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
@@ -219,7 +235,7 @@
_.invoke = function(obj, method) {
var args = slice.call(arguments, 2);
return _.map(obj, function(value) {
- return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
+ return (_.isFunction(method) ? method : value[method]).apply(value, args);
});
};
@@ -228,9 +244,25 @@
return _.map(obj, function(value){ return value[key]; });
};
+ // Convenience version of a common use case of `filter`: selecting only objects
+ // with specific `key:value` pairs.
+ _.where = function(obj, attrs) {
+ if (_.isEmpty(attrs)) return [];
+ return _.filter(obj, function(value) {
+ for (var key in attrs) {
+ if (attrs[key] !== value[key]) return false;
+ }
+ return true;
+ });
+ };
+
// Return the maximum element or (element-based computation).
+ // Can't optimize arrays of integers longer than 65,535 elements.
+ // See: https://bugs.webkit.org/show_bug.cgi?id=80797
_.max = function(obj, iterator, context) {
- if (!iterator && _.isArray(obj) && obj[0] === +obj[0]) return Math.max.apply(Math, obj);
+ if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
+ return Math.max.apply(Math, obj);
+ }
if (!iterator && _.isEmpty(obj)) return -Infinity;
var result = {computed : -Infinity};
each(obj, function(value, index, list) {
@@ -242,7 +274,9 @@
// Return the minimum element (or element-based computation).
_.min = function(obj, iterator, context) {
- if (!iterator && _.isArray(obj) && obj[0] === +obj[0]) return Math.min.apply(Math, obj);
+ if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
+ return Math.min.apply(Math, obj);
+ }
if (!iterator && _.isEmpty(obj)) return Infinity;
var result = {computed : Infinity};
each(obj, function(value, index, list) {
@@ -254,67 +288,94 @@
// Shuffle an array.
_.shuffle = function(obj) {
- var shuffled = [], rand;
- each(obj, function(value, index, list) {
- rand = Math.floor(Math.random() * (index + 1));
- shuffled[index] = shuffled[rand];
+ var rand;
+ var index = 0;
+ var shuffled = [];
+ each(obj, function(value) {
+ rand = _.random(index++);
+ shuffled[index - 1] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;
};
+ // An internal function to generate lookup iterators.
+ var lookupIterator = function(value) {
+ return _.isFunction(value) ? value : function(obj){ return obj[value]; };
+ };
+
// Sort the object's values by a criterion produced by an iterator.
- _.sortBy = function(obj, val, context) {
- var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
+ _.sortBy = function(obj, value, context) {
+ var iterator = lookupIterator(value);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value : value,
+ index : index,
criteria : iterator.call(context, value, index, list)
};
}).sort(function(left, right) {
- var a = left.criteria, b = right.criteria;
- if (a === void 0) return 1;
- if (b === void 0) return -1;
- return a < b ? -1 : a > b ? 1 : 0;
+ var a = left.criteria;
+ var b = right.criteria;
+ if (a !== b) {
+ if (a > b || a === void 0) return 1;
+ if (a < b || b === void 0) return -1;
+ }
+ return left.index < right.index ? -1 : 1;
}), 'value');
};
- // Groups the object's values by a criterion. Pass either a string attribute
- // to group by, or a function that returns the criterion.
- _.groupBy = function(obj, val) {
+ // An internal function used for aggregate "group by" operations.
+ var group = function(obj, value, context, behavior) {
var result = {};
- var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
+ var iterator = lookupIterator(value);
each(obj, function(value, index) {
- var key = iterator(value, index);
- (result[key] || (result[key] = [])).push(value);
+ var key = iterator.call(context, value, index, obj);
+ behavior(result, key, value);
});
return result;
};
- // Use a comparator function to figure out at what index an object should
- // be inserted so as to maintain order. Uses binary search.
- _.sortedIndex = function(array, obj, iterator) {
- iterator || (iterator = _.identity);
+ // Groups the object's values by a criterion. Pass either a string attribute
+ // to group by, or a function that returns the criterion.
+ _.groupBy = function(obj, value, context) {
+ return group(obj, value, context, function(result, key, value) {
+ (_.has(result, key) ? result[key] : (result[key] = [])).push(value);
+ });
+ };
+
+ // Counts instances of an object that group by a certain criterion. Pass
+ // either a string attribute to count by, or a function that returns the
+ // criterion.
+ _.countBy = function(obj, value, context) {
+ return group(obj, value, context, function(result, key, value) {
+ if (!_.has(result, key)) result[key] = 0;
+ result[key]++;
+ });
+ };
+
+ // Use a comparator function to figure out the smallest index at which
+ // an object should be inserted so as to maintain order. Uses binary search.
+ _.sortedIndex = function(array, obj, iterator, context) {
+ iterator = iterator == null ? _.identity : lookupIterator(iterator);
+ var value = iterator.call(context, obj);
var low = 0, high = array.length;
while (low < high) {
- var mid = (low + high) >> 1;
- iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
+ var mid = (low + high) >>> 1;
+ iterator.call(context, array[mid]) < value ? low = mid + 1 : high = mid;
}
return low;
};
// Safely convert anything iterable into a real, live array.
_.toArray = function(obj) {
- if (!obj) return [];
- if (_.isArray(obj)) return slice.call(obj);
- if (_.isArguments(obj)) return slice.call(obj);
- if (obj.toArray && _.isFunction(obj.toArray)) return obj.toArray();
+ if (!obj) return [];
+ if (obj.length === +obj.length) return slice.call(obj);
return _.values(obj);
};
// Return the number of elements in an object.
_.size = function(obj) {
- return _.isArray(obj) ? obj.length : _.keys(obj).length;
+ return (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
};
// Array Functions
@@ -327,7 +388,7 @@
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
};
- // Returns everything but the last entry of the array. Especcialy useful on
+ // Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N. The **guard** check allows it to work with
// `_.map`.
@@ -345,12 +406,12 @@
}
};
- // Returns everything but the first entry of the array. Aliased as `tail`.
- // Especially useful on the arguments object. Passing an **index** will return
- // the rest of the values in the array from that index onward. The **guard**
+ // Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
+ // Especially useful on the arguments object. Passing an **n** will return
+ // the rest N values in the array. The **guard**
// check allows it to work with `_.map`.
- _.rest = _.tail = function(array, index, guard) {
- return slice.call(array, (index == null) || guard ? 1 : index);
+ _.rest = _.tail = _.drop = function(array, n, guard) {
+ return slice.call(array, (n == null) || guard ? 1 : n);
};
// Trim out all falsy values from an array.
@@ -358,13 +419,21 @@
return _.filter(array, function(value){ return !!value; });
};
+ // Internal implementation of a recursive `flatten` function.
+ var flatten = function(input, shallow, output) {
+ each(input, function(value) {
+ if (_.isArray(value)) {
+ shallow ? push.apply(output, value) : flatten(value, shallow, output);
+ } else {
+ output.push(value);
+ }
+ });
+ return output;
+ };
+
// Return a completely flattened version of an array.
_.flatten = function(array, shallow) {
- return _.reduce(array, function(memo, value) {
- if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
- memo[memo.length] = value;
- return memo;
- }, []);
+ return flatten(array, shallow, []);
};
// Return a version of the array that does not contain the specified value(s).
@@ -375,30 +444,28 @@
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
- _.uniq = _.unique = function(array, isSorted, iterator) {
- var initial = iterator ? _.map(array, iterator) : array;
+ _.uniq = _.unique = function(array, isSorted, iterator, context) {
+ var initial = iterator ? _.map(array, iterator, context) : array;
var results = [];
- // The `isSorted` flag is irrelevant if the array only contains two elements.
- if (array.length < 3) isSorted = true;
- _.reduce(initial, function (memo, value, index) {
- if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
- memo.push(value);
+ var seen = [];
+ each(initial, function(value, index) {
+ if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.contains(seen, value)) {
+ seen.push(value);
results.push(array[index]);
}
- return memo;
- }, []);
+ });
return results;
};
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
- return _.uniq(_.flatten(arguments, true));
+ return _.uniq(concat.apply(ArrayProto, arguments));
};
// Produce an array that contains every item shared between all the
- // passed-in arrays. (Aliased as "intersect" for back-compat.)
- _.intersection = _.intersect = function(array) {
+ // passed-in arrays.
+ _.intersection = function(array) {
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
@@ -410,8 +477,8 @@
// Take the difference between one array and a number of other arrays.
// Only the elements present in just the first array will remain.
_.difference = function(array) {
- var rest = _.flatten(slice.call(arguments, 1), true);
- return _.filter(array, function(value){ return !_.include(rest, value); });
+ var rest = concat.apply(ArrayProto, slice.call(arguments, 1));
+ return _.filter(array, function(value){ return !_.contains(rest, value); });
};
// Zip together multiple lists into a single array -- elements that share
@@ -420,10 +487,27 @@
var args = slice.call(arguments);
var length = _.max(_.pluck(args, 'length'));
var results = new Array(length);
- for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
+ for (var i = 0; i < length; i++) {
+ results[i] = _.pluck(args, "" + i);
+ }
return results;
};
+ // Converts lists into objects. Pass either a single array of `[key, value]`
+ // pairs, or two parallel arrays of the same length -- one of keys, and one of
+ // the corresponding values.
+ _.object = function(list, values) {
+ var result = {};
+ for (var i = 0, l = list.length; i < l; i++) {
+ if (values) {
+ result[list[i]] = values[i];
+ } else {
+ result[list[i][0]] = list[i][1];
+ }
+ }
+ return result;
+ };
+
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
// we need this function. Return the position of the first occurrence of an
// item in an array, or -1 if the item is not included in the array.
@@ -432,22 +516,29 @@
// for **isSorted** to use binary search.
_.indexOf = function(array, item, isSorted) {
if (array == null) return -1;
- var i, l;
+ var i = 0, l = array.length;
if (isSorted) {
- i = _.sortedIndex(array, item);
- return array[i] === item ? i : -1;
+ if (typeof isSorted == 'number') {
+ i = (isSorted < 0 ? Math.max(0, l + isSorted) : isSorted);
+ } else {
+ i = _.sortedIndex(array, item);
+ return array[i] === item ? i : -1;
+ }
}
- if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
- for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
+ if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
+ for (; i < l; i++) if (array[i] === item) return i;
return -1;
};
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
- _.lastIndexOf = function(array, item) {
+ _.lastIndexOf = function(array, item, from) {
if (array == null) return -1;
- if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
- var i = array.length;
- while (i--) if (i in array && array[i] === item) return i;
+ var hasIndex = from != null;
+ if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) {
+ return hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item);
+ }
+ var i = (hasIndex ? from : array.length);
+ while (i--) if (array[i] === item) return i;
return -1;
};
@@ -539,17 +630,19 @@
context = this; args = arguments;
var later = function() {
timeout = null;