T
T
TEHEK2011-02-05 00:01:57
JavaScript
TEHEK, 2011-02-05 00:01:57

Interaction between closures in JavaScript?

There are closures:

(function () {
   // do something
})()


(function () {

   // do another thing

}) ()


How would you organize the interaction between objects in these closures?

Actually, the question arises when using jQuery in large projects ... here is, for example, a dialog box on jQuery UI. But this seems to be a special case of a general problem.

It usually looks like this for me:
<div id="dialog">
   <p>something</p>
</div>

<button id="show_dialog_btn">Show Dialog</button>

<script>
jQuery(function ($) {
    $("#dialog").dialog({
         куча параметров,
         ну просто тьма всяких настроек
         и разные колбэки,
         buttons: {
               OK: function () {
                     // что-нибудь родить....
               }
         }
    });

    $("#show_dialog_btn").click(function () {
         $("#dialog").dialog('open');
    });
});
</script>


The dialog box in this case actually consists of HTML markup and Javascript code. For simplicity, we omit the CSS. Now, in order to create the same window on another page, you need to either copy it or ... And here the difficulties begin ... If the window parameters depend on any parameters in the current closure, Let's say it is by pressing the OK button, the "local" for the closure should be called function. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
homm, 2011-02-05
@homm

data , bind and triggerHandler will help you . Well, here's my post :)

A
Anatoly, 2011-02-05
@taliban

The simplest option:

var common = {
    param1: 'value1',
    param2: 'value2',
    param3: 'value3',
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question