0

使用以下代码:

$scope.hasChanges = datacontext.manager.hasChanges;

我试图让视图了解当前微风上下文是否有任何更改以启用/禁用某些按钮:

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

如果我在视图中设置 true/false,则此方法有效,但如果我尝试调用 hasChanges 或 haschanges(),则始终禁用按钮。

我也试过这个:

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

无济于事。

当微风的 hasChanges 发生变化时,如何告诉视图?在淘汰赛中,我只会使用一个可观察的......我想知道我在这里做错了什么。

4

2 回答 2

2

Breeze EntityManager 有一个您可以订阅的“hasChangesChanged”事件。就像是:

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

EntityManager 还有一个可能有用的 EntityChanged 事件。

另请参阅:EntityManager api 文档

于 2013-08-08T17:28:07.167 回答
1

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.

于 2013-08-09T08:51:43.470 回答