annotate src/implab/guard.js @ 0:fc2517695ee1

Initial commit, draft import of existing work
author cin
date Thu, 01 Jun 2017 13:20:03 +0300
parents
children 00779cb63b12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([ "dojo/Deferred" ], function(Deferred) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 var toPromise = function(d) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 if (d && d.then)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 return d;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 var d2 = new Deferred();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 d2.resolve(d);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 return d2;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 * функция для асинхронного выполнения метода
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 * @param{Object} o Объект, который будет использован в качестве
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 * <code>this</code>, если не указан, будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 * <code>null</code>
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 * @param{Function|String} m Функция или имя метода, обязательный параметр.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 * Если указано имя, тогда параметр <code>o</code>
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 * также должен быть задан
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 * @param{Array} args Параметры для вызова метода, не обязательно.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 * @returns{dojo/promise}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 return function(o, m, args) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 if (arguments.length == 1) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 m = o;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 o = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 } else if (arguments.length == 2 && o instanceof Function &&
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 m instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 args = m;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 m = o;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 o = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 if (!(m instanceof Function)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 if (o)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 m = o[m];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 else if (arguments.length == 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 return toPromise(m);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 throw new Error("The target object must be specified");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 if (!m)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 throw new Error("Method not found");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 return toPromise(m.apply(o, args));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 } catch (err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 var d = new Deferred();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 d.reject(err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 return d;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 });