jQuery data() returns undefined -
i use jquery data() store meta data, seems jquery 1.4.4 , 1.4.3 both have problems. parts work , other parts not work.
for instance, have
const uimconst = { node_object: "nodeobject", children: "children", parent: "parent", sid: "sid", count: "count", exclude: "exclude", parent_count: "pcount", height: "uimheight" }; workspace.prototype.findancestor = function(element){ if(this.ancestor == null){ this.ancestor = $(element); this.ancestor.data(uimconst.height, 0); } else { ... }
where element dom element. then, value stored follows,
var height = this.ancestor.data(uimconst.height); logger.debug("current ui module height " + height);
unfortunately, return value undefined.
to further trace problem, changed code be
if(this.ancestor == null){ this.ancestor = $(element); this.ancestor.data(uimconst.height, 0); logger.debug("after set ancestor height, value " + this.ancestor.data(uimconst.height)); } else {
the return value in log "undefined" well. frustrated.
i used data() in other places , worked fine. not sure happened. hints?
the project here if wants take look:
http://aost.googlecode.com/svn/trunk/tools/tellurium-ide.
just subversion check out , run following command:
mvn install
and install generated .xpi file firefox.
after that, can open tellurium ide firefox plugin , javascript debugger firefox plugin track execution.
for problem, go workspace.js , set breakpointer @ beginning of findancestor() method.
more details tellurium ide here:
http://code.google.com/p/aost/wiki/telluriumide080rc1
thanks in advance,
john
this happen if element
null
, or string did not match elements in document.
$(element).data(key, value)
nothing if $(element).length == 0
.
jquery silently refuses store data on elements (embed
, applet
, , object
except flash), doesn't seem problem.
update: if firefox add-on code trying operate on elements in web page, it's no big surprise me doesn't work. elements behave bit differently when used add-on, libraries (like jquery) work great in normal web pages can fail work in add-ons.
update 2: new advice use development version of jquery, such http://code.jquery.com/jquery-latest.js , , step through data method in debugger. interesting parts happen in jquery.data, on 40 lines long:
data: function( elem, name, data ) { [...] var isnode = elem.nodetype, id = isnode ? elem[ jquery.expando ] : null, cache = jquery.cache, thiscache; if ( isnode && !id && typeof name === "string" && data === undefined ) { return; } // data object directly if ( !isnode ) { cache = elem; // compute unique id element } else if ( !id ) { elem[ jquery.expando ] = id = ++jquery.uuid; } // avoid generating new cache unless none exists , // want manipulate it. if ( typeof name === "object" ) { if ( isnode ) { cache[ id ] = jquery.extend(cache[ id ], name); } else { jquery.extend( cache, name ); } } else if ( isnode && !cache[ id ] ) { cache[ id ] = {}; } thiscache = isnode ? cache[ id ] : cache; // prevent overriding named cache undefined values if ( data !== undefined ) { thiscache[ name ] = data; } return typeof name === "string" ? thiscache[ name ] : thiscache; },
Comments
Post a Comment