electricessence / TypeScript.NET

A JavaScript-Friendly .NET Based TypeScript Library (Moved)
https://github.com/electricessence/TypeScript.NET-Core
Other
251 stars 36 forks source link

Watch an array modifications #51

Closed lommez closed 7 years ago

lommez commented 7 years ago

Hi,

For example, i have an array like that:

var numbersArray = [1, 2, 3, 5, 6]; var myEnumerable = Enumerable.from(numberArray); function total(): number { return myEnumerable.sum(); }

numberArray.push(7);

In my application i'm using angular and binding the total result in the view. Is possible whenever the numbersArray suffer any modification to be updated in the view?

electricessence commented 7 years ago

Well that's more of an Angular How-To question.

But let's start with expected behavior of the above code.

Everytime total() is called, .sum() creates a new ArrayEnumerator and walks through the array creates a sum. Then any objects that were related to that enumeration are disposed.

The key is, that the function total is actually being called. "Binding" to total may not be doing what you think.

When I had a changing value like you are describing, there still needed to be an event that triggered the change, or you are binding angular to an object/model that it can track changes.

You can't track changes on a function because the function doesn't change itself. Something has to 'update' the model that angular is 'watching'.

lommez commented 7 years ago

Thanks for tips.. I've implemented a 'watch' to detect changes and now is working!

electricessence commented 7 years ago

Yes. That's exactly how we had to do it. But just be very careful. Watches can get out of hand. If you have to add more than 3 of them, you probably did something wrong. :P