How can I detect a change to an entity's EntityState?

后端 未结 2 1114
失恋的感觉
失恋的感觉 2020-12-31 20:24

I want to put a \"Delete\" button and a \"Cancel\" button on each row of a list of Customers. The \"Cancel\" button is disabled when a customer is \"Unchanged\". But when a

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 21:18

    We found wrapping the breeze entity manager inside our own gives us nice flexibility that makes this challenge a walk in the park, especially with the hasChangesChanged event.

    var EntityManager = (function () {
        function EntityManager(breezeEntityManager) {
            this.breezeEntityManager = breezeEntityManager;
            this.hasChanges = ko.observable(breezeEntityManager.hasChanges());
    
            // Subscribe with handler watching for EntityState changes
            this.addEntityStateChangeTracking(breezeEntityManager, this);
        }
    
        EntityManager.prototype.addEntityStateChangeTracking = function (bem, em) {
            if (this.entityStateTrackingToken != null) return;
            this.entityStateTrackingToken = bem.hasChangesChanged.subscribe(function (changeArgs) {
                em.hasChanges(changeArgs.hasChanges);
            });
        };
        return EntityManager;
    })();
    

    Then on your viewModels, expose the EntityManager.

    var ViewModel = (function (_super) {
        __extends(ViewModel, _super);
        function ViewModel(typeName) {
            _super.call(this);
            this.type = entities.getType(typeName);
        }
    
        ViewModel.prototype.loadEntity = function (id) {
            this.entityManager = new breeze.EntityManager("MyManager");;
        };
        return ViewModel;
    })(ViewModelBase);
    exports.ViewModel = ViewModel;
    

    Then in your Knockout UI:

提交回复
热议问题