1

I have created a modal plugin that has options defined by default or the user. The majority of these values will be passed to a function and updated, when this happens, I want the value to be updated in the options. Ideally I don't want to have to update each individual one, and want to handle and update the object (options) as a whole.

the following is the users options:

modalDialog({ ID: 'before_unload', title: 'opt.heading', message: 'opt.text', confirm: 'opt.confirm', cancel: 'opt.cancel', link: $(this).attr('href'), }); 

Each of the options beginning with 'opt.' will have the function performed on them. Here is the code for the modalDialog plugin that deals with the options

var options = $.extend(defaults, options); return this.each(function() { var o = options; var reg = '^opt.*'; //regex to find all 'opt.' values console.log(o); $.each(o, function(i, v) { //for each value if(i != 'save'){ if(v.match(reg)){ // match to regex console.log(i+': '+v); var newval = rc.getResource(v); // function to update value console.log(newval); //value has been updated, function works v = i.replace(i, newval); // update the index with new value console.log('New i:v = '+i+': '+v); // checked and it works //Now the question is, how do I update o (options with the new values?) } }; }); 

In the code above, lets pretend that rc.getResource(v) come back with a different farm animal name depending on which value gets passed to it, I update the value with my new farm animal name, and check that it all matches up and everything is working up until I get to updating the options. I have tried o = options.replace(i,v) and it come back with options.replace is not a function.

One thought I did have was after I have created a new value (performed my function) unset/delete the corresponding i:v and add the new value to the object, e.g.

i1 = i; v1 = i.replace(i, newval); o -= i:v; o += i1:iv; 

Alas, my brain is frazzled and am in desperate need of help, which would be greatly appreciated!

1 Answer 1

4

Can't you just do this?:

o[i] = v; 
Sign up to request clarification or add additional context in comments.

2 Comments

I love you Scott, will you marry me?
Well, my dowry is a measly 9 characters of code... and two of them are spaces! Glad I could help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.