Sunday, October 18, 2009

Learning Jquery and rehashing javascript..

Been learning jquery and messing around with hiding things dynamically in the django-admin...


indexOf doesn't work in IE
so basically the work around is to apply it to ie's version of js..

if (!Array.indexOf) {
Array.prototype.indexOf = function (obj, start) {
for (var i = (start || 0); i < this.length; i++) {
if (this[i] == obj) {
return i;
}
}
}
}


There is no string title case function so again, put one in yourself..

// (c)GPL, apv
String.noLC = new Object
({the:1, a:1, an:1, and:1, or:1, but:1, aboard:1,
about:1, above:1, across:1, after:1, against:1,
along:1, amid:1, among:1, around:1, as:1, at:1,
before:1, behind:1, below:1, beneath:1, beside:1,
besides:1, between:1, beyond:1, but:1, by:1, 'for':1,
from:1, 'in':1, inside:1, into:1, like:1, minus:1,
near:1, of:1, off:1, on:1, onto:1, opposite:1,
outside:1, over:1, past:1, per:1, plus:1,
regarding:1, since:1, than:1, through:1, to:1,
toward:1, towards:1, under:1, underneath:1, unlike:1,
until:1, up:1, upon:1, versus:1, via:1, 'with':1,
within:1, without:1});

String.prototype.titleCase = function () {
var parts = this.split(' ');
if ( parts.length == 0 ) return '';

var fixed = new Array();
for ( var i in parts ) {
var fix = '';
if ( String.noLC[parts[i]] )
{
fix = parts[i].toLowerCase();
}
else if ( parts[i].match(/^([A-Z]\.)+$/i) )
{ // will mess up "i.e." and like
fix = parts[i].toUpperCase();
}
else if ( parts[i].match(/^[^aeiouy]+$/i) )
{ // voweless words are almost always acronyms
fix = parts[i].toUpperCase();
}
else
{
fix = parts[i].substr(0,1).toUpperCase() +
parts[i].substr(1,parts[i].length);
}
fixed.push(fix);
}
fixed[0] = fixed[0].substr(0,1).toUpperCase() +
fixed[0].substr(1,fixed[0].length);
return fixed.join(' ');
}
Javascripts objects work as associative arrays
var theStatus = new Object;
theStatus.Home = 'normal';
theStatus['Home'] //will print 'normal'

Arrays are useful as in all languages, but their function set isnt really all that complete and require some inelegant code to remove elements properly. (Mainly just
var arr = [4,5,6];
arr.splice(arr.indexOf(5), 1);
Also, to copy arrays by value, you have to use the splice() fn...

There's no elegant way to copy objects by value, so again.. add it in yourself.
Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};
Jquery doesn't have a proper way to filter with regexes... so.... yeah... But even that needs to be modified since it only searches on the text() field of an object.
jQuery.extend(
jQuery.expr[':'], {
regex: function(a, i, m, r) {
var r = new RegExp(m[3], 'i');
//Add modifications here....
return r.test(jQuery(a).text());
}
}
);

But a way to get around the lack of regex in some cases is to remember that you can chain [attributeFilters]..
$("input[id][name$='man']").val("only this one");

And finally(for now), you can add attributes to objects with attr.
$('#target').attr("disabled", true); 




I still don't like javascript, but jquery makes working with it a lot nicer.. sorta.

No comments:

Post a Comment