Here’s a trick I learned from Valerio (author of Mootools) on how to handle options for classes and functions. After refactoring a couple of legacy scripts I thought I’d share for those of you who might not know it already. This example below uses the Mootools syntax for class creation, but the rest of it can be used even on its own I think. Read the rest of this entry »
Archive for the ‘CNET JS Standards’ Category
this.options - setting defaults that can be overwritten in your classes
November 14th, 2006 by Aaron N.Bill Scott on Designing a Component
October 26th, 2006 by Aaron N.Bill Scott has a great article on designing a component that is worth a read. He talks about his rules of thumb when building something that should be reusable (and nearly everything should):
- Make the component specific in purpose, yet flexible in use.
Avoid the do-everything component. Instead make it do the main thing well. But make it pragmatic by keeping it flexible.
- Separate the concerns of interface, interaction, and content.
Avoid hard-coding visual style, give flexibility in the interaction model and provide flexible ways to manage data content. Keep these three areas independent to allow them to be customized separately.
- Document the interface and use the component before releasing it.
You know the experience. You think you have a great idea. Then when you go to explain it to another person you immediately see the holes in your logic. Documentation provides a way to explain your interface; writing demos allow you to exercise the component to test its ease of use and flexibility.
There’s a lot more detail and examples in the full article.
This kind of thinking has had a big influence on my work on CNET’s global framework and stumbling across this article just recently made me refocus my thoughts on the topic.
a.n.
Dean Edwards /packer/ compression tool
October 26th, 2006 by Aaron N.We’ve been working pretty hard on CNET’s new javascript global framework. It’s based on Mootools with a bunch of other things mixed in. Last week I took on the task of documenting the entire Mootools library, now online. I struck up a conversation with the author of that library, Valerio Proietti from mad4milk.net and have since been granted access to contribute to the source of that library (thanks Valerio!). Mostly my contributions have been focused on the docs, but I’ve also helped out with a few bugs and the like.
Anyway, one of the things that Mootools turned me on to was Dean Edward’s /packer/, which is an awesome javascript compression tool. This thing compresses our libraries dramatically, sometimes as much as halving their file size. We’ve actually put our new global libraries into production on news.com and I just got around to actually testing the efficiency of the compression algorythm. Read the rest of this entry »
jsDoc - a javadoc implementation for javascript
October 10th, 2006 by Aaron N.Ok, so I’ve finished as much digging on this subject as I plan on doing (mostly because the pickings seem to be rather slim). Here’s jsDoc - a javadoc implementation for javascript.
Comparing this library to NaturalDocs the choice seems to me to come down to two things: syntax and presentation. NaturalDocs gives you a really nice layout and a lot of flexibility in how you write your documentation inline, while jsDoc uses the same syntax as javadoc.
Does it matter if we use the same syntax as javadoc? Do we care if the output looks pretty and has a nicer interface? Which one? Spend a few minutes reading the documentation on each of these packages and sound off here, as the work on the global framework has gotten to the point where documentation is going to start to be the next thing that needs focus.
CNET Global Framework update
October 3rd, 2006 by Aaron N.Hi all,
It’s been a few days since I posted. I’ve been heads down on the project to create a global javascript framework for our sites (Redball, at least). I’ve finished this work (or at least gotten it to a releasable state) and I thought I’d share an update. I plan on documenting it extensively and providing a lot of examples (and teaching it) in the near future, but for those of you who are curious, you can peruse the library now. It’s mostly stable (we’re still tweaking and adding things) and you can just download them and read them if you want to see what’s in them.
They include a lot of functionality, are highly extendable, easily debuggable, and hopefully will be useful. The current framework file is about 30K, but Andy just turned me on to a more efficient javascript compressor that will bring that down to 19K. I can’t express how awesome it is to have this thing going out and it managing to pack in so much functionality in such a small package. Read the rest of this entry »
Mootools Ajax extension for handling errors on CNET
September 27th, 2006 by Aaron N.CNET always returns response code 200 for it’s pages, even when they are errors. This presents a problem for any ajax framework you might be using as your ajaxer will always think the page is returning 200, even if it’s not.
I wrote a quick extention for the Mootools framework as part of my forthcoming release of the CNET framework that handles this problem for you and adds some functionality to Mootools ajax class.
Ajax.implement({
responseIsSuccess: function(){
if(this.transport.status != undefined && (this.transport.status < 200 || this.transport.status >= 200)){
return false;
}
if(this.transport.responseText.indexOf("COMPONENT_RESPONSE_CODE=200") > 0)
return true;
return false
},
responseIsFailure: function(){
return !this.responseIsSuccess();
},
onStateChange: function(){
if (this.transport.readyState == 4 && this.responseIsSuccess()){
if (this.options.update) $(this.options.update).setHTML(this.transport.responseText);
this.options.onComplete.pass([this.transport.responseText, this.transport.responseXML], this).delay(20);
if (this.options.evalScripts) this.evalScripts.delay(30, this);
this.transport.onreadystatechange = Class.empty;
this.callChain();
} else if(this.responseIsFailure()) {
if($type(this.options.onFailure)=='function') this.options.onFailure();
}
}
});
This hasn’t been QAed yet, so I may need to tweak it.
It adds responseIsSuccess() and responseIsFailure(), and finally it adds as an excepted parameter to be passed in an onFailure function that will execute it if it fails. Example usage:
new ajax(url, {update: divIdToUpdate, method: 'get', parameters: ..., evalScripts:true, onFailure:errorHandlerFunction});
This will be available on every page on Redball when the new framework launches.
Rewrite of Behaviour.js in the CNET framework (mootools)
September 26th, 2006 by Aaron N.Huh. So this was eaiser than I thought.
In the Download.com Watch List profile page we use the Behaviour library to define a bunch of functionality (er, I mean “I use…” as I wrote all this over a year ago). We’re going to replace Prototype with our new framework (based mostly on Mootools). Behaviour will work in the environment, but it’s 8K that I don’t really want to keep around. So, what the hell, let’s try and rewrite it with Mootools. Well, here it is:
var BehaviourBaseClass = new Class({
initialize: function(){
this.behaviours = [];
var bhvr = this;
Event.onDOMReady(function(){bhvr.apply()});
},
register: function(actions){
if(! this.behaviours.test(actions))
this.behaviours.push(actions);
},
apply: function(actions) {
if ($type(actions)!='array') {
actions = this.behaviours;
}
actions.each(function(bhvrs){
for (bhvr in bhvrs){
try {
if($type(bhvrs[bhvr])=='function') {
$S(bhvr).each(function(el){
bhvrs[bhvr](el);
});
}
} catch(e){}
}
});
}
});
var Behaviour = new BehaviourBaseClass();
…which compresses down to 425 703 Bytes. Not bad.
Update
Ok, so I rushed to press a little. This code didn’t work when I put it into place. I fixed it, but that brought the file size up to 703 Bytes instead. Still, it’s a 10X reduction…
First look: CNET (Redball) Global Javascript Libraries
September 25th, 2006 by Aaron N.I’ve been working on a global library for cnet javascript standards and I’ve got my first crack at it ready. I’m going to spend some time in the coming weeks working on documentation and examples, but for those of you interested, here’s a quick look at what’s in the collection. Read the rest of this entry »
Getting crafty with javascript compression
September 25th, 2006 by Aaron N.So I’m working on a set of global javascript files for redball. I’ll post more on this when I’m finished (hopefully including documentation and examples, but that might take me a while), but here’s something I just whipped up that you might find useful.
I’m compressing all my javascript (so no extra spaces or comments) which means that debugging these things is going to be a pain. If you get an error on the live site and need to fix it, the debugging info you get out of your browser won’t be that helpful in fixing things. On top of that, I don’t want to have to edit a compressed file to debug it.
Previously, what I did was overwrote the compressed version with the non-compressed version to debug my stuff, but this is a pain if the code is on akami and you don’t want to do a bunch of work to change that.
So I wrote up a little script that goes along with my dbug console wrapper for firebug (note the update comment below the post for the most recent code). Here’s the function:
function dbugScripts(baseurl, libs){
if(window.location.href.indexOf("debug=true")>0){
for(i=0;i
");
}
return true;
}
return false;
}
Then, you go to your compressed libraries and you do this:
if(!dbugScripts("/the/location/of/my/scripts/",["script1.js","script2.js","etc"])){
...all my compressed javascript goes here...
}
So what’s this do? If you put “debug=true” into the query string of the page you’re viewing (and wish to debug), then all the dbug.log statments (again, see my dbug console wrapper function) will go to the console.log AND all your compressed javascript files will be ignored, with the document instead using the non-compressed versions. Voila, easy debugging.
Have fun.
MooTools followup
September 20th, 2006 by Aaron N.So I posted yesterday in my flurry of catch-up posts about the mad4milk.net guys new framework: MooTools. I’ve had a little time to dig into it and I must say that I’m blown away. In many ways, this is the framework that I’d say CNET should write for itself if it were to take on such a task. Read the rest of this entry »
