-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngPluralize): add support for bind-once in count
#10022
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,9 @@ | |
</example> | ||
*/ | ||
var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interpolate) { | ||
var BRACE = /{}/g; | ||
var BRACE = /{}/g, | ||
IS_WHEN = /^when(Minus)?(.+)$/; | ||
|
||
return { | ||
restrict: 'EA', | ||
link: function(scope, element, attr) { | ||
|
@@ -184,34 +186,44 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp | |
whensExpFns = {}, | ||
startSymbol = $interpolate.startSymbol(), | ||
endSymbol = $interpolate.endSymbol(), | ||
isWhen = /^when(Minus)?(.+)$/; | ||
braceReplacement = startSymbol + numberExp + '-' + offset + endSymbol, | ||
watchRemover = angular.noop, | ||
lastCount; | ||
|
||
forEach(attr, function(expression, attributeName) { | ||
if (isWhen.test(attributeName)) { | ||
whens[lowercase(attributeName.replace('when', '').replace('Minus', '-'))] = | ||
element.attr(attr.$attr[attributeName]); | ||
var tmpMatch = IS_WHEN.exec(attributeName); | ||
if (tmpMatch) { | ||
var whenKey = (tmpMatch[1] ? '-' : '') + lowercase(tmpMatch[2]); | ||
whens[whenKey] = element.attr(attr.$attr[attributeName]); | ||
} | ||
}); | ||
forEach(whens, function(expression, key) { | ||
whensExpFns[key] = | ||
$interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' + | ||
offset + endSymbol)); | ||
whensExpFns[key] = $interpolate(expression.replace(BRACE, braceReplacement)); | ||
|
||
}); | ||
|
||
scope.$watch(function ngPluralizeWatch() { | ||
var value = parseFloat(scope.$eval(numberExp)); | ||
scope.$watch(numberExp, function ngPluralizeWatchAction(newVal) { | ||
var count = parseFloat(newVal); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that using an interceptor here would be better as there will be no need to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have to look into the interceptor concept; I am not familiar with it at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lgalfaso: Hm...I could really use a hint here. How could be use an interceptor and how would an interceptor make The only thing that I could think of and would make
But considering how "watch" functions should be as lightweight as possible, it seems clearly wrong to me to put an extra There is probably a way to let There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to do On Fri, Nov 14, 2014 at 11:45 AM, Georgios Kalpakas <
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I mean the function that converts the number into what at On Fri, Nov 14, 2014 at 6:16 PM, Lucas Mirelmann [email protected] wrote:
|
||
var countIsNaN = isNaN(count); | ||
|
||
if (!isNaN(value)) { | ||
//if explicit number rule such as 1, 2, 3... is defined, just use it. Otherwise, | ||
//check it against pluralization rules in $locale service | ||
if (!(value in whens)) value = $locale.pluralCat(value - offset); | ||
return whensExpFns[value](scope); | ||
} else { | ||
return ''; | ||
if (!countIsNaN && !(count in whens)) { | ||
// If an explicit number rule such as 1, 2, 3... is defined, just use it. | ||
// Otherwise, check it against pluralization rules in $locale service. | ||
count = $locale.pluralCat(count - offset); | ||
} | ||
|
||
// If both `count` and `lastCount` are NaN, we don't need to re-register a watch. | ||
// In JS `NaN !== NaN`, so we have to exlicitly check. | ||
if ((count !== lastCount) && !(countIsNaN && isNaN(lastCount))) { | ||
watchRemover(); | ||
watchRemover = scope.$watch(whensExpFns[count], updateElementText); | ||
lastCount = count; | ||
} | ||
}, function ngPluralizeWatchAction(newVal) { | ||
element.text(newVal); | ||
}); | ||
|
||
function updateElementText(newText) { | ||
element.text(newText || ''); | ||
} | ||
} | ||
}; | ||
}]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
whens
was created withcreateMap()
thenforEach()
will break :(There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe default to
[]
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or
{}
I guessThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively, you could remove the
forEach()
and just do itObject.keys()-for-loop
style.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True !
But shouldn't we rather be fixing
forEach()
?(I.e. replacing
obj.hasOwnProperty(...)
withhasOwnProperty.call(obj, ...)
at https://github.com/angular/angular.js/blob/master/src/Angular.js#L265)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't care a whole lot about that --- @petebacondarwin would you be pro-refactoring forEach?