123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 'use strict';
- define(['../../lib/@waca/dashboard-common/dist/core/Collection', '../../lib/@waca/core-client/js/core-client/errors/BaseError'], function (Collection, BaseError) {
-
- var ServerCollection = Collection.extend({
- init: function init(models, options) {
- ServerCollection.inherited('init', this, arguments);
- this._ajaxSvc = options.ajaxSvc;
- },
-
- 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));
- });
- });
- },
-
- 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));
- });
- });
- },
-
- 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;
- });
|