mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-13 17:23:11 +00:00
Fire $eval only if the value has actually changed on input
This commit is contained in:
parent
e84d3334b0
commit
b225083a21
2 changed files with 18 additions and 6 deletions
|
|
@ -373,7 +373,7 @@ function optionsAccessor(scope, element) {
|
||||||
|
|
||||||
function noopAccessor() { return { get: noop, set: noop }; }
|
function noopAccessor() { return { get: noop, set: noop }; }
|
||||||
|
|
||||||
var textWidget = inputWidget('keyup change', modelAccessor, valueAccessor, initWidgetValue()),
|
var textWidget = inputWidget('keyup change', modelAccessor, valueAccessor, initWidgetValue(), true),
|
||||||
buttonWidget = inputWidget('click', noopAccessor, noopAccessor, noop),
|
buttonWidget = inputWidget('click', noopAccessor, noopAccessor, noop),
|
||||||
INPUT_TYPE = {
|
INPUT_TYPE = {
|
||||||
'text': textWidget,
|
'text': textWidget,
|
||||||
|
|
@ -451,7 +451,7 @@ function radioInit(model, view, element) {
|
||||||
expect(binding('checkboxCount')).toBe('1');
|
expect(binding('checkboxCount')).toBe('1');
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
function inputWidget(events, modelAccessor, viewAccessor, initFn) {
|
function inputWidget(events, modelAccessor, viewAccessor, initFn, dirtyChecking) {
|
||||||
return function(element) {
|
return function(element) {
|
||||||
var scope = this,
|
var scope = this,
|
||||||
model = modelAccessor(scope, element),
|
model = modelAccessor(scope, element),
|
||||||
|
|
@ -463,10 +463,13 @@ function inputWidget(events, modelAccessor, viewAccessor, initFn) {
|
||||||
// Don't register a handler if we are a button (noopAccessor) and there is no action
|
// Don't register a handler if we are a button (noopAccessor) and there is no action
|
||||||
if (action || modelAccessor !== noopAccessor) {
|
if (action || modelAccessor !== noopAccessor) {
|
||||||
element.bind(events, function (){
|
element.bind(events, function (){
|
||||||
model.set(view.get());
|
var value = view.get();
|
||||||
lastValue = model.get();
|
if (!dirtyChecking || value != lastValue) {
|
||||||
scope.$tryEval(action, element);
|
model.set(value);
|
||||||
scope.$root.$eval();
|
lastValue = model.get();
|
||||||
|
scope.$tryEval(action, element);
|
||||||
|
scope.$root.$eval();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
scope.$watch(model.get, function(value){
|
scope.$watch(model.get, function(value){
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,15 @@ describe("widget", function(){
|
||||||
expect(scope.$get('count')).toEqual(2);
|
expect(scope.$get('count')).toEqual(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not trigger eval if value does not change', function(){
|
||||||
|
compile('<input type="Text" name="name" value="Misko" ng:change="count = count + 1" ng:init="count=0"/>');
|
||||||
|
expect(scope.name).toEqual("Misko");
|
||||||
|
expect(scope.count).toEqual(0);
|
||||||
|
browserTrigger(element, 'keyup');
|
||||||
|
expect(scope.name).toEqual("Misko");
|
||||||
|
expect(scope.count).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('should allow complex refernce binding', function(){
|
it('should allow complex refernce binding', function(){
|
||||||
compile('<div ng:init="obj={abc:{}}">'+
|
compile('<div ng:init="obj={abc:{}}">'+
|
||||||
'<input type="Text" name="obj[\'abc\'].name" value="Misko""/>'+
|
'<input type="Text" name="obj[\'abc\'].name" value="Misko""/>'+
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue