forked from hay/stapes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1354 lines (1027 loc) · 49.8 KB
/
Copy pathindex.html
File metadata and controls
1354 lines (1027 loc) · 49.8 KB
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Stapes.js - a (really) tiny Javascript MVC microframework</title>
<link rel="stylesheet" href="web/style.css" />
<link rel="stylesheet" href="web/code.css" />
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-15405780-13']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<nav>
<h1>Stapes.js</h1>
<ul class="toc"></ul>
</nav>
<section id="content">
<h1>Stapes.js</h1>
<h2>A (really) tiny Javascript MVC microframework.</h2>
<h3 id="m-downloads">Downloads</h3>
<ul>
<li><a href="stapes.min.js">
Download production version 0.7.0 (minified)</a>
</li>
<li><a href="https://github.com/hay/stapes/archive/v0.7.0.zip">
Download development version 0.7.0 (full source and annotated examples)</a>
</li>
<li><a href="http://github.com/hay/stapes">Fork it and report bugs on Github</a>
</li>
</ul>
<p>For node.js <code>npm install stapes</code> will do.</p>
<h3 id="m-phil">Philosophy</h3>
<p><em>Right. There's a zillion other Javascript MVC frameworks, why
should i choose Stapes.js?</em></p>
<p>Here's why i think you should use it: it's really tiny (<strong>2.1kb</strong> minified and gzipped),
easy to fit in an existing codebase, and doesn't force you to
write your code in a specific style. Because of its small size it's <b>ideal
for mobile development</b>.
<p><em>So, Stapes.js isn't a traditional MVC framework?</em></p>
<p>No. Stapes doesn't have any pre-defined Models, Views or Controllers.
It also doesn't have Collections, Routers, or stuff you might find in other
libraries (remember, it's tiny :).</p>
<p><em>Right.. so how do i write my code?</em></p>
<p>Any way you want. Stapes has all the building blocks you need when
writing a MVC app.</p>
<p><em>Like what?</em></p>
<p>Here's a list:</p>
<ul>
<li>A powerful yet simple event system using <a href="#m-on"><code>on</code></a> and
<a href="#m-emit"><code>emit</code></a> methods. Usable on non-Stapes objects as well using
<a href="#m-mixinEvents">event mixins</a>.</li>
<li><a href="http://wiki.ecmascript.org/doku.php?id=harmony:classes">Ecmascript Harmony-like classes</a> using <a href="#m-subclass"><code>subclass</code></a>.</li>
<li>Event-triggering attributes using <a href="#m-get"><code>get</code></a> and <a href="#m-set"><code>set</code></a>.</li>
<li>Powerful <a href="#m-filter"><code>filter</code></a> and <a href="#m-remove"><code>remove</code></a> methods for attributes.</li>
<li>Usable in traditional Javascript, as an AMD module (for <a href="http://requirejs.org">Require.js</a>)
or on the server with <a href="http://www.nodejs.org">Node.js</a></li>
<li><a href="#m-plugins">A simple mechanism</a> for writing plugins and extensions to extend the functionality of Stapes.</li>
<li>Nice extensive documentation with lots of examples (you're reading that right now ;)</li>
</ul>
<p><em>But what about templating, persistence, routing, history management, etc?</em></p>
<p>That's the flexibility of Stapes: you're free to use any library you want.</p>
<p><em>Right, so this is your hobby project. Does this actually work in production?</em></p>
<p>Stapes was written for a complex HTML5 media player in a high-traffic website,
and it works nicely there, as well as in multiple other projects. Have you used Stapes in your
project? <a href="mailto:hay@bykr.org">Let me know</a>, and i'll add you to this site as a showcase.</p>
<p><em>Where do i report bugs, feature requests, patches?</em></p>
<p>Please report all of those things on <a href="https://github.com/hay/stapes">Github</a>.</p>
<p><em>Does this work in all browsers?</em></p>
<p>Stapes should work on all current (mobile) browsers, as well as on
older browsers like Internet Explorer 8. If you come accross any
browser-specific bugs, please <a href="http://github.com/hay/stapes">report them</a>
on Github.</p>
<p><em>Okay, seems promising. Where do i start?</em></p>
<p>Read on...</p>
<h3 id="m-intro">Introduction</h3>
<p class="info"><b>If you're new to writing MVC apps you probably want to read this
<a href="http://www.haykranen.nl/2012/07/19/learn-mvc-create-a-javascript-todo-list-using-stapes-js-in-less-than-100-lines-code/">
beginner's tutorial</a>, and write a todo app in less than 100 lines of code using Stapes</b>.</p>
<p>Write your Stapes modules like this</p>
<pre><code>
var Module = Stapes.subclass({
constructor : function(name) {
this.name = name;
}
sayName : function() {
console.log('My name is: ' + name);
}
});
</code></pre>
<p>Then, use it like this.</p>
<pre><code>
var module = new Module('Emmylou');
module.sayName(); // 'My name is Emmylou'
</code></pre>
<p>If you want to use private variables and functions, use Stapes like this</p>
<pre><code>
var Module = (function() {
var name;
function showPrivate() {
console.log("You shouldn't be seeing this", name);
}
var Module = Stapes.subclass({
constructor : function(aName) {
name = aName;
showPrivate();
}
});
return Module;
})();
var module = new Module('Emmylou');
</code></pre>
<h3 id="m-mixinevents-h3">Using event methods with any non-Stapes object or function</h3>
<p>It's possible to add the <a href="#m-events">event methods</a> to any
regular Javascript object or function. See <a href="#m-mixinEvents">
<code>Stapes.mixinEvents</code></a> for more information.</p>
<h3 id="m-require">Using it together with RequireJS</h3>
<p>To avoid lots of global variables you can use a dependency loader like
<a href="http://www.requirejs.org">Require.JS</a>. This also makes the
previous example easier to read.</p>
<p>A dependency loader also makes it a lot easier to modularize your code and
(with the Require.js optimizer) easily minify and concat your app.</p>
<pre><code>
// 'module.js'
require(["path/to/Stapes"], function(Stapes) {
var name;
function showPrivate() {
console.log("You shouldn't be seeing this: ", name);
}
var Module = Stapes.subclass({
constructor : function(aName) {
name = aName;
showPrivate();
}
});
return Module;
});
// somewhere else
require(['module'], function(Module) {
var module = new Module('Elvis'); // You shouldn't be seeing this: Elvis
});
</code></pre>
<h3 id="m-examples">Examples</h3>
<p>There are three examples available to get a taste on how to write a
Stapes.js application.</p>
<ul>
<li><a href="examples/todos">A todo list</a></li>
<li><a href="examples/todos-require">A todo list using Require.JS modules</a></li>
<li><a href="examples/bounce">A simple bouncing ball demo with HTML5 audio</a></li>
</ul>
<p>Code for these examples is available in the <a href="#m-downloads">development download</a>.</p>
<p>Note that the two todo examples are also available from
<a href="http://addyosmani.github.com/todomvc/labs/">TodoMVC</a>.</p>
<h3 id="m-general">API: Creation methods</h3>
<p>These methods aid in the creation and extension of classes, or
<em>modules</em>. These terms are used interchangeably in this
document.</p>
<article id="m-subclass">
<h1>subclass</h1>
<h2>Module.subclass( [object] )</h2>
<h2>Stapes.subclass( [object, classOnly] )</h2>
<p>Create a new Stapes class that you can instantiate later on with
<code>new</code>.</p>
<p class="warning">Note that until Stapes 0.6.0 the preferred way of creating new modules was by using <code>Stapes.create</code>. This has been deprecated by <code>subclass</code> since 0.7.0. <a href="http://www.haykranen.nl/2013/01/13/stapes-0-7-0-and-the-end-of-create/">Read this blogpost why</a>.</p>
<p>You can add a <code>constructor</code> (the function that is run when you
instantiate the class) property to the object. All other properties
will become prototype methods. To add static methods to your class use
<a href="#m-extend"><code>extend</code></a>. If you want to add
more properties to the prototype of the class later on, use
<a href="#m-proto"><code>proto</code></a>.</p>
<pre><code>
var Module = Stapes.subclass({
constructor : function(name) {
this.name = name;
},
getName : function() {
return this.name;
}
});
var module = new Module('emmylou');
module.getName(); // 'emmylou'
</code></pre>
<p>When calling <code>subclass</code> on a Stapes class you can extend
it into a new class that will inherit all prototype properties of the
parent class.</p>
<pre><code>
var Module = Stapes.subclass({
sayName : function() {
console.log('i say my name!');
}
});
var BetterModule = Module.subclass({
sayNameBetter : function() {
this.sayName(); // Inherits 'sayName' from 'Module'
console.log('i say my name better!');
}
});
var module = new BetterModule();
module.sayNameBetter(); // 'i say my name! i say my name better!'
</code></pre>
<p>Note that it's perfectly valid to call <code>subclass</code> without
any arguments at all. In that case you'll simply get a new class
with an empty constructor.</p>
<p>Because <code>subclass</code> set ups the prototype chain correctly
the <code>instanceof</code> parameter will work as expected.</p>
<pre><code>
module instanceof BetterModule; // true
module instanceof Module; // true
</code></pre>
<p>All modules automatically get a <code>parent</code> property that links
back to the prototype of the parent. You can use this to override a
method in a new class, but still call the method of the parent class, like
the <code>super</code> method that is available in lots of languages.
Unfortunately <code>super</code> is a reserved keyword in Javascript, so that's
not usable in Stapes.</p>
<pre><code>
var BetterModule = Module.subclass({
// Note that this method name is the same as the one in Module
sayName : function() {
BetterModule.parent.sayName.apply(this, arguments);
console.log('i say my name better');
}
});
var module = new BetterModule();
module.sayName(); // 'i say my name! i say my name better!'
</code></pre>
<p>The optional <code>classOnly</code> flag can be set to true to make
a class without any <a href="#m-events">event</a> or
<a href="#m-data">data</a> methods. You will get all of the
<a href="#m-general">creation</a> methods, so you can still subclass
and extend your class. Use this flag to use Stapes as a simple
class creation library.</p>
<pre><code>
var Class = Stapes.subclass({
constructor : function(name) {
this.name = name;
}
}, true /* <-- 'classOnly' flag */);
'subclass' in Class; // true
'get' in Class; // false
</code></pre>
</article>
<article id="m-extend">
<h1>extend</h1>
<h2>module.extend( object[, object...])</h2>
<h2>Stapes.extend( object )</h2>
<p>Extend your class instance properties by giving an
object. Keys with the same value will be overwritten. <code>this</code>
will be set to the objects context.</p>
<pre><code>
Module.extend({
"names" : ['claire', 'alice']
"sayName" : function(i) {
console.log( "Hello " + this.names[i] + "!" );
}
});
var module = new Module();
module.sayName(1); // 'alice'
</code></pre>
<p>Note that using <code>extend</code> is exactly the same as directly
assigning properties to the module:</p>
<pre><code>
Module.names = ['claire', 'alice'];
// Is the same as
Module.extend({
names : ['claire', 'alice'];
});
</code></pre>
<p>Both <code>extend</code> and <a href="#m-proto"><code>proto</code></a> accept
multiple objects as arguments:</p>
<pre><code>
var module = new (Stapes.subclass());
var instruments = { guitar : true };
var voices = { singing : true };
module.extend(instruments, voices);
console.log('guitar' in module, 'singing' in module); // true, true
</code></pre>
<p><code>extend</code> is also useful for stuff like configuration
properties.</p>
<pre><code>
var Module = new Stapes.subclass({
constructor : function(props) {
this.extend( props );
},
sayInfo : function() {
console.log(this.name + ' plays ' + this.instrument);
}
});
var module = new Module({
'name' : 'Johnny',
'instrument' : 'guitar'
});
module.sayInfo();
</code></pre>
<p id="m-plugins"><code>Stapes.extend()</code> can be used for writing extra methods
and properties that will be available on all Stapes modules,
even after you have created them. This is very useful for
writing plugins for functionality that isn't in Stapes.</p>
<pre><code>
Stapes.extend({
// Sets an DOM element for views
"setElement" : function(el) {
this.el = el;
this.$el = $(el); // jQuery or Zepto reference
}
});
var Module = Stapes.subclass();
var module = new Module();
module.setElement( document.getElementById("app") );
console.log( module.el ); // "#app" DOM element
console.log( module.$el ); // jQuery or Zepto element
</code></pre>
<p>All internal methods are available as the
<code>Stapes._</code> object, so if you want you can overwrite
and hack virtually all Stapes behaviour. Note that these
methods aren't documented, so you should take a look at the
source.</p>
</article>
<article id="m-proto">
<h1>proto</h1>
<h2>Module.proto( object )</h2>
<p>Adds properties and methods to the prototype of the module.</p>
<p>Note that it's usally easier to directly add methods to the prototype
using the <a href="#m-subclass"><code>subclass</code></a> method.
However,if you want to add methods to the prototype later on,
<code>proto</code> might be useful.</p>
<pre><code>
var Module = Stapes.subclass();
Module.proto({
'sayName' : function() {
console.log('I say my name');
}
});
'sayName' in Module; // false, not a static method
'sayName' in Module.prototype; // true
// Note that using proto is the same as:
Module.prototype.sayName = function() {
console.log('I say my name');
};
</code></pre>
<p>Both <code>proto</code> and <a href="#m-extend"><code>extend</code></a> accept
multiple objects as arguments. This can be very handy for mixins.</p>
<pre><code>
var Module = Stapes.subclass();
var instruments = { guitar : true };
var voices = { singing : true };
Module.proto(instruments, voices);
var module = new Module();
console.log(module.guitar, module.singing); // true, true
</code></pre>
</article>
<h3 id="m-events">API: Events</h3>
<article id="m-on">
<h1>on</h1>
<h2>module.on(eventName, handler [, context] )</h2>
<h2>module.on(object [, context] )</h2>
<h2>Stapes.on(object [, context] )</h2>
<h2>Stapes.on(eventName, handler [, context] )</h2>
<p>Add an event listener to a Stapes module or <a href="#m-mixinEvents">object
with mixed-in events</a>. When an event is
<a href="#m-emit">triggered</a> the <code>handler</code> will be called
with two arguments: <code>data</code>, which may contain any data, and an
<code>eventObject</code> that contains some useful information about the
event, such as the scope, target and event name.</p>
<p>As an optional third (when defining a single listener) or second
(when defining multipe listeners using an object) parameter you can set what the value of
<code>this</code> should be in the scope of the event handler.
This is handy to prevent having to temporarily save the scope
to a <code>self</code> or <code>that</code> variable or use the
new <code>bind</code> property of EcmaScript 5.</p>
<pre><code>
var module = Stapes.create();
module.on('ready', function() {
console.log('your module is ready!');
});
module.on({
"talk" : function() {
console.log('your module is talking!');
},
"walk" : function() {
console.log('your module is walking!');
},
"eat" : function( food ) {
console.log('your module is eating ' + (this.get('food') || 'nothing'));
}
});
var feedme = Stapes.create();
// Set the scope for the event handler to 'module'
feedme.on({
"feed" : function( food ) {
this.set('food', food);
}
}, module);
module.emit('eat');
// "your module is eating nothing"
feedme.emit('feed', 'cookies');
module.emit('eat');
// "your module is eating cookies"
module.get('food');
// "cookies"
feedme.get('food');
// null
</code></pre>
<p>The <code>on</code> method on the global <code>Stapes</code>
object can be used to listen to events from all defined modules.
</p>
<p>Listening to the special <code>all</code> event on the <code>Stapes</code>
global will trigger on <strong>all</strong> events thrown in all
objects. Very useful for debugging, but be careful not to leave it in
your production code :)</p>
<pre><code>
var Module = Stapes.subclass({
constructor : function(name) {
this.name = name;
},
beReady : function() {
this.emit('ready');
}
});
var module1 = new Module('module1');
var module2 = new Module('module2');
Stapes.on('ready', function(data, e) {
console.log('a ready event was triggered in: ' + e.scope.name);
});
module1.beReady(); // 'a ready event was triggered in module 1';
module2.beReady(); // 'a ready event was triggered in module 2';
</code></pre>
<p>The <code>all</code> event listener is also available on every module, so
you can listen to all events from a specific module.</p>
<pre><code>
var Module = Stapes.subclass({
"go" : function() {
this.emit('foo');
this.emit('bar');
}
});
var module = new Module();
module.on("all", function(data, e) {
console.log(e.type);
});
module.go(); // first 'foo', then 'bar'
</code></pre>
</article>
<article id="m-off">
<h1>off</h1>
<h2>module.off(eventType, handler)</h2>
<h2>module.off(eventType)</h2>
<h2>module.off()</h2>
<p>Removes event handlers from an object. Giving both an eventType
and a handler will remove that specific handler from a module.
Giving only an eventType will remove all handlers bound to
that event type.</p>
<p>With no arguments at all, <code>off()</code>
will remove <i>all</i> event handlers from a module.</p>
<pre><code>
var Module = Stapes.subclass();
var module = new Module();
var handler = function(){};
module.on({
"foo" : handler,
"bar" : function(){}
});
module.off("foo", handler); // Removes only the specific handler for foo
module.off("bar"); // Removes all 'bar' handlers
module.off(); // Removes all handlers
</code></pre>
</article>
<article id="m-emit">
<h1>emit</h1>
<h2>module.emit(eventName[, data])</h2>
<p>Trigger an event on the module. <code>eventName</code> can be a space seperated
string if you want to trigger more events. <code>data</code> can be any
Javascript variable you want, and will be passed to any event listeners</p>
<pre><code>
var Module = Stapes.subclass({
"sleep" : function() {
this.emit('sleeping', 'very deep');
}
});
var module = new Module();
module.on('sleeping', function(how) {
console.log("i'm sleeping " + how);
});
module.sleep(); // "i'm sleeping very deep"
</code></pre>
</article>
<article id="m-mixinEvents">
<h1>mixinEvents</h1>
<h2>Stapes.mixinEvents([object])</h2>
<p>
It's possible to add Stapes' <a href="#m-events">event handling
methods</a> to any Javascript object or function. This can be very
handy if you want to create an object that only uses event handlers, or
for an object or function that already exists and you don't want
to convert to a Stapes module.
</p>
<p>Here's how to add event methods to an object:</p>
<pre><code>
var module = {};
Stapes.mixinEvents(module);
module.on('sing', function() {
console.log("i'm singing!");
});
module.sing = function() {
this.emit('sing');
}
module.sing(); // i'm singing!
</code></pre>
<p>
<code>Stapes.mixinEvents</code> returns the <code>object</code>, so
you could write the first two lines from the previous example even shorter:
</p>
<pre><code>
var module = Stapes.mixinEvents( {} );
</code></pre>
<p>
No, wait! It can be even shorter! Without any arguments <code>Stapes.mixinEvents</code>
returns a new object with mixed-in events.
</p>
<pre><code>
var module = Stapes.mixinEvents();
</code></pre>
<p>You can also add event methods to a function:</p>
<pre><code>
function Module(what) {
this.what = what;
Stapes.mixinEvents(this);
}
Module.prototype.sing = function() {
this.emit('sing', this.what);
}
var m = new Module("Happy Birthday");
m.on('sing', function(what) {
console.log("i'm singing " + what + "!");
});
m.sing(); // i'm singing Happy Birthday!
</code></pre>
<p>Note that these events are also triggered on the main Stapes
object, so you can use <code>Stapes.on</code> to catch events
from these mixed-in objects as well.</p>
<pre><code>
Stapes.on('sing', function(data, e) {
console.log("Singing from " + e.scope.name);
});
var module = Stapes.mixinEvents( {} );
module.name = "a cool module!";
module.sing = function() {
this.emit('sing');
}
module.sing(); // 'Singing from a cool module!'
</code></pre>
</article>
<h3 id="m-data">API: Data methods</h3>
<article id="m-each">
<h1>each</h1>
<h2>module.each( function, [context] );</h2>
<p>Iterate over all attributes of a module.</p>
<p>When giving a <code>context</code> parameter this will be set as
the <code>this</code> value in the iterator function. If <code>context</code>
is not set it will be set to the module itself.</p>
<pre><code>
var Module = Stapes.subclass();
var singers = ['Johnny', 'Emmylou', 'Gram', 'June'];
var module = new Module();
module.msg = "I'll be your ";
module.push(singers);
module.each(function(singer) {
console.log(this.msg + singer);
});
// Using the second context parameter of each()
module.singers = new (Stapes.subclass());
module.singers.push( singers );
module.singers.each(function(singer) {
console.log(this.msg + singer);
}, module);
</code></pre>
</article>
<article id="m-filter">
<h1>filter</h1>
<h2>module.filter( function );</h2>
<p>Gets an array of attribute values using a custom function.</p>
<p>The callback <code>function</code> gets two arguments: the value of the attribute, and the original key.</p>
<pre><code>
module.set('singer', {
'name' : 'Elvis',
'instrument' : 'Guitar'
});
var singer = module.filter(function(item, key) {
return item.name === "Elvis" && key === "singer";
}); // [ { name : 'Elvis', instrument : 'Guitar' }]
</code></pre>
</article>
<article id="m-get">
<h1>get</h1>
<h2>module.get( key );</h2>
<h2>module.get( function );</h2>
<p>Gets an attribute by key. If the item is not available will return
<code>null</code></p>
<P>You can also use a function to get a specific key. This is
comparable to <code><a href="#m-filter">filter</a></code>,
however, <code>filter</code> always returns an array of results,
while <code>get</code> always returns a single result.</P>
<pre><code>
var Module = Stapes.subclass();
var module = new Module();
module.set({
'instrument': 'guitar',
'name': 'Johnny'
});
module.get('instrument'); // 'guitar'
module.get(function(value) {
return value === "guitar";
}); // 'instrument'
</code></pre>
</article>
<article id="m-getall">
<h1>getAll</h1>
<h2>module.getAll();</h2>
<p>Returns all the attributes of an module as an object. Handy for JSON
serializing and persistence layers.</p>
<p>Note that this method returns a copy/clone instead of a reference.</p>
</article>
<article id="m-getallasarray">
<h1>getAllAsArray</h1>
<h2>module.getAllAsArray();</h2>
<p>Returns all attributes as an array, so you can easily iterate.
Note that the original key of the attribute is always available as a
'id' key in the the value, provided your value is an object.</p>
<p>Note that this method returns a copy/clone instead of a reference.</p>
<pre><code>
module.set({
"name" : "Johnny",
"instrument" : "guitar"
});
module.getAllAsArray().length; // '2'
</code></pre>
</article>
<article id="m-has">
<h1>has</h1>
<h2>module.has( key );</h2>
<p>Checks if a key is available and returns true or false.</p>
<pre><code>
module.set('singer', 'Johnny');
module.has('singer'); // true
module.has('instrument'); // false
</code></pre>
</article>
<article id="m-map">
<h1>map</h1>
<h2>module.map( function, [context] )</h2>
<p>Just like <a href="#m-each"><code>each</code></a>, <code>map</code> iterates over
all attributes of a module. <code>map</code> returns a new array, where every attribute
value of a module has been passed through <code>function</code>.</p>
<pre><code>
module.push([1, 2, 3]);
var arr = module.map(function(nr) {
return nr * 2;
});
console.log(arr); // '[2, 4, 6]'
</code></pre>
<p>By default <code>map</code> gets the module as the value of <code>this</code>, but this
can be overwritten with the second argument.</p>
</article>
<article id="m-push">
<h1>push</h1>
<h2>module.push( value, [silent] );</h2>
<h2>module.push( array );</h2>
<p>Sets a value, automatically generates an unique uuid as a key.</p>
<p>You can also push an array of values.</p>
<p>Just as with <code>set</code> the optional <code>silent</code> flag will
prevent any change events from being emitted.</p>
<p>For the rest of the behaviour of this method see <code>
<a href="#m-set">set</a></code>.</p>
<pre><code>
m.push("foo");
m.getAll(); // will look something like { "5323be61-afb8-4034-b408-51132756cd43" : "foo"}
m.push([
"foo",
"bar",
"baz"
]);
</code></pre>
<p><code>push</code> returns the module, so this method is chainable.</p>
</article>
<article id="m-remove">
<h1>remove</h1>
<h2>module.remove( key, [silent] );</h2>
<h2>module.remove( function );</h2>
<p>Deletes an attribute. Triggers <code>remove</code> and
<code>change</code> events.</p>
<p><code>remove</code> also triggers namespaced <code>change</code> and
<code>remove</code> events (e.g. <code>change:foo</code> and
<code>remove:foo</code>).</p>
<p>You can either use a <code>key</code> as an argument or a
<code>function</code></p>
<pre><code>
module.remove(function(item) {
return item.done === true;
});
</code></pre>
<p>It's possible to remove multiple keys in one go, simply space seperate them:</p>
<pre><code>
module.remove('singer instrument johnny');
</code></pre>
<p>If you do not want your <code>remove</code> to trigger an event set
the optional <code>silent</code> flag to true.</p>
<pre><code>
module.remove('singer', true /* <-- silent flag */);
</code></pre>
<p><code>remove</code> returns the module, so this method is chainable.</p>
</article>
<article id="m-set">
<h1>set</h1>
<h2>module.set(key, value, [silent]);</h2>
<h2>module.set(object);</h2>
<p>Sets an attribute. Use <code><a href="#m-push">push</a></code> if you want to 'push' a value
with a random uuid, for collections.</p>
<p>To set multiple attributes in one go, use an object as an first argument.</p>
<p>Every attribute will trigger a <code>change</code> event. A key
that doesn't exist will trigger a <code>create</code> event, a key
that does exist will trigger an <code>update</code> event.</p>
<p>All events will have the key of the attribute as their event value.</p>
<p>Special namespaced events will also be triggered. These events have a value
of <code>eventType:key</code>. So for example, a <code>set</code> on
an attribute called 'name' will generate a <code>change:name</code>
event. These events will have the attribute value instead of the
key as a data argument in the event callback.</p>