Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$scope.$watch listener expressions can access $newVal & $oldVal #1708

Closed
wants to merge 1 commit into from

Conversation

flazz
Copy link

@flazz flazz commented Dec 15, 2012

a string listener expression can evaluate $newVal and $oldVal

example:

$scope.$watch('foo', 'foo = $newVal');

a string listener expression can evaluate $newVal and $oldVal

example:

```javascript
$scope.$watch('foo', 'foo = $newVal');
```
@pkozlowski-opensource
Copy link
Member

@flazz what is the motivation behind this change? If I understand correctly it just makes it easier for people to write JavaScript functions as strings (expressions). I've got my private opinion on this but before sharing it I would love to hear more about your use-case here.

@petebacondarwin
Copy link
Contributor

So there is the question of whether it is a good idea to allow strings as listeners at all!
If AngularJS is going to continue to support string listeners in the future then I think this is a good addition.

@flazz
Copy link
Author

flazz commented Dec 17, 2012

My biggest practical use case (to date) is sample above.

Also the case where the watchExpression is a function and not a simple expression, returning a value not in scope.

I like @peterbacondarwin's reasoning, I feel without access to old and new there is a half feature. How much value does the full feature provide?.

Also is $newVal and $oldVal the best naming aesthetics? would $newValue or $new suffice better?

@pkozlowski-opensource
Copy link
Member

@flazz I'm not sure I understand your example (please bear with me...). Are you talking about this one:

$scope.$watch('foo', 'foo = $newVal');

If so, it seems to be assigning a new value value to a variable... that its changing its value to a new one anyway (this is why the watcher is executing in the first place). I'm sure that I'm missing something here so if you could provide a practical use-case it would be awesome!

@petebacondarwin
Copy link
Contributor

This is effectively a no-op!

$scope.$watch('foo', 'foo = $newVal');

I could imagine calling a function...

$scope.$watch('foo', 'saveModel($newVal, $oldVal)');

Also, I think that there should also be a unit test where $oldVal was not undefined.

@jtymes
Copy link
Contributor

jtymes commented Dec 17, 2012

@petebacondarwin Wouldn't your second example already be taken care of by the $watch function?

$scope.$watch('foo', saveModel);

Where saveModel is defined as saveModel(newVal, oldVal)

@pkozlowski-opensource
Copy link
Member

@petebacondarwin This is exactly my point, the first example does nothing... unless I'm mistaken.

While I could also imagine this call $scope.$watch('foo', 'saveModel($newVal, $oldVal)'); I'm not sure what is the added value of this as compared to simply writing:

$scope.$watch('foo', $scope.saveModel);

There must be something I'm missing here...

@petebacondarwin
Copy link
Contributor

To some extent I agree, hence my thought that string based listeners could be dropped altogether. But in defence of my example, you may be passed a string for some reason - perhaps as an attribute on a directive.

@flazz
Copy link
Author

flazz commented Dec 18, 2012

@petebacondarwin the below case illustrates a non-noop case

$scope.watch(function(_scope) { return myService.val() }, 'scope.x = $newVal');

This could easily be written with the listener as a function. Is there a use to string expression listeners beyond a shorthand sugar?

@pkozlowski-opensource
Copy link
Member

@flazz for me it just moves to string something that could be written as a normal function. So it would be -1 from me but I might be missing a good use-case here...

@IgorMinar could you have a quick look and have a final word here?

@IgorMinar
Copy link
Contributor

-1

we should not be encouraging people to put code into strings within javascript files.

closing...

@IgorMinar IgorMinar closed this Jan 8, 2013
@petebacondarwin
Copy link
Contributor

Then should we not deprecate the use of strings as listeners altogether?

On 8 January 2013 21:53, Igor Minar [email protected] wrote:

-1

we should not be encouraging people to put code into strings within
javascript files.

closing...


Reply to this email directly or view it on GitHubhttps://github.com//pull/1708#issuecomment-12019455.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants