Permalink
Browse files

new annotations

@PostConstruct, @PreDestroy
yaap/wire/html package: @On event-binding
  • Loading branch information...
warmuuh committed Mar 28, 2013
1 parent e702e2d commit 2421a0edb3a0e83afadcb6d62fc68b27cd3cd377
View
@@ -6,7 +6,7 @@
}
},
"jshint" : {
- options: {node: true, white:false},
+ options: {node: true, browser: true, white:false},
"global" : ["console", "define", "exports", "module", "require"]
}
}
View
@@ -6,8 +6,10 @@ function MyClass (val){
MyClass.prototype = {
- doSomething: function( /*@Default("Default Message")*/msg, /*@Autowired("message")*/msg2, /*@Autowired*/message) {
- console.log("default: " +msg );
+ doSomething: function( /*@Default("Default Message")*/msg, /*@Autowired("message")*/msg2, /*@Autowired*/message) {
+ //console.log( node );
+ //output.value = "default: " +msg +"\n";
+ console.log("default: " +msg);
console.log("wired: " + this.value );
console.log("autowired: " +msg2 );
console.log("autowired(pname): " + message );
@@ -19,9 +21,12 @@ MyClass.prototype = {
console.log(msg + " " +this.value);
},
- doSomethingElseAgain: function( message) /*@Autowired*/{
- console.log("autowired(fn): " + message );
- },
+
+ onNodeClick: function( e )
+ /* @On("node", "click") */
+ {
+ alert("you clicked the button: " + e.target.value); // + node);
+ }
};
View
@@ -11,7 +11,7 @@
packages: {
wire: { path: 'components/wire', main: 'wire'},
when: { path: 'components/when/', main: 'when'},
- yaap: { path: 'components/yaap/yaap', main: 'yaap'},
+ yaap: { path: '../yaap', main: 'yaap'},
meld: { path: 'components/meld', main: 'meld' },
underscore:{ path: 'components/underscore', main: 'underscore',
config: {moduleLoader: 'curl/loader/cjsm11'}
@@ -24,15 +24,17 @@
</script>
</head>
<body>
-
-<script src="components/curl/src/curl.js"></script>
+<input class="pressed" type="button" id="myButton" value="Button1" />
+<input class="pressed" type="button" id="myButton" value="Button2" />
+<textarea style="display:block;width:200px;height:200px" id="output"></textarea>
+<script src="./components/curl/src/curl.js"></script>
<script type="text/javascript">
curl(['wire!spec']).then(function(ctx){
console.log("----- initialized -----");
ctx.myComponent.doSomething();
- ctx.myComponent.doSomethingElseAgain();
+
},function(err){
console.log("err: " + err);
View
@@ -3,6 +3,8 @@ define([], function(){
return {
message: "autowired",
+ node: {$ref: "dom.all!.pressed"},
+ //out: {$ref: "dom!output"},
myComponent: {
create: {
module: 'MyClass',
@@ -12,10 +14,12 @@ return {
plugins: [
//{module: "wire/debug", trace: true},
- {module: "yaap/wire/wireplugin"}
-
+ {module: "yaap/wire/wireplugin"},
+ {module: "yaap/wire/html/wireplugin"},
+ {module: "wire/dom"},
+ {module: "wire/on"}
]
-}
+};
});
View
@@ -31,7 +31,15 @@ MyClass.prototype = {
/* @Autowired @Initialize */
{
console.log("autowired init fn: " + message);
+ },
+
+ onDestroy: function()
+ /*@PreDestroy*/
+ {
+ console.log("Pre Destroy");
}
+
+
};
View
@@ -15,7 +15,7 @@ wire({
args: "wired"
}
},
-
+
plugins: [
//{module: "wire/debug", trace: true},
{module: "../yaap/wire"}
@@ -26,7 +26,10 @@ wire({
console.log("----- initialized -----");
ctx.myComponent.doSomething();
- ctx.myComponent.doSomethingElseAgain();
-
+ ctx.myComponent.doSomethingElseAgain();
+
+
+
+ ctx.destroy();
}, function(err){console.error(err);});
@@ -1,11 +1,15 @@
"use strict";
module.exports = {
- fn1: function() /*@Initialize*/
+ fn1: function() /*@PostConstruct*/
{
this.value = 1;
},
- fn2: function(bean)/*@Autowired @Initialize*/
+ fn2: function(bean)/*@Autowired @PostConstruct*/
{
this.bean = bean;
+ },
+ fn3: function()/*@PreDestroy*/
+ {
+ this.value = 2;
}
};
View
@@ -34,7 +34,7 @@ module.exports = {
test.done();
}, console.error);
},
- "wiring with @Initialize": function(test){
+ "wiring with @PostConstruct": function(test){
var spec = {
bean: "autowiredValue",
@@ -48,6 +48,21 @@ module.exports = {
test.equal(ctx.testInstance.value, 1);
test.done();
}, console.error);
+ },
+ "wiring with @PreDestroy": function(test){
+
+ var spec = {
+ bean: "autowiredValue",
+ testInstance:{create: './classes/InitializeTest'},
+ plugins: [{module:'../wire'}]
+ };
+
+ wire(spec, {require:require}).then(function(ctx){
+ var bean = ctx.testInstance;
+ ctx.destroy();
+ test.equal(bean.value, 2);
+ test.done();
+ }, console.error);
}
};
@@ -0,0 +1,3 @@
+{
+ "main":"./wireplugin"
+}
@@ -0,0 +1,47 @@
+/** @license MIT License (c) copyright 2013 original author or authors */
+
+/**
+ * @Autowired annotation processor.
+ *
+ * Autowires parameters of function calls based on a given or the parameter name.
+ *
+ * needs wire to be in the configuration object.
+ *
+ *
+ * Licensed under the MIT License at:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * @author Peter Mucha
+ *
+ * @version 0.0.3
+ */
+"use strict";
+(function(define) {
+define([ "wire/on"],
+function(on) {
+
+return {
+ annotation: "@On",
+
+
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ var wire = context.wire;
+ wire.resolveRef(annotationParams[0]).then(function(nodeRef){
+ if( nodeRef.length && nodeRef.length > 0 ) {
+
+ for(var i = 0; i < nodeRef.length; ++i)
+ on(nodeRef[i], annotationParams[1], function(evt){
+ obj[fnDescription.name](evt); //dynamic call because there could be other annotations
+ });
+ }
+ else
+ on(nodeRef, annotationParams[1], function(evt){
+ obj[fnDescription.name](evt); //dynamic call because there could be other annotations
+ });
+
+ });
+
+ }
+};
+
+});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});
@@ -0,0 +1,30 @@
+/** @license MIT License (c) copyright Peter Mucha */
+
+/**
+ * yaap/wire plugin
+ * wire plugin that provides annotation processing of components, e.g. @Autowired
+ *
+ * wire is part of the cujo.js family of libraries (http://cujojs.com/)
+ *
+ * Licensed under the MIT License at:
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+(function(define) {
+ define(["underscore", "../../yaap",
+ "./plugins/OnProcessor",
+ "wire"], function(_, yaap, on, wire) {
+ "use strict";
+
+
+ return {
+ wire$plugin: function(ready, destroyed, options) {
+ yaap.register(on);
+ return {};
+ }
+ };
+
+ });
+}(
+typeof define == 'function' && define.amd ? define : function(deps, factory) {
+ module.exports = factory.apply(this, deps.map(require));
+}));
@@ -39,6 +39,7 @@ return {
context.wire(refs).then(function (resolvedRefs) {
+
var origFn = obj[fnDescription.name];
obj[fnDescription.name] = function(){
@@ -1,11 +1,10 @@
/** @license MIT License (c) copyright 2013 original author or authors */
/**
- * @Autowired annotation processor.
+ * @PostConstruct annotation processor.
*
- * Autowires parameters of function calls based on a given or the parameter name.
+ * Invokes annotated method after configuration of the bean
*
- * needs wire to be in the configuration object.
*
*
* Licensed under the MIT License at:
@@ -25,7 +24,7 @@ var registeredInitializers = [];
return {
- annotation: "@Initialize",
+ annotation: "@PostConstruct",
@@ -0,0 +1,43 @@
+/** @license MIT License (c) copyright 2013 original author or authors */
+
+/**
+ * @PreDestroy annotation processor.
+ *
+ * Invokes annotated method of the bean before destroying context
+ *
+ *
+ *
+ * Licensed under the MIT License at:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * @author Peter Mucha
+ *
+ * @version 0.0.3
+ */
+"use strict";
+(function(define) {
+define([],
+function() {
+
+
+var registeredDestroyers = [];
+
+
+return {
+ annotation: "@PreDestroy",
+
+
+
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ registeredDestroyers.push({obj: obj, name: fnDescription.name});
+ },
+
+ beforeDestroying: function(obj){
+ while(registeredDestroyers.length > 0){
+ var init = registeredDestroyers.pop();
+ init.obj[init.name](); //call it
+ }
+ }
+};
+
+});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});
View
@@ -10,7 +10,11 @@
* http://www.opensource.org/licenses/mit-license.php
*/
(function(define) {
- define(["underscore", "../yaap", "./plugins/AutowireProcessor", "./plugins/InitializeProcessor", "wire"], function(_, yaap, autowire, initialize, wire) {
+ define(["underscore", "../yaap",
+ "./plugins/AutowireProcessor",
+ "./plugins/PostConstructProcessor",
+ "./plugins/PreDestroyProcessor",
+ "wire"], function(_, yaap, autowire, postConstruct, preDestroy, wire) {
"use strict";
@@ -27,22 +31,28 @@
function afterProcessing(resolver, facet, wire) {
//TODO: find more decoupled solution
- initialize.afterProcessing(facet.target);
+ postConstruct.afterProcessing(facet.target);
resolver.resolve();
}
-
+ function beforeDestroying(resolver, facet, wire) {
+ //TODO: find more decoupled solution
+ preDestroy.beforeDestroying(facet.target);
+ resolver.resolve();
+ }
return {
wire$plugin: function(ready, destroyed, options) {
yaap.register(autowire); //register annotation processor for @Autowire
- yaap.register(initialize); //register annotation processor for @Initialize
+ yaap.register(postConstruct); //register annotation processor for @Initialize
+ yaap.register(preDestroy); //register annotation processor for @PreDestroy
return {
configure: processAnnotations,
- "ready": afterProcessing
+ "ready": afterProcessing,
+ "destroy": beforeDestroying
};
}
};
Oops, something went wrong.

0 comments on commit 2421a0e

Please sign in to comment.