Skip to content

Commit ce4e14f

Browse files
committed
wip: Move to service structure.
The idea es to have RestangularService instead of the service object result of createServiceForConfiguration. Instead, we use new RestangularService(config). Also, RestangularElement & RestangularCollection should keep a reference of the RestangularService, which they will use to forward several calls.
1 parent bf11bd4 commit ce4e14f

File tree

5 files changed

+287
-271
lines changed

5 files changed

+287
-271
lines changed

src/models/base.js

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,119 @@
11

22
var RestangularBase = {
33
getRestangularUrl: function() {
4-
return this.$urlHandler.fetchUrl(this);
4+
return this.$service.urlHandler.fetchUrl(this);
55
},
66
getRequestedUrl: function() {
7-
return this.$urlHandler.fetchRequestedUrl(this);
7+
return this.$service.urlHandler.fetchRequestedUrl(this);
88
},
99
clone: function() {
1010
var copiedElement = _.cloneDeep(this);
1111
// TODO remove restangularizeElem() call
12-
return this.$restangularizeElem(copiedElement[this.$config.restangularFields.parentResource],
12+
return this.$service.restangularizeElem(copiedElement[this.$config.restangularFields.parentResource],
1313
copiedElement, copiedElement[this.$config.restangularFields.route], true);
14-
}
14+
},
15+
16+
addRestangularMethod: function (name, operation, path, defaultParams, defaultHeaders, defaultElem) {
17+
var bindedFunction;
18+
if (operation === 'getList') {
19+
bindedFunction = _.bind(fetchFunction, this, path);
20+
} else {
21+
bindedFunction = _.bind(customFunction, this, operation, path);
22+
}
23+
24+
var createdFunction = function(params, headers, elem) {
25+
var callParams = _.defaults({
26+
params: params,
27+
headers: headers,
28+
elem: elem
29+
}, {
30+
params: defaultParams,
31+
headers: defaultHeaders,
32+
elem: defaultElem
33+
});
34+
return bindedFunction(callParams.params, callParams.headers, callParams.elem);
35+
};
36+
37+
if (this.$config.isSafe(operation)) {
38+
this[name] = createdFunction;
39+
} else {
40+
this[name] = function(elem, params, headers) {
41+
return createdFunction(params, headers, elem);
42+
};
43+
}
44+
},
45+
46+
plain: function() {
47+
this.$service.stripRestangular(this);
48+
},
49+
50+
one: function(/*parent, route, id, singleOne*/) {
51+
return this.$service.one.apply(this.$service, [this].concat(arguments));
52+
},
53+
54+
all:function(/*parent, route*/) {
55+
return this.$service.all.apply(this.$service, [this].concat(arguments));
56+
},
57+
58+
several: function(/*parent, route , ..ids */) {
59+
return this.$service.serveral.apply(this.$service, [this].concat(arguments));
60+
},
61+
62+
oneUrl: function(/*parent, route, url*/) {
63+
return this.$service.oneUrl.apply(this.$service, [this].concat(arguments));
64+
},
65+
66+
allUrl: function(/*parent, route, url*/) {
67+
return this.$service.allUrl.apply(this.$service, [this].concat(arguments));
68+
},
69+
70+
remove:function (params, headers) {
71+
return _.bind(elemFunction, this)('remove', undefined, params, undefined, headers);
72+
},
73+
74+
head: function (params, headers) {
75+
return _.bind(elemFunction, this)('head', undefined, params, undefined, headers);
76+
},
77+
78+
trace: function (params, headers) {
79+
return _.bind(elemFunction, this)('trace', undefined, params, undefined, headers);
80+
},
81+
82+
options: function (params, headers) {
83+
return _.bind(elemFunction, this)('options', undefined, params, undefined, headers);
84+
},
85+
86+
patch: function (elem, params, headers) {
87+
return _.bind(elemFunction, this)('patch', undefined, params, elem, headers);
88+
},
89+
90+
restangularized: true
1591

1692
};
93+
94+
function addCustomOperation(elem) {
95+
elem[config.restangularFields.customOperation] = _.bind(customFunction, elem);
96+
_.each(['put', 'post', 'get', 'delete'], function(oper) {
97+
_.each(['do', 'custom'], function(alias) {
98+
var callOperation = oper === 'delete' ? 'remove' : oper;
99+
var name = alias + oper.toUpperCase();
100+
var callFunction;
101+
102+
if (callOperation !== 'put' && callOperation !== 'post') {
103+
callFunction = customFunction;
104+
} else {
105+
callFunction = function(operation, elem, path, params, headers) {
106+
return _.bind(customFunction, this)(operation, path, params, headers, elem);
107+
};
108+
}
109+
elem[name] = _.bind(callFunction, elem, callOperation);
110+
});
111+
});
112+
elem[config.restangularFields.customGETLIST] = _.bind(fetchFunction, elem);
113+
elem[config.restangularFields.doGETLIST] = elem[config.restangularFields.customGETLIST];
114+
}
115+
116+
117+
function customFunction(operation, path, params, headers, elem) {
118+
return _.bind(elemFunction, this)(operation, path, params, elem, headers);
119+
}

src/models/collection.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11

2-
function RestangularCollection(config, urlHandler) {
3-
this.$config = config;
4-
this.$urlHandler = urlHandler;
2+
function RestangularCollection(service) {
3+
this.$service = service;
4+
this.$config = service.config;
55
// route y reqParams restangularized fromServer
66
}
77

88
// TODO proper way of doing inheritance or Array like object
99
RestangularCollection.prototype = new Array();
1010

1111
mixin(RestangularCollection, RestangularBase, { chain: false });
12+
13+
14+
RestangularCollection.prototype.post = function (elem, params, headers) {
15+
return _.bind(elemFunction, this)('post', null, params, elem, headers);
16+
};

src/models/element.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11

2-
function RestangularElement(config, urlHandler, restangularizeElem) {
3-
this.$config = config;
4-
this.$urlHandler = urlHandler;
5-
this.$restangularizeElem = restangularizeElem;
2+
function RestangularElement(service) {
3+
this.$service = service;
4+
this.$config = service.config;
65
// route y reqParams restangularized fromServer
76
}
87

9-
mixin(RestangularElement, RestangularBase, { chain: false });
8+
mixin(RestangularElement, RestangularBase, { chain: false });
9+
10+
11+
12+
RestangularElement.prototype.put = function (params, headers) {
13+
return _.bind(elemFunction, this)('put', undefined, params, undefined, headers);
14+
};
15+
16+
RestangularElement.prototype.save = function (params, headers) {
17+
if (this[this.$config.restangularFields.fromServer]) {
18+
return this.put(params, headers);
19+
} else {
20+
return _.bind(elemFunction, this)('post', undefined, params, undefined, headers);
21+
}
22+
};
23+
24+
RestangularElement.prototype.get = function (params, headers) {
25+
return _.bind(elemFunction, this)('get', undefined, params, undefined, headers);
26+
};
27+
28+
RestangularElement.prototype.post = function (what, elem, params, headers) {
29+
return _.bind(elemFunction, this)('post', what, params, elem, headers);
30+
};

0 commit comments

Comments
 (0)