2010-12-02 159 views
2

我使用jQuery data()来存储元数据,但似乎jQuery 1.4.4和1.4.3都有问题。有些部件可以工作,其他部件不起作用。jQuery data()返回undefined

举例来说,我有

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 { 
... 

} 

其中元素是DOM元素。然后,我得到我存储的值如下,

var height = this.ancestor.data(UimConst.HEIGHT); 
logger.debug("Current UI module height " + height); 

不幸的是,返回值是未定义的。

为了进一步追查问题,我改变了代码是

if(this.ancestor == null){ 
    this.ancestor = $(element); 
    this.ancestor.data(UimConst.HEIGHT, 0); 
    logger.debug("After set the ancestor height, the value is " + this.ancestor.data(UimConst.HEIGHT)); 
} else { 

日志中的返回值是“不确定”的可能。真的很沮丧。

我在一些其他地方使用数据(),他们工作得很好。不知道发生了什么事。 任何提示?

该项目是在这里如果有人想看一看:

http://aost.googlecode.com/svn/trunk/tools/tellurium-ide

只是不要颠覆检查出并运行以下命令:

mvn install 

,然后将生成的.xpi文件安装到Firefox。

之后,您可以打开Tellurium IDE Firefox插件和JavaScript调试器Firefox插件来跟踪执行情况。

对于此问题,请转到workspace.js并在findAncestor()方法的开头设置breakpointer。

约碲IDE的更多细节在这里:

http://code.google.com/p/aost/wiki/TelluriumIde080RC1

由于提前,

约翰

+0

您确定调用了`Workspace.prototype.findAncestor`吗?也许尝试添加一些输出,证明它在之后立即被设置:`console.log('data has been set:'+ this.ancestor.data(UimConst.HEIGHT));` – clarkf 2010-12-02 20:16:43

+0

是的。它被称为。我使用JavaScript调试器来追踪执行流程。 – 2010-12-02 20:18:09

回答

0

粘贴代码在jslint会有所帮助。你可以在那里得到很多好的提示。

const声明不适用于每个浏览器,因此将其更改为var是一个提示。

但是没有足够的代码来做很多事情。

查看Javascript module design pattern上的这些提示,以便您进一步寻求帮助。

+0

那么,我工作在一个Firefox插件上,并且有很多文件,并且很难将它们剪切并粘贴到jslint。但是,我确实运行了jslint Maven插件,即使对于jQuery库也提供了太多警告。不太有用。 – 2010-12-02 20:37:27

2

如果elementnull或者与文档中的任何元素不匹配的字符串,则会发生这种情况。

$(element).data(key, value)如果$(element).length == 0什么都不做。

jQuery也默默拒绝存储某些元素的数据(embed,applet和任何object除了Flash),但这似乎不是你的问题。

更新:如果这是试图对网页中的元素进行操作的Firefox附加代码,那么这对我来说并不是什么大惊喜。使用附加组件时,元素的行为有点不同,所以在常规网页中工作良好的库(如jQuery)无法在附加组件中使用。

更新2:我的新建议是使用jQuery的开发版本,如http://code.jquery.com/jquery-latest.js,并逐步调试器中的数据方法。有趣的部分发生在jQuery.data中,它长度超过40行:

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; 
    } 

    // Get the data from the object directly 
    if (!isNode) { 
     cache = elem; 

    // Compute a unique ID for the element 
    } else if (!id) { 
     elem[ jQuery.expando ] = id = ++jQuery.uuid; 
    } 

    // Avoid generating a new cache unless none exists and we 
    // want to 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 the named cache with undefined values 
    if (data !== undefined) { 
     thisCache[ name ] = data; 
    } 

    return typeof name === "string" ? thisCache[ name ] : thisCache; 
},