Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

new annotations

@PostConstruct, @PreDestroy
yaap/wire/html package: @On event-binding
  • Loading branch information...
commit 2421a0edb3a0e83afadcb6d62fc68b27cd3cd377 1 parent e702e2d
Peter authored March 28, 2013
2  .scripted
@@ -6,7 +6,7 @@
6 6
     }
7 7
   },
8 8
   "jshint" : {
9  
-  	options: {node: true, white:false},
  9
+  	options: {node: true, browser: true, white:false},
10 10
   	"global" : ["console", "define", "exports", "module", "require"]
11 11
   }
12 12
 }
15  browser-example/MyClass.js
@@ -6,8 +6,10 @@ function MyClass (val){
6 6
 
7 7
 MyClass.prototype = {
8 8
 
9  
-	doSomething: function( /*@Default("Default Message")*/msg, /*@Autowired("message")*/msg2, /*@Autowired*/message) {
10  
-		console.log("default: " +msg );
  9
+	doSomething: function( 	/*@Default("Default Message")*/msg, 	/*@Autowired("message")*/msg2, 	/*@Autowired*/message) {
  10
+		//console.log( node );
  11
+		//output.value = "default: " +msg +"\n";
  12
+		console.log("default: " +msg);
11 13
 		console.log("wired: " + this.value );
12 14
 		console.log("autowired: " +msg2 );
13 15
 		console.log("autowired(pname): " + message );
@@ -19,9 +21,12 @@ MyClass.prototype = {
19 21
 		console.log(msg + " " +this.value);
20 22
 	},
21 23
   
22  
-  doSomethingElseAgain: function( message) /*@Autowired*/{
23  
-		console.log("autowired(fn): " + message );
24  
-	},
  24
+  
  25
+  onNodeClick: function( e ) 
  26
+  /* @On("node", "click") */  
  27
+  {
  28
+		alert("you clicked the button: " + e.target.value); // + node);
  29
+  }
25 30
   
26 31
 };
27 32
 
10  browser-example/index.html
@@ -11,7 +11,7 @@
11 11
 			 packages: {
12 12
 		        wire: { path: 'components/wire', main: 'wire'},
13 13
 		        when: { path: 'components/when/', main: 'when'},
14  
-		        yaap: { path: 'components/yaap/yaap', main: 'yaap'},
  14
+		        yaap: { path: '../yaap', main: 'yaap'},
15 15
 		        meld: { path: 'components/meld', main: 'meld' },
16 16
 		        underscore:{ path: 'components/underscore', main: 'underscore',
17 17
 		        		config: {moduleLoader: 'curl/loader/cjsm11'}
@@ -24,15 +24,17 @@
24 24
 </script>
25 25
 </head>
26 26
 <body>
27  
-
28  
-<script src="components/curl/src/curl.js"></script>
  27
+<input class="pressed" type="button" id="myButton" value="Button1" />
  28
+<input class="pressed" type="button" id="myButton" value="Button2" />
  29
+<textarea style="display:block;width:200px;height:200px" id="output"></textarea>
  30
+<script src="./components/curl/src/curl.js"></script>
29 31
 <script type="text/javascript">
30 32
 
31 33
 curl(['wire!spec']).then(function(ctx){
32 34
 
33 35
 	console.log("----- initialized -----");
34 36
 	ctx.myComponent.doSomething();
35  
-  	ctx.myComponent.doSomethingElseAgain();
  37
+  	
36 38
   	
37 39
 },function(err){
38 40
 	console.log("err: " + err);
10  browser-example/spec.js
@@ -3,6 +3,8 @@ define([], function(){
3 3
 
4 4
 return {
5 5
   message: "autowired",
  6
+  node:   {$ref: "dom.all!.pressed"},
  7
+  //out: {$ref: "dom!output"},
6 8
   myComponent: {
7 9
           create: {
8 10
 	            module: 'MyClass',
@@ -12,10 +14,12 @@ return {
12 14
   	
13 15
   plugins: [
14 16
     //{module: "wire/debug", trace: true},
15  
-     {module: "yaap/wire/wireplugin"}
16  
-     
  17
+     {module: "yaap/wire/wireplugin"},
  18
+     {module: "yaap/wire/html/wireplugin"},
  19
+     {module: "wire/dom"},
  20
+     {module: "wire/on"}
17 21
   ]
18 22
         
19  
-}
  23
+};
20 24
 
21 25
 });
8  node-example/MyClass.js
@@ -31,7 +31,15 @@ MyClass.prototype = {
31 31
 	/* @Autowired @Initialize */
32 32
 	{
33 33
 		console.log("autowired init fn: " + message);
  34
+	},
  35
+	
  36
+	onDestroy: function()
  37
+	/*@PreDestroy*/
  38
+	{
  39
+		console.log("Pre Destroy");
34 40
 	}
  41
+	
  42
+	
35 43
 };
36 44
 
37 45
 
9  node-example/main.js
@@ -15,7 +15,7 @@ wire({
15 15
 				            args: "wired"
16 16
 				        }
17 17
         },
18  
-        	
  18
+
19 19
         plugins: [
20 20
           //{module: "wire/debug", trace: true},
21 21
            {module: "../yaap/wire"}
@@ -26,7 +26,10 @@ wire({
26 26
 
27 27
 	console.log("----- initialized -----");
28 28
 	ctx.myComponent.doSomething();
29  
-  	ctx.myComponent.doSomethingElseAgain();
30  
-
  29
+	ctx.myComponent.doSomethingElseAgain();
  30
+		
  31
+		
  32
+		
  33
+	ctx.destroy();
31 34
 }, function(err){console.error(err);});
32 35
 
8  yaap/test/classes/InitializeTest.js
... ...
@@ -1,11 +1,15 @@
1 1
 "use strict";
2 2
 module.exports = {
3  
-	fn1: function() /*@Initialize*/
  3
+	fn1: function() /*@PostConstruct*/
4 4
 	{
5 5
 		this.value = 1;
6 6
 	},
7  
-	fn2: function(bean)/*@Autowired @Initialize*/
  7
+	fn2: function(bean)/*@Autowired @PostConstruct*/
8 8
 	{
9 9
 		this.bean = bean;
  10
+	},
  11
+	fn3: function()/*@PreDestroy*/
  12
+	{
  13
+		this.value = 2;
10 14
 	}
11 15
 };
17  yaap/test/wire.js
@@ -34,7 +34,7 @@ module.exports = {
34 34
 			test.done();
35 35
 		}, console.error);
36 36
 	},
37  
-	"wiring with @Initialize": function(test){
  37
+	"wiring with @PostConstruct": function(test){
38 38
 		
39 39
 		var spec = {
40 40
 			bean: "autowiredValue",
@@ -48,6 +48,21 @@ module.exports = {
48 48
 			test.equal(ctx.testInstance.value, 1);
49 49
 			test.done();
50 50
 		}, console.error);
  51
+	},
  52
+	"wiring with @PreDestroy": function(test){
  53
+		
  54
+		var spec = {
  55
+			bean: "autowiredValue",
  56
+			testInstance:{create: './classes/InitializeTest'},
  57
+			plugins: [{module:'../wire'}]
  58
+		};
  59
+		
  60
+		wire(spec, {require:require}).then(function(ctx){
  61
+			var bean = ctx.testInstance;
  62
+			ctx.destroy();
  63
+			test.equal(bean.value, 2);
  64
+			test.done();
  65
+		}, console.error);
51 66
 	}
52 67
 	
53 68
 };
3  yaap/wire/html/package.json
... ...
@@ -0,0 +1,3 @@
  1
+{
  2
+  "main":"./wireplugin"
  3
+} 
47  yaap/wire/html/plugins/OnProcessor.js
... ...
@@ -0,0 +1,47 @@
  1
+/** @license MIT License (c) copyright 2013 original author or authors */
  2
+
  3
+/**
  4
+ * @Autowired annotation processor.
  5
+ * 
  6
+ * Autowires parameters of function calls based on a given or the parameter name.
  7
+ * 
  8
+ * needs wire to be in the configuration object. 
  9
+ *    
  10
+ *
  11
+ * Licensed under the MIT License at:
  12
+ * http://www.opensource.org/licenses/mit-license.php
  13
+ *
  14
+ * @author Peter Mucha
  15
+ *
  16
+ * @version 0.0.3
  17
+ */
  18
+"use strict";
  19
+(function(define) { 
  20
+define([ "wire/on"], 
  21
+function(on) {
  22
+    
  23
+return  {
  24
+  annotation: "@On",
  25
+  
  26
+  
  27
+  processFunction: function(obj, fnDescription, annotationParams, context){
  28
+    var wire = context.wire;
  29
+	wire.resolveRef(annotationParams[0]).then(function(nodeRef){
  30
+		if( nodeRef.length && nodeRef.length > 0 ) {
  31
+
  32
+			for(var i = 0; i < nodeRef.length; ++i)
  33
+				on(nodeRef[i], annotationParams[1], function(evt){
  34
+					obj[fnDescription.name](evt); //dynamic call because there could be other annotations
  35
+				});
  36
+		}
  37
+		else
  38
+			on(nodeRef, annotationParams[1], function(evt){
  39
+					obj[fnDescription.name](evt); //dynamic call because there could be other annotations
  40
+				});
  41
+	
  42
+	});
  43
+    
  44
+  }
  45
+};
  46
+
  47
+});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});
30  yaap/wire/html/wireplugin.js
... ...
@@ -0,0 +1,30 @@
  1
+/** @license MIT License (c) copyright Peter Mucha */
  2
+
  3
+/**
  4
+ * yaap/wire plugin
  5
+ * wire plugin that provides annotation processing of components, e.g. @Autowired
  6
+ *
  7
+ * wire is part of the cujo.js family of libraries (http://cujojs.com/)
  8
+ *
  9
+ * Licensed under the MIT License at:
  10
+ * http://www.opensource.org/licenses/mit-license.php
  11
+ */
  12
+(function(define) {
  13
+        define(["underscore", "../../yaap", 
  14
+        "./plugins/OnProcessor",
  15
+        "wire"], function(_, yaap, on, wire) {
  16
+                "use strict";
  17
+
  18
+
  19
+                return {
  20
+                        wire$plugin: function(ready, destroyed, options) {
  21
+								yaap.register(on);
  22
+                                return {};
  23
+                        }
  24
+                };
  25
+
  26
+        });
  27
+}(
  28
+typeof define == 'function' && define.amd ? define : function(deps, factory) {
  29
+        module.exports = factory.apply(this, deps.map(require));
  30
+}));
1  yaap/wire/plugins/AutowireProcessor.js
@@ -39,6 +39,7 @@ return  {
39 39
     
40 40
     context.wire(refs).then(function (resolvedRefs) {
41 41
       
  42
+
42 43
       var origFn = obj[fnDescription.name];
43 44
       
44 45
       obj[fnDescription.name] = function(){
7  yaap/wire/plugins/InitializeProcessor.js → yaap/wire/plugins/PostConstructProcessor.js
... ...
@@ -1,11 +1,10 @@
1 1
 /** @license MIT License (c) copyright 2013 original author or authors */
2 2
 
3 3
 /**
4  
- * @Autowired annotation processor.
  4
+ * @PostConstruct annotation processor.
5 5
  * 
6  
- * Autowires parameters of function calls based on a given or the parameter name.
  6
+ * Invokes annotated method after configuration of the bean
7 7
  * 
8  
- * needs wire to be in the configuration object. 
9 8
  *    
10 9
  *
11 10
  * Licensed under the MIT License at:
@@ -25,7 +24,7 @@ var registeredInitializers = [];
25 24
 
26 25
     
27 26
 return  {
28  
-  annotation: "@Initialize",
  27
+  annotation: "@PostConstruct",
29 28
  
30 29
 
31 30
   
43  yaap/wire/plugins/PreDestroyProcessor.js
... ...
@@ -0,0 +1,43 @@
  1
+/** @license MIT License (c) copyright 2013 original author or authors */
  2
+
  3
+/**
  4
+ * @PreDestroy annotation processor.
  5
+ * 
  6
+ * Invokes annotated method of the bean before destroying context
  7
+ * 
  8
+ *    
  9
+ *
  10
+ * Licensed under the MIT License at:
  11
+ * http://www.opensource.org/licenses/mit-license.php
  12
+ *
  13
+ * @author Peter Mucha
  14
+ *
  15
+ * @version 0.0.3
  16
+ */
  17
+"use strict";
  18
+(function(define) {
  19
+define([], 
  20
+function() {
  21
+
  22
+
  23
+var registeredDestroyers = [];
  24
+
  25
+    
  26
+return  {
  27
+  annotation: "@PreDestroy",
  28
+ 
  29
+
  30
+  
  31
+  processFunction: function(obj, fnDescription, annotationParams, context){
  32
+	registeredDestroyers.push({obj: obj, name: fnDescription.name});
  33
+  },
  34
+  
  35
+  beforeDestroying: function(obj){
  36
+	while(registeredDestroyers.length > 0){
  37
+		var init = registeredDestroyers.pop();
  38
+		init.obj[init.name](); //call it
  39
+	}
  40
+  }
  41
+};
  42
+
  43
+});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});
20  yaap/wire/wireplugin.js
@@ -10,7 +10,11 @@
10 10
  * http://www.opensource.org/licenses/mit-license.php
11 11
  */
12 12
 (function(define) {
13  
-        define(["underscore", "../yaap", "./plugins/AutowireProcessor", "./plugins/InitializeProcessor", "wire"], function(_, yaap, autowire, initialize, wire) {
  13
+        define(["underscore", "../yaap", 
  14
+        "./plugins/AutowireProcessor", 
  15
+        "./plugins/PostConstructProcessor", 
  16
+        "./plugins/PreDestroyProcessor", 
  17
+        "wire"], function(_, yaap, autowire, postConstruct, preDestroy, wire) {
14 18
                 "use strict";
15 19
 
16 20
 
@@ -27,22 +31,28 @@
27 31
 
28 32
 				function afterProcessing(resolver, facet, wire) {
29 33
 						//TODO: find more decoupled solution
30  
-						initialize.afterProcessing(facet.target);
  34
+						postConstruct.afterProcessing(facet.target);
31 35
                         resolver.resolve();
32 36
 				}
33 37
 
34  
-
  38
+				function beforeDestroying(resolver, facet, wire) {
  39
+						//TODO: find more decoupled solution
  40
+						preDestroy.beforeDestroying(facet.target);
  41
+                        resolver.resolve();
  42
+				}
35 43
 
36 44
 
37 45
                 return {
38 46
                         wire$plugin: function(ready, destroyed, options) {
39 47
 
40 48
                                 yaap.register(autowire); //register annotation processor for @Autowire
41  
-								yaap.register(initialize); //register annotation processor for @Initialize
  49
+								yaap.register(postConstruct); //register annotation processor for @Initialize
  50
+								yaap.register(preDestroy); //register annotation processor for @PreDestroy
42 51
 
43 52
                                 return {
44 53
                                         configure: processAnnotations,
45  
-                                        "ready": afterProcessing
  54
+                                        "ready": afterProcessing,
  55
+                                        "destroy": beforeDestroying
46 56
                                 };
47 57
                         }
48 58
                 };
12  yaap/yaap.js
@@ -94,12 +94,12 @@ function(_, registry, NotNullProcessor, DefaultProcessor, wire, PanPG_util, es5,
94 94
           try{
95 95
             fnDescriptions = PanPG_util.treeWalker(walker, ast);
96 96
           } catch(e){console.error(e); throw e;}
97  
-		    
98  
-			    _(fnDescriptions).each(function(fnDescription){
99  
-			        fnDescription.name = f;
100  
-				    callProcessors(obj, fnDescription, config);
101  
-				});
102  
-		  });
  97
+					    
  98
+		_(fnDescriptions).each(function(fnDescription){
  99
+		    fnDescription.name = f;
  100
+		    callProcessors(obj, fnDescription, config);
  101
+		});
  102
+	  });
103 103
       
104 104
      return obj; 
105 105
 		}

0 notes on commit 2421a0e

Please sign in to comment.
Something went wrong with that request. Please try again.