How to monitor a function for changes in Angular / breeze?

With the following code:

$scope.hasChanges = datacontext.manager.hasChanges;

I'm trying to give the view some insight into whether or not the current breeze context has any changes to enable/disable some buttons:

<button ng-disabled="!hasChanges" class="btn btn-warning cancel" ng-click="cancel()">Cancel</button>

This works if I set true/false in the view, but if I try to call hasChanges or haschanges(), the buttons are always disabled.

I also tried this:

 $scope.$watch(datacontext.manager.hasChanges, function () {
    $scope.hasChanges = datacontext.manager.hasChanges;

to no avail.

How can I tell the view when there are changes to the breeze hasChanges? In knockout I would just use an observable... I wonder what I'm doing wrong here.


The problem with $scope.hasChanges = datacontext.manager.hasChanges; is classic JavaScript. You're watching a function (hasChanges) that has been torn away from its owning object (the manager).

Try this instead:

$scope.isCancelDisabled = function () {return !datacontext.manager.hasChanges();};

Then write your html like this:

<button ng-disabled="isCancelDisabled()" ... ng-click="cancel()">Cancel</button>

Of course that will call datacontext.manager.hasChanges() frequently. It's pretty fast but you will be calling it roughly twice every digest cycle. If (and I mean "IF") you discover through measurement that this is too slow for your screen (again, I said "IF") ... you could turn cancelDisabled into a field of the VM and set it by listening to the hasChangesChanged event as Jay suggested. I'm not sure I'd bother.

The Breeze EntityManager has a "hasChangesChanged" event that you can subscribe to. Something like:

myEntityManager.hasChangesChanged.subscribe(function(args) {
    var hasChanges = args.hasChanges;
    var entityManager = args.entityManager;
    ... do something interesting...

The EntityManager also has an EntityChanged event that might be useful.

Also see: EntityManager api docs

Need Your Help

MySQL engine=InnoDB error 1005 errno 150

mysql foreign-keys innodb

I'm creating a small database so that I can get the hang of MySQL and SQL in general.

Adding a new line to SerializeArray output

javascript newline serializearray

I'm trying to create a form which returns a sum of the items selected (done) and a list of the items selected. My item values are formatted as follows:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.