-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloy.js
106 lines (80 loc) · 2.25 KB
/
alloy.js
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
// The contents of this file will be executed before any of
// your view controllers are ever executed, including the index.
// You have access to all functionality on the `Alloy` namespace.
//
// This is a great place to do any initialization for your app
// or create any global variables/functions that you'd like to
// make available throughout your app. You can easily make things
// accessible globally by attaching them to the `Alloy.Globals`
// object. For example:
//
// Alloy.Globals.someGlobalFunction = function(){};
Alloy.Globals.fa = require('fa');
if (OS_ANDROID) {
getTime = (Date.now ||
function() {
return new Date().getTime();
});
// Monkey patch throttle and debounce to fix bug in throttle function on
// Titanium (3.2.0.GA) Android.
_.throttle = function(func, wait, options) {
var context, args, result, timeout, previous, later;
timeout = null;
previous = 0;
options = options || {};
later = function() {
previous = options.leading === false ? 0 : getTime();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return function() {
var now, remaining;
now = getTime();
if (!previous && options.leading === false) {
previous = now;
}
remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result, later = function() {
var last = getTime() - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
context = args = null;
}
}
};
return function() {
var callNow = immediate && !timeout;
context = this;
args = arguments;
timestamp = getTime();
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
}