aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgor Minar2011-03-27 15:58:24 -0700
committerIgor Minar2011-03-30 15:24:03 -0700
commit96a1df192a167e6e34988af687693506f4efd1d1 (patch)
treedf4603b0e1043500aaecc5c51a257371ad9f2732 /src
parent89c25fe7136e49faa88a4b2a1922c822a5470313 (diff)
downloadangular.js-96a1df192a167e6e34988af687693506f4efd1d1.tar.bz2
extend size() to take ownPropsOnly param
- extend size() to take size(obj, ownPropsOnly) - add specs for size() - update docs to mention string support - use size() in ng:repeat including the hasOwnProp check for all object doesn't create significant perf penalty: http://jsperf.com/dedicated-code-branch-for-hasownprop
Diffstat (limited to 'src')
-rw-r--r--src/Angular.js11
-rw-r--r--src/widgets.js11
2 files changed, 8 insertions, 14 deletions
diff --git a/src/Angular.js b/src/Angular.js
index c815d5fc..f4cb9b51 100644
--- a/src/Angular.js
+++ b/src/Angular.js
@@ -461,12 +461,14 @@ function map(obj, iterator, context) {
* @function
*
* @description
- * Determines the number of elements in an array or number of properties of an object.
+ * Determines the number of elements in an array, number of properties of an object or string
+ * length.
*
* Note: this function is used to augment the Object type in angular expressions. See
* {@link angular.Object} for more info.
*
- * @param {Object|Array} obj Object or array to inspect.
+ * @param {Object|Array|string} obj Object, array or string to inspect.
+ * @param {boolean} [ownPropsOnly=false] Count only "own" properties in an object
* @returns {number} The size of `obj` or `0` if `obj` is neither an object or an array.
*
* @example
@@ -483,14 +485,15 @@ function map(obj, iterator, context) {
* </doc:scenario>
* </doc:example>
*/
-function size(obj) {
+function size(obj, ownPropsOnly) {
var size = 0, key;
if (obj) {
if (isNumber(obj.length)) {
return obj.length;
} else if (isObject(obj)){
for (key in obj)
- size++;
+ if (!ownPropsOnly || obj.hasOwnProperty(key))
+ size++;
}
}
return size;
diff --git a/src/widgets.js b/src/widgets.js
index a5a8ee79..a7d59289 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -913,19 +913,10 @@ angularWidget('@ng:repeat', function(expression, element){
childCount = children.length,
lastIterElement = iterStartElement,
collection = this.$tryEval(rhs, iterStartElement),
- is_array = isArray(collection),
- collectionLength = 0,
+ collectionLength = size(collection, true),
childScope,
key;
- if (is_array) {
- collectionLength = collection.length;
- } else {
- for (key in collection)
- if (collection.hasOwnProperty(key))
- collectionLength++;
- }
-
for (key in collection) {
if (collection.hasOwnProperty(key)) {
if (index < childCount) {
function(text, textNode, parentElement) { var bindings = parseBindings(text), self = this; if (hasBindings(bindings)) { if (isLeafNode(parentElement[0])) { parentElement.attr('ng-bind-template', text); } else { var cursor = textNode, newElement; foreach(parseBindings(text), function(text){ var exp = binding(text); if (exp) { newElement = self.element('span'); newElement.attr('ng-bind', exp); } else { newElement = self.text(text); } cursor.after(newElement); cursor = newElement; }); } textNode.remove(); } }); // TODO: this should be widget not a markup angularTextMarkup('OPTION', function(text, textNode, parentElement){ if (parentElement[0].nodeName == "OPTION") { var select = document.createElement('select'); select.insertBefore(parentElement[0].cloneNode(true), null); if (!select.innerHTML.match(/<option(\s.*\s|\s)value\s*=\s*.*>.*<\/\s*option\s*>/gi)) { parentElement.attr('value', text); } } }); var NG_BIND_ATTR = 'ng-bind-attr'; angularAttrMarkup('{{}}', function(value, name, element){ if (name.substr(0, 3) != 'ng-') { var bindings = parseBindings(value), bindAttr; if (hasBindings(bindings)) { element.removeAttr(name); bindAttr = fromJson(element.attr(NG_BIND_ATTR) || "{}"); bindAttr[name] = value; element.attr(NG_BIND_ATTR, toJson(bindAttr)); } } });