public
Description: Random things I've written in JavaScript. Most are untested and/or half-baked.
Clone URL: git://github.com/savetheclocktower/javascript-stuff.git
Search Repo:
Added bindable.js for linking an element with a widget instance.
Thu May 08 21:06:43 -0700 2008
commit  1cfebe803a58724f5eb1365f492e029ac53b3e5f
tree    a4dd088498366c874c0bedb558c5995d4cb425b0
parent  6675372eca71395e0d30f7781079af76f12af940
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,45 @@
0
+// Mixin for linking an element and a widget instance that controls its
0
+// behavior. (Storing the instance as a property of the element itself
0
+// leads to memory leaks.)
0
+//
0
+// Usage:
0
+//
0
+// var TableWidget = Class.create(Bindable, {
0
+// initialize: function(element, options) {
0
+// this.element = $(element);
0
+// this.bindToElement();
0
+// /* ... */
0
+// }
0
+//
0
+// /* ... */
0
+// });
0
+//
0
+// To retrieve an instance:
0
+//
0
+// $('some_table').getInstanceOf(TableWidget);
0
+
0
+var Bindable = {
0
+ bindToElement: function(element) {
0
+ element = element || this.element;
0
+ var id = element.identify();
0
+
0
+ if (this.constructor._instances)
0
+ this.constructor._instances = {};
0
+
0
+ this.constructor._instances[id] = this;
0
+ }
0
+};
0
+
0
+Element.addMethods({
0
+ getInstanceOf: function(element, klass) {
0
+ element = $(element);
0
+ var instances = klass._instances;
0
+
0
+ if (instances && element.id && instances[element.id]) {
0
+ return instances[element.id];
0
+ } else {
0
+ throw "Must call Bindable#bindToElement first."
0
+ }
0
+ }
0
+});

Comments

  • samleb Thu May 15 06:41:39 -0700 2008

    I really like the idea, it’s very useful with event delegation.
    I’m actually using it, thanks!