Javascript 在使用Angularjs截取保存函数时,向Breezejs的SaveBundle添加更多实体对象

Javascript 在使用Angularjs截取保存函数时,向Breezejs的SaveBundle添加更多实体对象,javascript,angularjs,breeze,Javascript,Angularjs,Breeze,我希望创建一个拦截器,它将拦截Breezejs的保存功能,并希望创建更多实体对象并将它们添加到SaveBundle中,然后最终让它与其他对象一起进入BreezeController的SaveChanges功能 app.factory('changeHistoryInterceptor', ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) { var deferred = null, //promise

我希望创建一个拦截器,它将拦截Breezejs的保存功能,并希望创建更多实体对象并将它们添加到SaveBundle中,然后最终让它与其他对象一起进入BreezeController的
SaveChanges
功能

app.factory('changeHistoryInterceptor', ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
    var deferred = null, //promise to be returned
        rConfig = null, //config to be returned
        historySectionSet = false, 
        rDatacontext = null;

    var Interceptor = {
        request: function (config) {
            deferred = $q.defer();
            rConfig = config;

            if (historySectionSet && config.url.contains('SaveChanges') && rDatacontext && rDatacontext.hasChanges()) {
                $timeout(function () {
                  //I wish to create more entity objects and add them into SaveBundle on listening to this event
                  // so that it can be carried to server along with other objects
                  $rootScope.$broadcast('prepareHistory', { currentDate: new Date() });
                }, 5);                                
                return deferred.promise;
            } 
            else
                return config;
        },
        response: function (res) {
            return res;
        },
        goThrough: function () {
            // called from directive's controller on successfully creation of more entity objects
            // how to access SaveBundle and add those created objects in SaveBundle?
            if (deferred) {
                historySectionSet = false;
                deferred.resolve(rConfig);
                deferred = null; rConfig = null;
            }
        },
        setHistorySection: function (setSection, datacontext) {
            historySectionSet = setSection;
            rDatacontext = datacontext;
        }
    }

    return Interceptor;
}]);

是否仍然可以访问SaveBundle并将更多对象放入传递到服务器的数据属性中

通常的方法是在EntityManager周围使用包装类(例如UnitOfWork)。包装器自己的
SaveChanges
方法将在调用EntityManager之前对实体执行任何特殊操作

function SaveChanges() {
    // get array of changed entities
    var changes = _entityManager.getChanges();
    // push additional entities onto the array
    addMoreEntities(changes);
    // save it all.  Note we pass the changes as a parameter
    return _entityManager.saveChanges(changes);
}
然后,您的应用程序将使用此
SaveChanges
功能,而不是直接调用EntityManager.SaveChanges