2010-06-07 104 views
5

我正在使用uploadify並且更改設置的功能似乎不起作用。.uploadifySettings無法按預期工作

我從下面的例子中立足我的代碼:

#(‘#someID’).uploadifySettings(’scriptData’, {‘name’ : some.val()}); 

因此,這裏是我在做什麼:

// INITIALIZATION 
$("#"+elementId).uploadify({ 
    // other data 
    "scriptData": { 
    "token": token 
    } 
}); 

後來我想更新scriptData:

$("#"+elementId).uploadifySettings("scriptData",{"token": "pleasework"}); 

...但這不起作用,它仍然在初始化過程中使用scriptData集。

我在做什麼錯?


更新:我需要這樣做,因爲我需要處理令牌。這裏的worflow:

1- Get a token 
2- Init uploadify with this token 
3- Upload a file 
4- Get another token asynchronously 
5- Add the token to the already initialized uploadify (bugged) 
6- Go to 3 

我試圖做這樣的初始化:

"scriptData": { 
    "token": $(".token").val() 
} 

...和更新的步驟4

.token這不工作,要麼

UPDATE 2:另外如果我這樣做:

"scriptData": { 
    "token": getAToken() 
} 

function getAToken(){ 
    alert("abcd"); 
    return "sometoken"; 
} 

...我可以看到,只有getAToken函數被調用一次(僅1個警報)

+0

你有一個演示頁面向我們展示了什麼問題?這可能是您腳本另一部分的問題。 – nebkat 2010-06-10 09:54:44

+0

@neb:不,我很抱歉。這是在一個未公佈的項目的管理部分:\ – marcgg 2010-06-10 10:10:13

+0

@neb:但是除了這個問題,其餘的腳本完美地工作 – marcgg 2010-06-10 10:10:46

回答

1

我已經看了源和我注意到uploadifySettings()有一個可選的,無證(它沒有出現here)第三個參數。顯然,如果您將其設置爲true(如$("#"+elementId).uploadifySettings("scriptData",{"token": "pleasework"}, true);),它將會摧毀scriptData的現有設置,這可能會產生一些影響。

但基於來源,我不能準確地說出設置變化必然會產生什麼樣的影響。

uploadifySettings:function(settingName, settingValue, resetObject) { 
     var returnValue = false; 
     jQuery(this).each(function() { 
      if (settingName == 'scriptData' && settingValue != null) { 
       if (resetObject) { 
        var scriptData = settingValue; 
       } else { 
        var scriptData = jQuery.extend(settings.scriptData, settingValue); 
       } 
       var scriptDataString = ''; 
       for (var name in scriptData) { 
        scriptDataString += '&' + name + '=' + escape(scriptData[name]); 
       } 
       settingValue = scriptDataString.substr(1); 
      } 
      returnValue = document.getElementById(jQuery(this).attr('id') + 'Uploader').updateSettings(settingName, settingValue); 
     }); 

該代碼是從2.1.0版本。

有沒有辦法在初始化之前有可能決定設置?

此外,我發現這個存在的SO問題:Uploadify updateSettings problems

+0

不錯的發現,我今天會看更多這裏,並在這裏更新 – marcgg 2010-06-14 09:24:37

+0

@artlung:我試着設置resetObject trye,它並沒有改變任何東西。現在有了決定設置的方式,因爲他們之前 – marcgg 2010-06-14 14:59:46

+0

什麼設置你正在改變?什麼值改變? – artlung 2010-06-14 15:03:30

0

嘗試在初始化時在線定義函數嗎? IE:

"scriptData": { 
    "token": function() { return $("#token").val(); } 
} 

我不知道爲什麼這會比你的其他解決方案不同。

+0

感謝您的回答,我現在嘗試一下 – marcgg 2010-06-14 16:31:56

+0

如果我嘗試這樣做,它會嘗試將該函數作爲參數傳遞,但它不會實際執行它。服務器端我結束了一個漂亮的數據庫調用:SELECT COUNT(*)AS count_id FROM'tokens' WHERE('tokens'.'value' ='function(){\ n return el.find(\「。token \ 「).val(); \ n}') – marcgg 2010-06-14 16:38:40

+0

doh!也許uploadify無法處理。對不起,你的原始解決方案看起來像是直接從Uploadify的文檔中找出來的。我想不用說,你有最新版本,並且對uploadifysettings的調用正在正確執行......有時我討厭javascript :) – 2010-06-14 22:23:42

2

這對我的作品,加入了獨特的隨機數以每個文件上傳

  function setScriptData(){ 
       $("#product_attachment").uploadifySettings("scriptData", 
        { 
         '_fsg_distro_session' : '<%= u cookies["_fsg_distro_session"] %>', 
         'authenticity_token' : '<%= u form_authenticity_token if protect_against_forgery? %>', 
         'nonce'        : new Date().getTime() + "<%= @current_user.id %>"      
        } 
       ); 
       console.debug($("#product_attachment").uploadifySettings("scriptData")); 
      } 

      $("#product_attachment").uploadify({ 
       uploader     : '/uploadify/uploadify.swf', 
       script    : '/products/temp_upload', 
       cancelImg   : '/uploadify/cancel.png', 
       fileDataName  : 'asset[temp_file]', 
       'queueID'   : 'fileQueue', 
       onComplete    : function(event, ID, fileObj, response, data){ fileOnCompleteHandler(event,data); }, 
       onSelect     : setScriptData, 
       auto    : true, 
       multi    : false, 
       buttonImg   : '/images/attach.png', 
       fileNameMaxLength : 30 
      }); 

我使用的是最新的Uploadify(2.1.0)和JQuery(1.4.1)

+0

我試過了,它沒有工作。我使用jQuery 1.4.2,但我懷疑這是問題所在。我會在本週得到片刻時看看你的解決方案 – marcgg 2010-06-23 12:23:36

+0

@marcgg - 好奇你在做這件事時會得到什麼樣的錯誤 – Tony 2010-06-23 14:57:17

0

找到了解決辦法:

我在「onSelect」函數中有個錯誤。我拼錯元素ID名稱,而不是file_upload我有fileUpload,這就是爲什麼它沒有更新scriptData參數。我知道這是一個愚蠢的錯誤,但很容易做出來。這裏是整個ONSELECT功能:

'onSelect' : function(event,data) { 
      $("#file_upload").uploadifySettings('scriptData', {'id' : $('#name_of_the_element').val()} 
      ); 
     } 

因此,檢查,如果這是