2010-12-02 158 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; 
},