mootools on ringojs - 28 Nov 2010
My blog platform ringopress is getting moopowered from now on.
There are many reasons for this: i come from java world, and class, extends and such goodies is a good way for me to express myself in coding, then, my platform is getting more complex, and i try to reuse code, add more features [plugins ... ] and i cannot do it in javascript - i know it can be done, but it beats me; also, i have experience in mootools from here and here . the ride in adding mootools into ringojs was not so easy as i presumed.
First, mootools adds goodies to globals. The one i like and use is function's bind. ringojs has it too, as it has many of the things mootools adds to String, Array... . This won't be a problem unless you want to offer others the pleasure of having their own version of Array.each, or if you do not want to stumble into problems of for ... in ... Array. Unfortunately I did run across such constructs, and proposed a patch to Hannes, the main developer of ringojs. He accepted it, and right now it is ok. But i might not be so lucky with other people that will extend this blog platform [well, wishful thinking ... , but anyway... ]; they could do a for... in...Array and not know why they get more items. or the for each (var in Array).
There are a few ways out of this:
- you could hack the moootools lib and remove what they add to the global prototypes.
- you could hack the mootools lib and do a defineProperty with enumerable property set to false.
- you could leave it the way it is and advertise to the users of the platform it uses Mootools, and they are aware of it - that is what i chose to do for now.
Secondly, ringojs maps requests to actions. I am using a feature of this mapping shown here: http://ringojs.org/wiki/Tutorial#urlmatching_argumentscapture where anything after /post/ is passed as a second argument to the function that handles the request.
unfortunately, the function in my case is presenting no arguments at all, as it is an inherited method mootools way.
here is in detail the problem:
at https://github.com/ringo/ringojs/blob/master/modules/ringo/webapp.js#L167 action is the function the handles the request. a check is done for the number of the parameters expected by the function. if a url comes like this: http://myserver.com/post/id and hits a function with only 1 parameter:
function(req){}
then ringojs gives you a 404 not found.
now, my Action is PostAction mootools class, and its process method is called when handlind the /post/id request.
but as you see here:
https://github.com/jgabios/ringopress/blob/mootoolish/WEB-INF/app/actions/PostAction.js#L9 it extends RingoPressAction, which in turn extends Action , and that's where i have process(req,url) method.
it does present 2 arguments in Action.js, but PostAction.process doesn't have them, its length returns 0.
it's a mootool thing.
so, i hacked into webapp.js and removed that IF where the check for action.length is done.
Other than that, everything is fine.
In ringojs examples i had the functions handling requsts presented in action.js module and exported. with mootools i managed to have them in separate files and inside classes.
huge benefits: instead of :
return Response.skin('skinname',context)
now i only specify the skinname in initialize method of the mootools class and that's it.
listing posts in the IndexAction and IndexAdminAction reuses lots of code, like getting the posts from db, managing pagination, etc...
More things can be taken away from Actions, like making a biz layer [yeah, the java way of solving problems: add 1 more layer of abstraction] that manages the ValueObjects, and a DAO layer, but that in the future.
for the moment i do not like the way i export the actions:
actually it is an instantioation of a mootools class, then a call its process method, then bind it to the object.
and I do it for every Action. sure it can be done somehow automatically in a general way.
Ringopress now has the master branch on github without mootools, and a branch here: https://github.com/jgabios/ringopress/blob/mootoolish/ with mootools in it. The mootoolish branch will get developed now, and if all gets well, it will make it into the master.