Skip to content

Commit

Permalink
@initialize for wire added
Browse files Browse the repository at this point in the history
  • Loading branch information
warmuuh committed Mar 18, 2013
1 parent 7271e52 commit 07578db
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .scripted
Expand Up @@ -6,6 +6,7 @@
}
},
"jshint" : {
options: {node: true, white:false}
options: {node: true, white:false},
"global" : ["console", "define", "exports", "module", "require"]
}
}
29 changes: 19 additions & 10 deletions node-example/MyClass.js
@@ -1,28 +1,37 @@
"use strict";

function MyClass (val){
function MyClass(val) {
this.value = val;
}


MyClass.prototype = {

doSomething: function( /*@Default("Default Message")*/msg, /*@Autowired("message")*/msg2, /*@Autowired*/message) {
console.log("default: " +msg );
console.log("wired: " + this.value );
console.log("autowired: " +msg2 );
console.log("autowired(pname): " + message );
doSomething: function (
/*@Default("Default Message")*/ msg,
/*@Autowired("message")*/ msg2,
/*@Autowired*/ message) {
console.log("default: " + msg);
console.log("wired: " + this.value);
console.log("autowired: " + msg2);
console.log("autowired(pname): " + message);

},


doSomethingElse: function( msg) /*@NotNull*/{
console.log(msg + " " +this.value);
doSomethingElse: function (msg) /*@NotNull*/{
console.log(msg + " " + this.value);
},

doSomethingElseAgain: function( message) /*@Autowired*/{
console.log("autowired(fn): " + message );
doSomethingElseAgain: function (message) /*@Autowired*/{
console.log("autowired(fn): " + message);
},

onInit: function (message)
/* @Autowired @Initialize */
{
console.log("autowired init fn: " + message);
}
};


Expand Down
11 changes: 11 additions & 0 deletions yaap/test/classes/InitializeTest.js
@@ -0,0 +1,11 @@
"use strict";
module.exports = {
fn1: function() /*@Initialize*/
{
this.value = 1;
},
fn2: function(bean)/*@Autowired @Initialize*/
{
this.bean = bean;
}
};
16 changes: 16 additions & 0 deletions yaap/test/wire.js
Expand Up @@ -33,5 +33,21 @@ module.exports = {
test.equal(ctx.testInstance.fn3(), ctx.bean);
test.done();
}, console.error);
},
"wiring with @Initialize": function(test){

var spec = {
bean: "autowiredValue",
testInstance:{create: './classes/InitializeTest'},
plugins: [//{module: "wire/debug", trace: true},
{module:'../wire'}]
};

wire(spec, {require:require}).then(function(ctx){
test.equal(ctx.testInstance.bean, ctx.bean);
test.equal(ctx.testInstance.value, 1);
test.done();
}, console.error);
}

};
1 change: 0 additions & 1 deletion yaap/wire/plugins/AutowireProcessor.js
Expand Up @@ -68,7 +68,6 @@ return {
});

when.map(refs, cfg.wire).then(function (resolvedRefs){
//console.log("1");
var origFn = obj[fnObj.name];

obj[fnObj.name] = function(){
Expand Down
46 changes: 46 additions & 0 deletions yaap/wire/plugins/InitializeProcessor.js
@@ -0,0 +1,46 @@
/** @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([],
function() {


var registeredInitializers = [];


return {
annotation: "@Initialize",



processFunction: function(obj, fnObj, annotationParams, cfg){


registeredInitializers.push({obj: obj, name: fnObj.name});
},

afterProcessing: function(obj){
while(registeredInitializers.length > 0){
var init = registeredInitializers.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);}));});
16 changes: 12 additions & 4 deletions yaap/wire/wireplugin.js
Expand Up @@ -10,20 +10,26 @@
* http://www.opensource.org/licenses/mit-license.php
*/
(function(define) {
define(["underscore", "../yaap", "./plugins/AutowireProcessor", "wire"], function(_, yaap, autowire, wire) {
define(["underscore", "../yaap", "./plugins/AutowireProcessor", "./plugins/InitializeProcessor", "wire"], function(_, yaap, autowire, initialize, wire) {
"use strict";


function annotatesFacet(resolver, facet, wire) {
function annotatesFacet(resolver, facet, wire) {
var options = facet.options;
var obj = facet.target;
yaap.process(obj, {
wire: wire
});
resolver.resolve();


}


function afterProcessing(resolver, facet, wire) {
//TODO: find more decoupled solution
initialize.afterProcessing(facet.target);
resolver.resolve();
}



Expand All @@ -32,9 +38,11 @@
wire$plugin: function(ready, destroyed, options) {

yaap.register(autowire); //register annotation processor for @Autowire
yaap.register(initialize); //register annotation processor for @Initialize

return {
initialize: annotatesFacet
configure: annotatesFacet,
"ready": afterProcessing
};
}
};
Expand Down
7 changes: 5 additions & 2 deletions yaap/yaap.js
Expand Up @@ -50,6 +50,9 @@ function(_, registry, NotNullProcessor, DefaultProcessor, wire, PanPG_util, es5,
});





};


Expand All @@ -61,7 +64,7 @@ function(_, registry, NotNullProcessor, DefaultProcessor, wire, PanPG_util, es5,
register: function (processor) {
registry.register([processor]);
},

process: function (obj, config){
var functions = _(obj).functions();
_(functions).each(function(f){
Expand Down Expand Up @@ -91,7 +94,7 @@ function(_, registry, NotNullProcessor, DefaultProcessor, wire, PanPG_util, es5,

return obj;
}
}
};



Expand Down

0 comments on commit 07578db

Please sign in to comment.