1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>JSDoc: Source: lib/model/ServiceObject.js</title>
|
6 |
|
7 | <script src="scripts/prettify/prettify.js"> </script>
|
8 | <script src="scripts/prettify/lang-css.js"> </script>
|
9 | |
10 |
|
11 |
|
12 | <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
13 | <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
14 | </head>
|
15 |
|
16 | <body>
|
17 |
|
18 | <div id="main">
|
19 |
|
20 | <h1 class="page-title">Source: lib/model/ServiceObject.js</h1>
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | <section>
|
28 | <article>
|
29 | <pre class="prettyprint source linenums"><code>var Promise = require('bluebird');
|
30 | var _ = require('lodash');
|
31 | var d = require('debug')("raptorjs:ServiceObject");
|
32 |
|
33 | var util = require('../util');
|
34 |
|
35 | var Action = require('./Action');
|
36 | var Stream = require('./Stream');
|
37 |
|
38 | /**
|
39 | *
|
40 | * The Service Object
|
41 | *
|
42 | * @param {Object} obj A JSON object with the object definition
|
43 | * @param {Raptor} container reference to the Raptor sdk wrapper
|
44 | *
|
45 | * @augments Container
|
46 | * @constructor
|
47 | */
|
48 | var ServiceObject = function (obj, container) {
|
49 |
|
50 | var me = this;
|
51 |
|
52 | this.data = {};
|
53 |
|
54 | this.data.id = null;
|
55 | this.data.parentId = null;
|
56 | this.data.name = null;
|
57 | // this.data.createdAt = new Date();
|
58 |
|
59 | this.data.settings = {
|
60 | storeEnabled: true,
|
61 | eventsEnabled: true
|
62 | };
|
63 |
|
64 | this.data.customFields = {};
|
65 |
|
66 | this.data.streams = {};
|
67 | this.data.actions = [];
|
68 |
|
69 | this.exportProperties();
|
70 |
|
71 | if(container) {
|
72 | this.setContainer(container);
|
73 | }
|
74 |
|
75 | if(obj) {
|
76 | this.parseJSON(obj);
|
77 | }
|
78 |
|
79 | this.permissions = (function (p) {
|
80 | return {
|
81 | get: function (user) {
|
82 | return p.get(me, user);
|
83 | },
|
84 | set: function (user, permissions) {
|
85 | return p.set(me, permissions, user);
|
86 | }
|
87 | };
|
88 | })(container.auth.permissions);
|
89 |
|
90 |
|
91 | };
|
92 | util.extends(ServiceObject, "Container");
|
93 |
|
94 | /**
|
95 | * @inheritdoc
|
96 | */
|
97 | ServiceObject.prototype.setContainer = function (c) {
|
98 | this.container = c;
|
99 | this.client = c.client;
|
100 | };
|
101 |
|
102 | /**
|
103 | * @return {String} id The object id
|
104 | */
|
105 | ServiceObject.prototype.getId = function () {
|
106 | return this.data.id || null;
|
107 | };
|
108 |
|
109 | /**
|
110 | * @return {Number} createdAt The creation date as unix timestamp
|
111 | */
|
112 | ServiceObject.prototype.getCreatedAt = function () {
|
113 | return this.data.createdAt || null;
|
114 | };
|
115 |
|
116 | /**
|
117 | * Return a stream reference
|
118 | *
|
119 | * @param {String} name stream name
|
120 | * @return {Stream} stream requested stream, if available
|
121 | */
|
122 | ServiceObject.prototype.stream = function (name) {
|
123 | return this.data.streams[name];
|
124 | };
|
125 |
|
126 | /**
|
127 | * Return an action reference
|
128 | *
|
129 | * @param {String} name action name
|
130 | * @return {Action} action requested action, if available
|
131 | */
|
132 | ServiceObject.prototype.action = function (name) {
|
133 | return this.data.actions[name];
|
134 | };
|
135 |
|
136 | /**
|
137 | * @inheritdoc
|
138 | */
|
139 | ServiceObject.prototype.toJSON = function () {
|
140 | var json = this.__super__.toJSON.call(this);
|
141 | var actions = [];
|
142 | _.forEach(json.actions, function (action) {
|
143 | actions.push(action);
|
144 | });
|
145 | json.actions = actions;
|
146 | return json;
|
147 | };
|
148 |
|
149 | /**
|
150 | * @inheritdoc
|
151 | */
|
152 | ServiceObject.prototype.parseJSON = function (data) {
|
153 |
|
154 | this.__super__.parseJSON.call(this, data);
|
155 |
|
156 | if(data.actions !== undefined)
|
157 | this.setActions(data.actions);
|
158 |
|
159 | if(data.streams !== undefined)
|
160 | this.setStreams(data.streams);
|
161 |
|
162 | this.validate();
|
163 | };
|
164 |
|
165 | /**
|
166 | * @inheritdoc
|
167 | */
|
168 | ServiceObject.prototype.validate = function () {
|
169 |
|
170 | if(!this.name)
|
171 | throw new Error("Object name is required");
|
172 |
|
173 | _.forEach(this.streams, function (stream) {
|
174 | stream.validate();
|
175 | });
|
176 |
|
177 | _.forEach(this.actions, function (action) {
|
178 | action.validate();
|
179 | });
|
180 |
|
181 | };
|
182 |
|
183 | /**
|
184 | * Set actions to the object
|
185 | *
|
186 | * @param {Array} actions The list of actions to set
|
187 | */
|
188 | ServiceObject.prototype.setActions = function (actions) {
|
189 | var me = this;
|
190 | if(!actions) return;
|
191 |
|
192 | this.data.actions = {};
|
193 |
|
194 | _.forEach(actions, function (raw, name) {
|
195 |
|
196 | if(typeof raw === 'string') {
|
197 | raw = {
|
198 | name: raw
|
199 | };
|
200 | }
|
201 |
|
202 | if(!raw.name && typeof name === 'string')
|
203 | raw.name = name;
|
204 |
|
205 | var action = new Action(raw, me);
|
206 | me.data.actions[action.data.name] = action;
|
207 | });
|
208 |
|
209 | };
|
210 |
|
211 | /**
|
212 | * Add a stream to the object
|
213 | *
|
214 | * @param {String} name The stream name
|
215 | * @param {Object} definition The steam definition
|
216 | */
|
217 | ServiceObject.prototype.addStream = function (name, def) {
|
218 |
|
219 | def = def || {};
|
220 |
|
221 | if(!def.channels) {
|
222 | def = {
|
223 | channels: def
|
224 | };
|
225 | }
|
226 | def.name = name;
|
227 |
|
228 | var stream = new Stream(def, this);
|
229 |
|
230 | this.streams[stream.name] = stream;
|
231 | };
|
232 |
|
233 | /**
|
234 | * Set streams to the object
|
235 | *
|
236 | * @param {Array} streams The list of streams to set
|
237 | */
|
238 | ServiceObject.prototype.setStreams = function (streams) {
|
239 | var me = this;
|
240 | if(!streams) return;
|
241 |
|
242 | this.data.streams = {};
|
243 |
|
244 | _.forEach(streams, function (raw, name) {
|
245 | me.addStream(name, raw);
|
246 | });
|
247 | };
|
248 |
|
249 | /**
|
250 | * Create a new Object instance
|
251 | *
|
252 | * @return {Promise} object the newly created reference
|
253 | */
|
254 | ServiceObject.prototype.create = function () {
|
255 | return this.client.post('/', this.toJSON())
|
256 | .bind(this)
|
257 | .then(function (res) {
|
258 | d("Created new object " + res.id);
|
259 | this.id = res.id;
|
260 | this.createdAt = res.createdAt;
|
261 | return Promise.resolve(this);
|
262 | });
|
263 | };
|
264 |
|
265 | /**
|
266 | * Load the Object definition
|
267 | *
|
268 | * @param {String} id The object Id
|
269 | *
|
270 | * @return {Promise} Promise of the request with the ServiceObject as argument
|
271 | */
|
272 | ServiceObject.prototype.load = function (id) {
|
273 | return this.client.get('/' + (id || this.getId()))
|
274 | .bind(this)
|
275 | .then(function (obj) {
|
276 | this.parseJSON(obj);
|
277 | return Promise.resolve(this);
|
278 | });
|
279 | };
|
280 |
|
281 | /**
|
282 | * Update an Object
|
283 | *
|
284 | * @return {Promise} Promise of the request with the ServiceObject as argument
|
285 | */
|
286 | ServiceObject.prototype.update = function (data) {
|
287 | return this.client.put('/' + this.getId(), data || this.toJSON())
|
288 | .bind(this)
|
289 | .then(function (obj) {
|
290 | this.parseJSON(obj);
|
291 | return Promise.resolve(this);
|
292 | });
|
293 | };
|
294 |
|
295 | /**
|
296 | * Delete a Service Object
|
297 | *
|
298 | * @param {String} Optional, the objectId to delete
|
299 | * @return {Promise} Promise of the request with a new empty so as argument
|
300 | */
|
301 | ServiceObject.prototype.delete = function (objectId) {
|
302 | return this.client.delete('/' + (objectId || this.getId()))
|
303 | .bind(this)
|
304 | .then(function () {
|
305 | this.parseJSON({});
|
306 | return Promise.resolve(null);
|
307 | });
|
308 | };
|
309 |
|
310 | /**
|
311 | * Get children
|
312 | *
|
313 | * @return {Promise} Promise of the request with a the list of children
|
314 | */
|
315 | ServiceObject.prototype.getChildren = function () {
|
316 | return this.client.get('/' + this.getId() + '/tree')
|
317 | .bind(this)
|
318 | .then(function (list) {
|
319 | var me = this;
|
320 | return Promise.resolve(list.map(function (raw) {
|
321 | return me.getContainer().fromJSON(raw);
|
322 | }));
|
323 | });
|
324 | };
|
325 |
|
326 | /**
|
327 | * Set children
|
328 | *
|
329 | * @return {Promise} Promise of the request with a the list of children
|
330 | */
|
331 | ServiceObject.prototype.setChildren = function (list) {
|
332 | list = list instanceof Array ? list : [list]
|
333 | list = list.map(function (raw) {
|
334 | return raw.id || raw;
|
335 | })
|
336 | return this.client.post('/' + this.getId() + '/tree', list).bind(this)
|
337 | };
|
338 |
|
339 | /**
|
340 | * Add a child
|
341 | *
|
342 | * @param {ServiceObject} child The object instance or id to add
|
343 | * @return {Promise} Promise of the request with a the list of children
|
344 | */
|
345 | ServiceObject.prototype.addChild = function (obj) {
|
346 | return this.client.put('/' + this.getId() + '/tree/' + (obj.id || obj))
|
347 | .bind(this)
|
348 | .then(function (list) {
|
349 | if(obj.id) {
|
350 | list.forEach(function (o1) {
|
351 | if(obj.id === o1.id) {
|
352 | obj.parentId = o1.parentId
|
353 | obj.path = o1.path
|
354 | }
|
355 | })
|
356 | }
|
357 | return Promise.resolve(list)
|
358 | })
|
359 | .bind(this)
|
360 | };
|
361 |
|
362 | /**
|
363 | * Remove a child
|
364 | *
|
365 | * @param {ServiceObject} child The object instance or id to remove
|
366 | * @return {Promise} Promise of the request with a the list of children
|
367 | */
|
368 | ServiceObject.prototype.removeChild = function (obj) {
|
369 | return this.client.delete('/' + this.getId() + '/tree/' + (obj.id || obj))
|
370 | .then(function (list) {
|
371 | if(obj.id) {
|
372 | obj.parentId = null
|
373 | obj.path = null
|
374 | }
|
375 | return Promise.resolve(list)
|
376 | })
|
377 | .bind(this)
|
378 | };
|
379 |
|
380 | /**
|
381 | * Callback called on an event notification
|
382 | *
|
383 | * @callback ServiceObjectOnEvent
|
384 | * @param {Object} event event information
|
385 | */
|
386 |
|
387 | /**
|
388 | * Subscribe for Service Object updates
|
389 | *
|
390 | * @param {ServiceObjectOnEvent} cb The callback that handles an event notification
|
391 | * @return {EventEmitter} An emitter that will notify of updates
|
392 | */
|
393 | ServiceObject.prototype.subscribe = function (fn) {
|
394 | var topic = this.getId() + "/events";
|
395 | return this.client.subscribe(topic, fn)
|
396 | .bind(this)
|
397 | .then(function (emitter) {
|
398 | return Promise.resolve(emitter);
|
399 | });
|
400 | };
|
401 |
|
402 | /**
|
403 | * Unsubscribe from Service Object updates
|
404 | */
|
405 | ServiceObject.prototype.unsubscribe = function (fn) {
|
406 | var topic = this.getId() + "/events";
|
407 | return this.client.unsubscribe(topic, fn)
|
408 | .bind(this)
|
409 | .then(function (emitter) {
|
410 | return Promise.resolve(emitter);
|
411 | });
|
412 | };
|
413 |
|
414 | module.exports = ServiceObject;
|
415 | </code></pre>
|
416 | </article>
|
417 | </section>
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | </div>
|
423 |
|
424 | <nav>
|
425 | <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Action.html">Action</a></li><li><a href="Channel.html">Channel</a></li><li><a href="Client.html">Client</a></li><li><a href="Raptor.html">Raptor</a></li><li><a href="RecordSet.html">RecordSet</a></li><li><a href="ResultSet.html">ResultSet</a></li><li><a href="ServiceObject.html">ServiceObject</a></li><li><a href="Stream.html">Stream</a></li><li><a href="User.html">User</a></li></ul><h3>Global</h3><ul><li><a href="global.html#Container">Container</a></li></ul>
|
426 | </nav>
|
427 |
|
428 | <br class="clear">
|
429 |
|
430 | <footer>
|
431 | Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Wed Nov 02 2016 11:15:06 GMT+0100 (CET)
|
432 | </footer>
|
433 |
|
434 | <script> prettyPrint(); </script>
|
435 | <script src="scripts/linenumber.js"> </script>
|
436 | </body>
|
437 | </html>
|