views:

1333

answers:

2

is it possible to add a new property to a previously declared object? Here is some code to clarify (with the uploadify jquery plugin):

$('#featuredimageupload').uploadify({
        'uploader' : base_url + 'media/js/uploadify.swf',
        'script': base_url + 'post/uploadflash',
        'multi' : true,
        'queueID' : 'queue',
        'cancelImg' : base_url + '/media/images/cancel.png',
        'fileDesc' : 'Allowed Types: (*.jpg,*.png,*.gif)',
        'fileExt' : '*.jpg;*.JPG;*.gif;*.GIF;*.png;*.PNG',
        'queueSizeLimit' : 9,
        'sizeLimit': 1000000,
        'method' : 'GET',
        'buttonText' : 'Browse',
        'onComplete' : function(event, queue, obj, response, data){
            if(response =='false'){
                alert('Maximum number of images reached!');
                $('#uploader').uploadifyClearQueue();
                return false;
            }
        },
        'onError' : function(event,queue,file,error){
            alert('An error occured. No files were uploaded');
            $('#uploader').uploadifyClearQueue();
        }
    });

then do something like

$('#form').submit(function(){
   $('#featuredimageupload').uploadify({scripData : data})
})

I saw this done with jqgrid

+1  A: 

Yes it is possible to add properties (dynamic or otherwise) to an existing Javascript object). You can just use the [] operator to add a dynamic property to an object. For example:

var key = 'foo';
var obj = {param: 'value'};
obj[key] = 'bar';
alert(obj.foo); // bar
cletus
@Doug The information that @cletus provided is correct albeit general. IMHO, answers should be voted down when they are erroneous, off topic, self-promoting, or promote bad practices. +1 to offset ridiculousness and other absurdities. @cletus on a separate note, in addition to bracket notation, dot notation can also be used to create new members on an object.
Justin Johnson
If theres a more appropriate answer, by all means vote it up. But it wasn't (and still isn't) clear to me from the OP's question whether the issue was the uploadify() in particular or dynamic properties in general (or both). If the issue is the uploadify() script then it badly needs rewording. Downvoting something that arguably answers the question is (imho) a grey area.
cletus
The seeming clarity for me must have come from my having used uploadify before, and knowing that there is a function to update `scriptData` (which is misspelled in the question). Without that background, the question would be unclear, and I didn't look at it that way when I downvoted your answer. There were no ulterior motives, I promise :)
Doug Neiner
@Doug If it were all about accuracy, then all answer suggesting different approaches, even when the OP is following a bad practice, should be downvoted. IMHO, providing general or additional information shouldn't be penalized. While your answer is a better answer, it doesn't make @cletus' a bad one.
Justin Johnson
+4  A: 

I believe what you are looking for is this:

$("#form").submit(function(){
    var $upload = $("#featuredimageupload");
    $upload.uploadifySettings('scriptData', { key : value });
    $upload.uploadifyUpload(); // Triggers the upload to start.
});

You can also get the current value by leaving the second parameter off:

var currentSetting = $upload.uploadifySettings('scriptData');
Doug Neiner
The above debate notwithstanding, +1 for the record ;)
Justin Johnson
@Justin LOL, oh well. Still learning when to downvote and when to answer and move on ;)
Doug Neiner