blob: c92a8705873d79f328c63d20f002d927fd716a01 [file] [log] [blame]
Matteo Scandolo6be0cd22015-11-03 16:27:07 +01001/*jshint -W069 */
2/*global angular:false */
3angular.module('{{&moduleName}}', [])
4.factory('{{&className}}', ['$q', '$http', '$rootScope', function($q, $http, $rootScope){
5 'use strict';
6
7 /**
8 * {{&description}}
9 * @class {{&className}}
10 * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
11 * @param {string} [domainOrOptions.domain] - The project domain
12 * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
13 * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
14 * @param {string} [cache] - An angularjs cache implementation
15 */
16 var {{&className}} = (function(){
17 function {{&className}}(options, cache){
18 var domain = (typeof options === 'object') ? options.domain : options;
19 this.domain = typeof(domain) === 'string' ? domain : '{{&domain}}';
20 cache = cache || ((typeof options === 'object') ? options.cache : cache);
21 this.cache = cache;
22 {{#isSecure}}
23 this.token = (typeof options === 'object') ? (options.token ? options.token : {}) : {};
24 {{/isSecure}}
25 }
26
27 {{&className}}.prototype.$on = function($scope, path, handler){
28 var url = domain + path;
29 $scope.$on(url, function(){
30 handler();
31 });
32 return this;
33 };
34
35 {{&className}}.prototype.$broadcast = function(path){
36 var url = domain + path;
37 //cache.remove(url);
38 $rootScope.$broadcast(url);
39 return this;
40 };
41
42 {{&className}}.transformRequest = function(obj) {
43 var str = [];
44 for(var p in obj) {
45 var val = obj[p];
46 if(angular.isArray(val)) {
47 val.forEach(function(val){
48 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
49 });
50 } else {
51 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
52 }
53 }
54 return str.join("&");
55 };
56
57 {{#isSecure}}
58 /**
59 * Set Token
60 * @method
61 * @name {{&className}}#setToken
62 * @param {string} value - token's value
63 * @param {string} headerOrQueryName - the header or query name to send the token at
64 * @param {boolean} isQuery - true if send the token as query param, otherwise, send as header param
65 *
66 */
67 {{&className}}.prototype.setToken = function (value, headerOrQueryName, isQuery) {
68 this.token.value = value;
69 this.token.headerOrQueryName = headerOrQueryName;
70 this.token.isQuery = isQuery;
71 };
72 {{/isSecure}}
73
74 {{#methods}}
75 {{> method}}
76 {{/methods}}
77
78 return {{&className}};
79 })();
80
81 return {{&className}};
82}]);