FlaresAPI service reference - alansfyeung/flares GitHub Wiki
Injecting
var flaresApp = angular.module('someModule', ['flaresBase']);
var flaresController = flaresApp.controller('someController', ['flaresBase', ... , function(flaresAPI, ...){ ... }])
Note: no preceding $ sign when injecting flaresAPI into controller
Factorying
flaresAPI( defined-api-name )
where defined-api-name is [ 'member' | 'activity' | 'refData' ]
Calling an operation
flaresAPI('activity').get([_*fragments_]).then(function(response){ ... }, ... )
Operations are:
FlaresAPI.prototype.getAll = function(params){ // don't expect ID
return $http.get(this._buildEndpoint(), params);
};
FlaresAPI.prototype.get = function(parts, params){
return $http.get(this._buildEndpoint(parts), params);
};
FlaresAPI.prototype.post = function(data, params){ // don't expect ID
return $http.post(this._buildEndpoint(parts), data, params);
};
FlaresAPI.prototype.put = function(parts, data, params){
return $http.put(this._buildEndpoint(parts), data, params);
};
FlaresAPI.prototype.patch = function(parts, data, params){
return $http.patch(this._buildEndpoint(parts), data, params);
};
FlaresAPI.prototype.delete = function(parts, params){
return $http.delete(this._buildEndpoint(parts), params);
};
Each operation is a shortcut for calling Angular's $http service and will return a thenable.
Defining sub-resources
For calling sub-resources related to the first-level resource, firstly each sub-resource route name needs to be defined in flaresBase-services.js
e.g. The following defines 3 sub-resources for member:
if (className === 'member'){
return new FlaresAPI('/api/member', ['posting', 'picture', 'status']);
}
A sub-resource is accessed like this:
`flaresAPI('member').postingFor($scope.member.regt_num).post({context: $scope.dischargeContext}).then( ... )`
The fluent method name is the subresource name plus the word 'For', e.g.
`postingFor( resourceId )`