app utils wrapFunction function fn after before condition if isFunctio

 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
app.utils.wrapFunction = function(fn, after, before, condition) {
if (!_.isFunction(after) && _.isObject(after)) {
before = after.before;
condition = after.condition;
after = after.after;
}
var oldfn = fn;
return function() {
var context = this,
result,
beforeresult;
if (_.isFunction(condition) && (condition.apply(context, arguments) === false)) {
return false;
}
if (_.isFunction(before)) {
beforeresult = before.apply(context, arguments);
}
if (beforeresult !== undefined && beforeresult.then) {
var args = Array.prototype.slice.call(arguments);
beforeresult.then(function(resolved) {
args.push(resolved);
result = oldfn.apply(context, args);
_.isFunction(after) && after.apply(context, args);
});
} else {
result = oldfn.apply(context, arguments);
_.isFunction(after) && after.apply(context, arguments);
}
return result;
}
}