123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- '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
|