views:

308

answers:

3

I'm using uploadify and the function to change the settings doesn't seem to be working.

I'm basing my code from the following example:

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

So here's what I'm doing:

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

Later on I want to update the scriptData:

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

... but this is not working, it's still using the scriptData set during the initialization.

What am I doing wrong?


UPDATE: I need to do this because I need to handle tokens. Here's the 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

I tried doing this on initialization:

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

... and update .token on step 4

This doesn't work either

UPDATE 2: Also if I do:

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

with

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

... I can see that the function getAToken only gets called once (only 1 alert)

+1  A: 

I've looked at the source and I notice that uploadifySettings() has an optional, undocumented (it does not appear here) third parameter. Apparently if you set it to true as in $("#"+elementId).uploadifySettings("scriptData",{"token": "pleasework"}, true); it will clobber the existing settings for scriptData and perhaps that will have some impact.

But based on the source I can't exactly tell what impact a change in settings necessarily has.

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

That code is from version 2.1.0.

Is there a way to potentially decide on the settings before initialization?

Also, I found this existing SO question: Uploadify updateSettings problems.

artlung
nice find, I'll look more into this today and give an update here
marcgg
@artlung: I tried setting the resetObject to trye and it didn't change anything. And now there is now way of deciding the settings before since they will have to change.
marcgg
What setting exactly are you changing? What values change?
artlung
@artlung: In order to upload something I need to pass along a token. After the first upload, the token expires. There is a callback to get a new token once the image is uploaded, and I need to set the uploadify to pass this new token as a parameter (does it makes sense?)
marcgg
What if you set the setting to a function that refers to a function that returns a current token? so wherever the token is set instead of `{ 'myToken': 'B12345' }` you do `{ 'myToken' : currentMyToken() }` then all you need to do is to reset `currentMyToken()`'s value. Have it return the current token: `var currentMyToken = function() { return 'B12345'; };` {}
artlung
Then update it when you need to: `currentMyToken = function() { return 'X999999'; };`
artlung
@artlung: this might work, but since I can have multiple uploaders on the same page and want to keep a generic way of creating them this might cause problems. I'll try something like that right now, but I'd still prefer to have uploadifysettings() working ^^
marcgg
@artlung: I tried and it failed :\ I updated my question to add more info about what I tried
marcgg
yet another update of my question
marcgg
A: 

Try defining the function in-line at initialization? IE:

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

I'm not sure why this would be different than some of your other solutions though.

epalla
Thanks for the answer, I'll try that right now
marcgg
@epalla: If I try to do that, it tries to pass the function as a parameter, it doesn't actually execute it. Server side I end up with a pretty funky DB call: SELECT COUNT(*) AS count_id FROM `tokens` WHERE (`tokens`.`value` = 'function () {\n return el.find(\".token\").val();\n}')
marcgg
doh! Maybe uploadify can't handle that. Sorry man, your original solution looks like it's straight out of Uploadify's docs. I suppose it goes without saying that you've got the latest version and that the call to uploadifysettings is being executed correctly... Sometimes I hate javascript :)
epalla
@epalla that's sad
marcgg
A: 

This works for me, adding a unique nonce to every file upload

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

I am using the latest Uploadify (2.1.0) and JQuery (1.4.1)

Tony
@tony: I tried that and it didn't work. I'm using jquery 1.4.2 but I doubt it's the problem. I'll take a look at your solution when I get a moment this week
marcgg
@marcgg - curious what error you get when doing that
Tony