Just chatting with nate about a few of his recent JSAN and Behaviour posts…his site was down when I tried to comment on them…so I’ll post the chat session here.
James: cool.
did you read this?
http://openjsan.org/doc/c/cw/cwest/JSAN/0.10/lib/JSAN.html
esp the section on namespaces.
nminshew: yes, that is where I got my examples and such.
James: I take from that page that you need to define and explicitly add to the namespace (so a use(’xml.Blah’) wont do anything if xml/Blah.js doesn’t actually have a blah class in an xml namespace.
also about the Behaviour thing…i think you need to be careful about how you’ve done it…I would actually consider it flawed…just by chance that it works. what you’ve told it to register is a hash that IS the function…when behaviour iterates over that hash it is adding rules for the items you’ve defined inside the function…but also properties of the function itself. You’d be much better off by defining the hash directly (as the docs do).
nminshew: Yeah, I guess it wasn’t exactly as I thought it was. That kind of sucks actually, because if you have a complex heirarchy, that can get pretty complicated.
I got that [Behaviour] example straight from the docs. I used their example when I wrote my first app.
James: I’ve never seen that example anywhere. send me a link.
nminshew: Well their site has moved and changed since I first went there, I don’t know if I can find it again, but I’ll try.
I pretty much copied and pasted it though. the window.onload and everything.
Sent at 8:54 AM on Tuesday
nminshew: I can’t find it, but I didn’t come up with the function names and such and wouldn’t of thought to do it that way if it wasn’t for the example.
Also, what is you problem with putting the rules in a function and passing it to the register method?
James: anyway it is was a bad example. I’d change the code to do as it shows on the docs now.
nminshew: In my example, I should have had () after defineRules.
James: I don’t have a problem with that…but exactly…the way you had it confused me and that was the ‘does this work’ question came from…you were passing a reference to the function not calling the function and passing what was returned….thought it was a typo at first…but the function did not have a return statement.
To make it work such that you can (as you have already changed it to do) call the method is like so:
function defineRules() {
var rules = {
‘a.popup’ : function (element) {
var popupLocation = element.href;
element.onclick = function () {
window.open(popupLocation);
}
element.href = ‘javascript:void(0);’;
}
};
return rules;
}
OR
function defineRules() {
return {
‘a.popup’ : function (element) {
var popupLocation = element.href;
element.onclick = function () {
window.open(popupLocation);
}
element.href = ‘javascript:void(0);’;
}
};
}
nminshew: Yeah yeah, I know, that was another typo I just fixed.
Here’s an example of someone else doing it this way: http://javascript.about.com/library/blonload.htm
James: I’d really recommend that you re-consider the onload thing. a lot of scripts add onload events (in a non-destructive, portable way)….it really makes it easy for me to just take advantage of some lib or module that does so…I dont’ have to know that if i use a module, i’ll need to plug in some boiler plate onload event that will call it…it could get pretty hairy if i’ve got a lot of modules I’m using that have some work to do when the page loads.
that’s [the link] gonna blow away any registered onload events.
nminshew: Well if you do it statically, don’t you run the risk of it executing before all the elements are loaded on the page?
James: I”M not saying do it statically…I’m saying register an event handler (all browsers support what are effectively event listeners…just in different ways). I’m saying register a listener for the onload event on the window.
nminshew: do you have an example of the way you are talking about?
James: yes, i sent the link to you via email yesterday [http://www.scottandrew.com/weblog/articles/cbs-events]. Its what I’ve been using here.
from my code:
addEvent(window, ‘load’, initWidgets);
that adds an ‘onload’ listener to the window object…so when onload occurs, the initWidgets method is called.
(addEvent is the function written by the guy in that article)…actually I somehow found a version that doesn’t have the useCapture stuff…written by a different guy…looks mostly the same just minus that useCapture boolean
nminshew: k, thanks. I’ve just never known a better way of doing it.
James: which is why i thought you’d like to know.
nminshew: thanks.
do you have the example without the useCapture?
James: funny…it’s on the unobtrusive article:
http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html