[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Identical functions in multiple loaded scripts - choosing which version is called

I've got a single page for editing various sets of data; the user chooses which set to work with from a menu, and the page loads the list of stuff & textboxes, etc, into a container div on that page via xmlhttp. There is one universal "save" button that simply calls "save()".

Each editing module has its own .js file with native functions and a "save()" function defined. Since the content & scripts are loaded dynamically, if the user chooses 3 or 4 different options, each of those .js files gets loaded and the "save" functions keep getting overwritten. Since javascript uses the last defined version of a function this causes problems if they go back to a previously loaded module.

So basically my question is, how can I reload or reposition scripts in such a way that an already-loaded script's function becomes the "last defined" version of that function?

Here's a pseudocode example:

function loadScript(filename)
{
  if(document.scripts[filename]) return, else document.scripts += new script(filename)
}

function onMenuChange(newModule)
{
 loadScript(newModule)
 loadContent(newModule)
}
since each module has its own "save" function, this happens

onMenuChange(moduleA)
onMenuchange(moduleB)
onMenuChange(moduleA) // calls the "Save" function from moduleB.js
0
noamattd
Asked:
noamattd
  • 3
  • 2
1 Solution
 
Jens FiedererCommented:
One solution is NOT to use globally scoped save functions.

If you make the Save function a method of an object (or methods of several objects), then multiple Save functions
can coexist.

You can then call either

moduleAobject.Save()
or
moduleBobject.Save()

depending on which is appropriate.
0
 
noamattdAuthor Commented:
I thought about that.  I'm doing this with .NET and each module is a composite server control, as is the universal save button (and other buttons with similar features); the goal being to generate the universal buttons once, and not have to mess with them any more.

If I were to have save be the method of different objects I would have to change the event handlers for each button every time a new module was loaded, and I was hoping to avoid that.
0
 
Jens FiedererCommented:
You could have the event handlers call a function that uses a global object.  This global object would contain whatever you want to be the "last defined" object, and the handler could call g_functions.Save()
0
 
noamattdAuthor Commented:
That sounds more like what I had in mind, could you give an example?
0
 
noamattdAuthor Commented:
Never mind, I got it.  Thanks a bunch.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now