UNPKG

fh-wfm-user

Version:
96 lines (81 loc) 1.92 kB
'use strict'; var q = require('q'); var _ = require('lodash'); var config = require('../../config/config-group'); var groupClient; var GroupClient = function(mediator) { this.mediator = mediator; this.initComplete = false; this.initPromise = this.init(); }; var xhr = function(_options) { var defaultOptions = { path: '/', method: 'get', contentType: 'application/json' }; var options = _.defaults(_options, defaultOptions); var deferred = q.defer(); $fh.cloud(options, function(res) { deferred.resolve(res); }, function(message, props) { var e = new Error(message); e.props = props; deferred.reject(e); }); return deferred.promise; }; GroupClient.prototype.init = function() { var deferred = q.defer(); var self = this; $fh.on('fhinit', function(error) { if (error) { deferred.reject(new Error(error)); return; } self.appid = $fh.getFHParams().appid; self.initComplete = true; deferred.resolve(); }); return deferred.promise; }; GroupClient.prototype.list = function() { return xhr({ path: config.apiPath }); }; GroupClient.prototype.read = function(id) { return xhr({ path: config.apiPath + '/' + id }); }; GroupClient.prototype.update = function(group) { return xhr({ path: config.apiPath + '/' + group.id, method: 'put', data: group }); }; GroupClient.prototype.create = function(group) { return xhr({ path: config.apiPath, method: 'post', data: group }); }; GroupClient.prototype.delete = function(group) { return xhr({ path: config.apiPath + '/' + group.id, method: 'delete', data: group }); }; module.exports = function(mediator) { //Only want a single group client per application, //If one already exists, use this one. if (groupClient) { return groupClient; } groupClient = new GroupClient(mediator); return groupClient; };