'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(['../../lib/@waca/dashboard-common/dist/core/Collection', '../../lib/@waca/core-client/js/core-client/errors/BaseError'], function (Collection, BaseError) { /** * A collection which is created from the server. For example, if we had a server collection * called ThingsCollection and it specified a URL of /things then if we did: * * new ThingsCollection().fetch(); * * Then that collection would make a call to: * * GET /things * * And materialize the collection from the results of that call. * * Overriders of this class should define a url property, e.g: * * ThingsCollection = ServerCollection.extend({ * * url: '/things', * init: function() { * ... * }, * * ... * }); * */ var ServerCollection = Collection.extend({ init: function init(models, options) { ServerCollection.inherited('init', this, arguments); this._ajaxSvc = options.ajaxSvc; }, /** * Get all the models in the collection from the server. Returns a promise which * is resolved when the collection has been reset with the result from the server. */ fetch: function fetch() { var _this = this; return new Promise(function (resolve, reject) { _this._ajaxSvc.ajax({ url: _this.url, type: 'GET', cache: false, headers: { Accept: 'application/json' } }).then(function (models) { _this.reset(models); resolve(models); }).catch(function (error) { reject(new BaseError(error)); }); }); }, /** * Fetch a model with the given ID and add it to the collection */ fetchModel: function fetchModel(id) { var _this2 = this; return new Promise(function (resolve, reject) { _this2._ajaxSvc.ajax({ url: _this2.url + '/' + id, type: 'GET', headers: { Accept: 'application/json' } }).then(function (model) { if (model) { _this2.add(model); } resolve(_this2.get(model)); }).catch(function (error) { reject(new BaseError(error)); }); }); }, /** * Adds a new model to the collection by POSTing it to the collection. * */ addModel: function addModel(model) { var _this3 = this; return new Promise(function (resolve, reject) { _this3._ajaxSvc.ajax({ url: _this3.url, type: 'POST', contentType: 'application/json', processData: false, dataType: 'json', data: JSON.stringify(model), headers: { Accept: 'application/json' } }).then(function (serverModel) { if (serverModel) { _this3.add(serverModel); } resolve(_this3.get(serverModel)); }).catch(function (error) { reject(new BaseError(error)); }); }); } }); return ServerCollection; }); //# sourceMappingURL=ServerCollection.js.map