data attribute namespace normalization to comply with spec and jquery 1.6 changes

This commit is contained in:
John Bender 2011-05-20 00:31:27 -07:00
parent 2cff5c3380
commit 7eb1085764
2 changed files with 24 additions and 15 deletions

View file

@ -20,7 +20,7 @@
//hash segment before &ui-page= is used to make Ajax request
subPageUrlKey: "ui-page",
//anchor links with a data-rel, or pages with a data-role, that match these selectors will be untrackable in history
//anchor links with a data-rel, or pages with a data-role, that match these selectors will be untrackable in history
//(no change in URL, not bookmarkable)
nonHistorySelectors: "dialog",
@ -110,32 +110,41 @@
setTimeout(function() {
$.event.special.scrollstart.enabled = true;
}, 150 );
},
nsNormalize: function(prop){
if(!prop) return;
// NOTE the spec specifies that attributes will be converted to lower case
// ascii so the regex can remain simple
return ($.mobile.ns + prop).replace(/-([a-z])/g, function(s, capture){
return capture.toUpperCase();
});
}
});
//mobile version of data and removeData and hasData methods
//ensures all data is set and retrieved using jQuery Mobile's data namespace
$.fn.jqmData = function( prop, value ){
return this.data( prop ? $.mobile.ns + prop : prop, value );
return this.data( prop ? $.mobile.nsNormalize(prop) : prop, value );
};
$.jqmData = function( elem, prop, value ){
return $.data( elem, prop && $.mobile.ns + prop, value );
return $.data( elem, $.mobile.nsNormalize(prop), value );
};
$.fn.jqmRemoveData = function( prop ){
return this.removeData( $.mobile.ns + prop );
return this.removeData( $.mobile.nsNormalize(prop) );
};
$.jqmRemoveData = function( elem, prop ){
return $.removeData( elem, prop && $.mobile.ns + prop );
return $.removeData( elem, $.mobile.nsNormalize(prop) );
};
$.jqmHasData = function( elem, prop ){
return $.hasData( elem, prop && $.mobile.ns + prop );
return $.hasData( elem, $.mobile.nsNormalize(prop) );
};
// Monkey-patching Sizzle to filter the :jqmData selector
var oldFind = $.find;

View file

@ -41,15 +41,15 @@
same( $("body").jqmData("foo"), true, "getting data returns the right value" );
same( $("body").data($.mobile.ns + "foo"), true, "data was set using namespace" );
same( $("body").data($.mobile.nsNormalize("foo")), true, "data was set using namespace" );
same( $("body").jqmData("foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
same( $("body").jqmData(), { "nstest-foo": true}, "passing no arguments returns a hash with all set properties" );
same( $("body").jqmData(), { "nstestFoo": true}, "passing no arguments returns a hash with all set properties" );
same( $("body").jqmData(undefined), { "nstest-foo": true}, "passing a single undefined argument returns a hash with all set properties" );
same( $("body").jqmData(undefined), { "nstestFoo": true}, "passing a single undefined argument returns a hash with all set properties" );
same( $("body").jqmData(undefined, undefined), {"nstest-foo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
same( $("body").jqmData(undefined, undefined), {"nstestFoo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
same( $("body").jqmRemoveData("foo"), $("body"), "jqmRemoveData returns the element" );
@ -63,15 +63,15 @@
same( $.jqmData(document.body, "foo"), true, "getting data returns the right value" );
same( $.data(document.body, $.mobile.ns + "foo"), true, "data was set using namespace" );
same( $.data(document.body, $.mobile.nsNormalize("foo")), true, "data was set using namespace" );
same( $.jqmData(document.body, "foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
same( $.jqmData(document.body), { "nstest-foo": true}, "passing no arguments returns a hash with all set properties" );
same( $.jqmData(document.body), { "nstestFoo": true}, "passing no arguments returns a hash with all set properties" );
same( $.jqmData(document.body, undefined), { "nstest-foo": true}, "passing a single undefined argument returns a hash with all set properties" );
same( $.jqmData(document.body, undefined), { "nstestFoo": true}, "passing a single undefined argument returns a hash with all set properties" );
same( $.jqmData(document.body, undefined, undefined), {"nstest-foo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
same( $.jqmData(document.body, undefined, undefined), {"nstestFoo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
same( $.jqmRemoveData(document.body, "foo"), undefined, "jqmRemoveData returns the undefined value" );