2017-02-07 35 views
0

Я хочу создать перехватчик, который перехватит функцию сохранения Breezejs и хотел бы создать больше объектов сущности и добавить их в SaveBundle, прежде чем, наконец, BreezeController's SaveChanges функционирует вместе с другими объектами.Добавьте еще объекты entitiy в SaveBundle of Breezejs при перехвате функции Save с помощью Angularjs

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 и поместить больше объектов в свойства данных, которые передаются на сервер?

ответ

0

Обычный способ сделать это - использовать класс обертки (например, UnitOfWork) вокруг EntityManager. Собственный метод SaveChanges обертки будет выполнять любые специальные манипуляции с объектами до вызова 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 непосредственно.