-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-v0.4.1.js
676 lines (559 loc) · 26.4 KB
/
action-v0.4.1.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/****************************************
Action! v0.4.1 Akshay Kumar
https://github.com/designfrontier/Action
****************************************/
//TODO routing and pushstate
// view rendering on routing events
var action = function(){
'use strict';
var action = {
eventMe: function(objectIn){
var returnObject = objectIn
, localEvents = {};
//set an emitter id for troubleshooting
returnObject.emitterId = Math.ceil(Math.random() * 10000);
//create the lcoal event Store
returnObject.eventStore = {};
returnObject.emit = function(eventNameIn, eventDataIn, localFlag){
var that = this
, eventStack
, functionToCall
, i
, isLocal = (typeof localFlag !== 'undefined' && localFlag);
if(isLocal){
eventStack = that.eventStore[eventNameIn];
} else {
eventStack = action.eventStore[eventNameIn];
}
//emit the event
if(typeof eventStack !== 'undefined'){
for(i = 0; i < eventStack.length; i ++){
if(typeof eventStack[i].scope !== 'undefined'){
eventStack[i].call.apply(eventStack[i].scope,[eventDataIn, that.emitterId]);
}else{
eventStack[i].call(eventDataIn, that.emitterId);
}
if(eventStack[i].once){
that.silence(eventNameIn, eventStack[i].call, true, isLocal);
}
}
}
};
returnObject.emitLocal = function(eventNameIn, eventDataIn){
var that = this;
that.emit(eventNameIn, eventDataIn, true);
};
returnObject.listen = function(eventNameIn, handlerIn, scopeIn, onceIn, localFlagIn){
var that = this
, i
, newCheck = true
//attribute holders and such
, eventName = eventNameIn
, handler = handlerIn
, scope = scopeIn
, local = localFlagIn
, once = onceIn
//variables for later
, eventStack
, newEvent;
if(typeof eventNameIn === 'object'){
//we have an object to split up dude
eventName = eventNameIn.eventName;
handler = eventNameIn.handler;
scope = eventNameIn.scope;
once = eventNameIn.once;
local = eventNameIn.local;
}
eventStack = (typeof local !== 'undefined' && local) ? that.eventStore[eventNameIn] : action.eventStore[eventNameIn];
newEvent = (typeof local !== 'undefined' && local) ? that : action;
if(typeof eventStack !== 'undefined'){
//already exists check to see if the function is already bound
for(i = 0; i < eventStack.length; i ++){
if(eventStack[i].call === handler && eventStack[i].once === false){
newCheck = false;
break;
}
}
if(newCheck && typeof scopeIn !== 'undefined'){
eventStack.push({once: false, call: handler, scope: scope});
}else if(newCheck){
eventStack.push({once: false, call:handler});
}
} else {
//new event
newEvent.eventStore[eventName] = []; //use an array to store functions
if(typeof scopeIn !== 'undefined'){
newEvent.eventStore[eventName].push({once: false, call: handler, scope: scope});
}else{
newEvent.eventStore[eventName].push({once: false, call: handler});
}
}
};
returnObject.listenLocal = function(eventNameIn, handlerIn, scopeIn, onceIn){
var that = this;
//convenience function for local listens
if(typeof eventNameIn === 'object'){
eventNameIn.local = true;
that.listen(eventNameIn);
} else {
that.listen({
eventName: eventNameIn
, handler: handlerIn
, scope: scopeIn
, once: onceIn
, local: true
});
}
};
returnObject.listenOnce = function(eventNameIn, handlerIn, scopeIn, localFlagIn){
//same thing as .listen() but is only triggered once
var that = this
, i
, newCheck = true
, eventStack
, newEvent
, eventName = eventNameIn
, handler = handlerIn
, scope = scopeIn
, localFlag = localFlagIn;
if(typeof eventNameIn === 'object'){
eventName = eventNameIn.eventName;
handler = eventNameIn.handler;
scope = eventNameIn.scope;
localFlag = eventNameIn.local;
}
if(typeof localFlag !== 'undefined' && localFlag){
//make it local!
eventStack = that.eventStore[eventName];
newEvent = that;
}else{
eventStack = action.eventStore[eventName];
newEvent = action;
}
if(typeof eventStack !== 'undefined'){
//already exists check to see if the function is already bound
for(i = 0; i < eventStack.length; i ++){
if(eventStack[i].call === handler && eventStack[i].once === true){
newCheck = false;
break;
}
}
if(newCheck){
eventStack.push({once:true, call: handler, scope: scope});
}
} else{
//new event
newEvent.eventStore[eventNameIn] = []; //use an array to store functions
newEvent.eventStore[eventNameIn].push({once:true, call: handler, scope: scope});
}
};
returnObject.listenOnceLocal = function(eventNameIn, handlerIn, scopeIn){
var that = this;
//same thing as .listen() but is only triggered once
if(typeof eventNameIn === 'object'){
eventNameIn.local = true;
that.listenLocal(eventNameIn);
}else{
that.listenLocal({
eventName: eventNameIn
, handler: handlerIn
, scope: scopeIn
, local: true
});
}
};
returnObject.silence = function(eventNameIn, handlerIn, onceIn, scopeIn, localFlagIn){
//localize variables
var that = this
, i
, truthy = false
, eventName = eventNameIn
, handler = handlerIn
, once = onceIn
, scope = scopeIn
, localFlag = localFlagIn
, store;
if(typeof eventNameIn === 'object'){
// passed in a collection of params instead of params
eventName = eventNameIn.eventName;
handler = eventNameIn.handler;
once = eventNameIn.once;
scope = eventNameIn.scope;
localFlag = eventNameIn.local;
}
//local or global event store?
store = (typeof localFlag === 'undefined' || !localFlag) ? action : that;
if(typeof store.eventStore[eventName] === 'undefined'){
//if there is no event with a name... return nothing
return;
}
//there is an event that matches... proceed
for(i = 0; i < store.eventStore[eventName].length; i ++){
//reset this variable
truthy = false;
if(typeof handler !== 'undefined'){
//function is passed in
if(typeof scope !== 'undefined'){
//scope is passed in...
if(typeof once === 'boolean'){
// function + scope + once provides the match
truthy = (handler === store.eventStore[eventName][i].call && scope === store.eventStore[eventName][i].scope && once === store.eventStore[eventName][i].once);
} else {
//function + scope provides the match
truthy = (handler === store.eventStore[eventName][i].call && scope === store.eventStore[eventName][i].scope);
}
} else {
//function + once in for the match
if(typeof once === 'boolean'){
truthy = (handler === store.eventStore[eventName][i].call && store.eventStore[eventName][i].once === once);
} else {
truthy = (handler === store.eventStore[eventName][i].call);
}
}
} else {
//no function unbind everything by resetting
store.eventStore[eventName] = [];
//and exit
break;
}
if(truthy){
//remove this bad boy
store.eventStore[eventName].splice(i,1);
}
}
};
returnObject.silenceLocal = function(eventNameIn, handlerIn, onceIn, scopeIn){
var that = this;
//essentially a convenience function.
if(typeof eventNameIn === 'object'){
eventNameIn.local = true;
that.silence(eventNameIn);
}else{
that.silence({
eventName: eventNameIn
, handler: handlerIn
, once: onceIn
, scope: scopeIn
, local: true
});
}
};
//Event Based state machine
returnObject.requiredEvent = function(name, callback, context, fireMultipleIn){
var that = this
, stateUpdate;
that._fireMultiple = (typeof fireMultipleIn !== 'undefined') ? fireMultipleIn : false;
//init some hidden storage if needed
if(typeof that.stateEvents === 'undefined'){
that.stateEvents = {};
}
if(typeof that._triggeredStateReady === 'undefined'){
that._triggeredStateReady = false;
}
that.stateEvents[name] = false;
stateUpdate = that.stateUpdate(name, that.stateEvents);
that.listen(name, callback, context);
that.listen(name, stateUpdate, that);
};
returnObject.stateUpdate = function(nameIn, stateEventsIn){
var name = nameIn
, stateEvents = stateEventsIn
, that = this;
return function(){
var truthy = true
, key;
if(typeof stateEvents[name] !== 'undefined'){
stateEvents[name] = true;
for(key in stateEvents){
truthy = truthy && stateEvents[key];
}
if(truthy){
if(!that._triggeredStateReady || that._fireMultiple){
//feels like a little bit of a hack.
// lets the data finish propogating before triggering the call
setTimeout(that.stateReady.apply(that), 100);
that._triggeredStateReady = true;
}
}
}
};
};
returnObject.stateReady = function(){
//this is a default action when all required events have been completed.
// needs to be overridden if you want to do something real
console.log('ready!');
};
returnObject.listen('system:trace', function(emitterIDIn){
var that = this;
if(that.emitterId === emitterIDIn){
// that.emit('system:addTraced', that);
action.traced = that;
}
}, returnObject);
//execute the init function if it exists
if(typeof returnObject.init === 'function'){
returnObject.init.apply(returnObject);
}
return returnObject;
}
, modelMe: function(objectIn){
//this is the module for creating a data model object
var that = this
, newModel = that.eventMe({})
, attributes = {}
, changes = [];
newModel.super = {};
newModel.get = function(attributeName){
return attributes[attributeName];
};
newModel.set = function(attributeName, attributeValue){
var that = this
, key;
if(typeof attributeName === 'object'){
//well... this is an object... iterate and rock on
for(key in attributeName){
if(attributeName.hasOwnProperty(key)){
//this attribute does not belong to the prototype. Good.
//TODO: maybe make this do a deep copy to prevent
// pass by reference or switch to clone()
if(key !== 'destroy' && key !== 'fetch' && key !== 'save' && typeof attributeName[key] !== 'function'){
if(typeof attributeValue === 'object'){
attributes[attributeName] = (Array.isArray(attributeName[key])) ? [] : {};
action.clone(attributes[attributeName], attributeName[key]);
}else{
attributes[key] = attributeName[key];
}
that.emitLocal('attribute:changed', key);
} else {
if(typeof that[key] === 'function' && !that.super[key]){
//wrap the super version in a closure so that we can
// still execute it correctly
that.super[key] = that[key].bind(that);
}
that[key] = attributeName[key];
}
}
}
} else{
if(attributeName !== 'destroy' && attributeName !== 'fetch' && attributeName !== 'save'){
if(typeof attributeValue === 'object'){
attributes[attributeName] = (Array.isArray(attributeValue)) ? [] : {};
action.clone(attributes[attributeName], attributeValue);
}else{
attributes[attributeName] = attributeValue;
}
that.emitLocal('attribute:changed', attributeName);
} else {
if(typeof that[attributeName] === 'function'){
//wrap the super version in a closure so that we can
// still execute it correctly
that.super[attributeName] = that[attributeName].bind(that);
}
that[attributeName] = attributeValue;
}
}
}
newModel.flatten = function(){
return attributes;
}
newModel.fetch = function(setVariableName, successFunction, errorFunction, flushCache){
var that = this
, requestUrl = that.get('url')
, useLocal = action.useLocalCache && !flushCache;
if(typeof requestUrl !== 'undefined'){
//make the request for the model
if(useLocal){
window.localforage.getItem(window.btoa(that.get('url')), function(data){
if(data === null){
//this doesn't exist locally...
that.ajaxGet(setVariableName, function(dataIn){
var localData = dataIn
, articleId = that.get('url');
window.localforage.setItem(window.btoa(articleId), localData, function(){
// console.log('data done');
});
});
}else{
//it does exist!
that.emit(that.get('dataEvent'), data);
}
});
} else {
that.ajaxGet(setVariableName, successFunction);
}
} else {
that.emit('global:error', new action.Error('http', 'No URL defined', that));
if(typeof errorFunction === 'function'){
errorFunction.apply(that);
}
}
};
newModel.ajaxGet = function(setVariableName, successFunction){
var that = this
, requestUrl = that.get('url')// + '?' + Date.now()
, oReq = new XMLHttpRequest();
oReq.onload = function(){
var data = JSON.parse(this.responseText);
//TODO: make the statuses more generic
if(this.status === 200 || this.status === 302){
that.emit(that.get('dataEvent'), data);
if(typeof setVariableName === 'string'){
that.set(setVariableName, data);
}else{
that.set(data);
}
if(typeof successFunction === 'function'){
successFunction.apply(that, [data]);
}
}else if(this.status === 400){
}else if(this.status === 500){
that.emit('global:error', new action.Error('http', 'Error in request', that));
}
};
oReq.onerror = function(xhr, errorType, error){
that.emit('global:error', new action.Error('http', 'Error in request type: ' + errorType, that, error));
};
oReq.open('get', requestUrl, true);
oReq.send();
};
newModel.save = function(){
//TODO make this talk to a server with the URL
//TODO make it only mark the saved changes clear
var that = this
, requestUrl = that.get('url')
, id = that.get('id')
, type = (typeof id === 'undefined') ? 'post' : 'put'
, oReq = new XMLHttpRequest();
if(typeof requestUrl !== 'undefined'){
oReq.onload = function(){
if(this.status === 200 || this.status === 302){
that.clearChanges();
that.set(data);
that.emit(that.get('dataEvent'), data);
}else if(this.status === 500 || this.status === 400){
that.emit('global:error', new action.Error('http', 'Error in request', that));
}
};
oReq.submittedData = that.flatten();
oReq.open(type, requestUrl, true);
oReq.send();
// $.ajax({
// type: type
// , url: requestUrl + '/' + id
// , data: that.flatten()
// , success: function(data, status){
// //only do this on success...
// that.clearChanges();
// //update the model with stuff from the server
// that.set(data);
// //emit the data event for this model to refresh everyone's values
// that.emit(that.get('dataEvent'), data);
// }
// , error: function(){
// that.emit('global:error', new action.Error('http', 'Error in request', that));
// }
// });
} else {
action.emit('global:error', new action.Error('http', 'No URL defined', that));
}
}
newModel.clearChanges = function(){
changes = [];
}
newModel.getChanges = function(){
return changes;
}
newModel.clear = function(){
attributes = {};
}
newModel.destroy = function(){
//TODO not really working... should get rid of this thing
// and all of its parameters
var that = this
, key;
setTimeout(function(){
// delete me;
},0); // not quite working...
for(key in that){
// delete this[key];
}
//TODO this still doesn't kill the attributes or changes
// private data
}
newModel.set(objectIn); //set the inital attributes
newModel.listenLocal('attribute:changed', function(nameIn){
changes.push(nameIn);
}, this); //maybe eliminate this 'this'
if(typeof newModel.init === 'function'){
newModel.init.apply(newModel);
}
return newModel;
}
//TODO: figure out if this is needed since the global:error...
// , trace: function(emitterIdIn){
// //log out the function that has the emitterId attached
// //create the traced object/stack
// action.traced = action.modelMe({
// stack: []
// , emitterId: emitterIdIn
// });
// action.traced.listen('system:addTraced', function(objectIn){
// var that = this;
// that.get('stack').push(objectIn);
// }, action.traced);
// //trigger the event that will cause the trace
// action.emit('system:trace', emitterIdIn);
// }
, Error: function(typeIn, messageIn, objectIn, errorObjectIn){
return {
type: typeIn
, message: messageIn
, createdBy: objectIn
, errorObject: errorObjectIn
}
}
, clone: function(objectIn, cloneMe){
var key;
for(key in cloneMe){
if(cloneMe.hasOwnProperty(key)){
//good to copy this one...
if (typeof cloneMe[key] === 'object'){
//set up the object for iteration later
objectIn[key] = (Array.isArray(cloneMe[key])) ? [] : {};
action.clone(objectIn[key], cloneMe[key]);
}else{
objectIn[key] = cloneMe[key];
}
}
}
}
, eventStore: {}
};
//add an events hook for global dealing with events...
action = action.eventMe(action);
action.listen('global:error', function(errorIn) {
console.group('An Error occured in an object with emitterid: ' + errorIn.createdBy.emitterId);
console.log('It was a ' + errorIn.type + 'error.');
if(typeof errorIn.errorObject === 'string'){
console.log('It says: ' + errorIn.errorObject);
console.log('and: ' + errorIn.message);
} else {
console.log('It says: ' + errorIn.message);
}
console.log('The Whole Enchilada (object that caused this mess):');
console.dir(errorIn.createdBy);
if(typeof errorIn.createdBy.flatten === 'function'){
console.log('Just the Lettuce (attributes):');
console.dir(errorIn.createdBy.flatten());
}
if(typeof errorIn.errorObject === 'object'){
console.log('Oh look an Error Object!:');
console.dir(errorIn.errorObject);
}
console.groupEnd();
// action.trace(errorIn.createdBy.emitterId);
// throw errorIn;
});
//return the tweaked function
return action;
}(this);