12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 'use strict';
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2017
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['underscore', '../../lib/@waca/dashboard-common/dist/core/Collection'], function (_, Collection) {
- var TransactionalCollection = null;
- /**
- * A collection which allow changes to be made within the context of a transaction
- *
- * @see Collection
- *
- */
- TransactionalCollection = Collection.extend({
- init: function init() {
- TransactionalCollection.inherited('init', this, arguments);
- },
- /**
- * Start a transaction on this collection. During a transaction modifications will be silent and the
- * user of the collection can be notified of all the changes during the transaction at the end of the transaction
- * by calling stopTransaction(...)
- */
- startTransaction: function startTransaction() {
- if (this._transactionLog) {
- throw new Error('A transaction is already started');
- }
- this._transactionLog = new Collection(this.toJSON(), { modelClass: this.model });
- },
- /**
- * Stop the transaction on this collection. If changes were made to this collection in the context of the transaction
- * then a 'reset' event will be emitted describing the changes.
- */
- stopTransaction: function stopTransaction() {
- if (!this._transactionLog) {
- throw new Error('No transaction to stop');
- }
- var transactionLog = this._transactionLog;
- this._transactionLog = null;
- TransactionalCollection.inherited('reset', this, [transactionLog.toJSON()]);
- }
- });
- // Mix-in an override for all the collection methods that delegate to the transaction log collection when
- // we are in the context of a transaction
- var collectionFns = _.union(['add', 'remove', 'reset', 'set', 'getModels', 'get', 'toJSON'], Collection._underscoreMethods, Collection._underscoreAttributeMethods);
- _.each(collectionFns, function (method) {
- TransactionalCollection.prototype[method] = function () {
- if (this._transactionLog) {
- return this._transactionLog[method].apply(this._transactionLog, arguments);
- }
- return TransactionalCollection.inherited(method, this, arguments);
- };
- });
- return TransactionalCollection;
- });
- //# sourceMappingURL=TransactionalCollection.js.map
|