forked from platanus/angular-restmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.js
More file actions
174 lines (161 loc) · 5.41 KB
/
Copy pathmodule.js
File metadata and controls
174 lines (161 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
// Preload some angular stuff
var RMModule = angular.module('plRestmod', ['ng', 'platanus.inflector']);
/**
* @class $restmodProvider
*
* @description
*
* The $restmodProvider exposes $restmod configuration methods
*/
RMModule.provider('$restmod', [function() {
var BASE_CHAIN = []; // The base mixin chain
return {
/**
* @memberof $restmodProvider#
*
* @description
*
* Adds base mixins for every generated model.
*
* **ATTENTION** Model names should NOT be added to this chain.
*
* Base model chain is by default empty, all mixins added to the chain are
* prepended to every generated model.
*
* Usage:
*
* ```javascript
* $provider.pushModelBase('ChangeModel', 'LazyRelations', 'ThrottledModel')
* ```
*/
pushModelBase: function(/* _mix_names */) {
Array.prototype.push.apply(BASE_CHAIN, arguments);
return this;
},
/**
* @class $restmod
*
* @description
*
* The restmod service provides factory methods for the different restmod consumables.
*/
$get: ['RMModelFactory', 'RMBuilder', function(factory, Builder) {
var arraySlice = Array.prototype.slice;
var restmod = {
/**
* @memberOf $restmod#
*
* @description
*
* The model factory is used to generate new $restmod model types. It's recommended to put models inside factories,
* this is usefull later when defining relations and inheritance, since the angular $injector is used by
* these features. It's also the angular way of doing things.
*
* A simple model can be built like this:
*
* ```javascript
* angular.module('bike-app').factory('Bike', function($restmod) {
* return $restmod.model('/bikes');
* });
*```
*
* The `_url` parameter is the resource url the generated model will be bound to, if `null` is given then
* the model is *anonymous* and can only be used in another model context.
*
* The model also accepts one or more definition providers as one or more arguments after the _url parameter,
* posible definition providers are:
*
* * A definition object (more on this at the {@link BuilderApi}):
*
* ```javascript
* $restmod.model('/bikes', {
* viewed: { init: false },
* parts: { hasMany: 'Part' },
* '~afterCreate': function() {
* alert('Bike created!!');
* }
* });
*```
*
* * A definition function (more on this at the {@link BuilderApi}):
*
* ```javascript
* $restmod.model('/bikes', function() {
* this.attrDefault('viewed', false);
* this.attrMask('id', 'CU');
* });
*```
*
* * A mixin (generated using the mixin method) or model factory name:
*
* ```javascript
* $restmod.model('/bikes', 'BaseModel', 'PagedModel');
*```
*
* * A mixin (generated using the mixin method) or model object:
*
* ```javascript
* $restmod.model('/bikes', BaseModel, PagedModel);
* ```
*
* @param {string} _url Resource url.
* @param {mixed} _mix One or more mixins, description objects or description blocks.
* @return {StaticApi} The new model.
*/
model: function(_baseUrl/* , _mix */) {
// Generate a new model type.
var Model = factory(_baseUrl);
// Load builder and execute it.
var builder = new Builder(Model);
builder.loadMixinChain(BASE_CHAIN);
builder.loadMixinChain(Model.$chain = arraySlice.call(arguments, 1));
return Model;
},
/**
* @memberOf $restmod#
*
* @description
*
* The mixin factory is used to pack model behaviors without the overload of generating a new
* model. The mixin can then be passed as argument to a call to {@link $restmod#model#model}
* to extend the model capabilities.
*
* A mixin can also be passed to the {@link $restmodProvider#pushModelBase} method to provide
* a base behavior for all generated models.
*
* @param {mixed} _mix One or more mixins, description objects or description blocks.
* @return {object} The mixin
*/
mixin: function(/* _mix */) {
return { $isAbstract: true, $chain: arraySlice.call(arguments, 0) };
},
/**
* @memberOf $restmod#
*
* @description
*
* Shorcut method used to create singleton resources.
*
* Same as calling `$restmod.model(null).$single(_url)`
*
* Check the {@link StaticApi#$single} documentation for more information.
*
* @param {string} _url Resource url,
* @param {mixed} _mix Mixin chain.
* @return {RecordApi} New resource instance.
*/
singleton: function(_url/*, _mix */) {
return restmod.model.apply(this, arguments).$single(_url);
}
};
return restmod;
}]
};
}])
.factory('model', ['$restmod', function($restmod) {
return $restmod.model;
}])
.factory('mixin', ['$restmod', function($restmod) {
return $restmod.mixin;
}]);