Domanda

Sto cercando di creare una generica finestra di dialogo di conferma che può essere utilizzato da più widget facilmente, ma io sono in esecuzione in problemi con i ambito di applicazione e speravo in un chiaro modo di fare quello che sto cercando di fare.

Attualmente ho la seguente -

(function() {

    var global = this;
    global.confirmationBox = function() {
    config = {
        container: '<div>',
        message:''
    }
    return {
        config: config,
        render: function(caller) {
            var jqContainer = $(config.container);
            jqContainer.append(config.message);
            jqContainer.dialog({
                buttons: {
                    'Confirm': caller.confirm_action,
                     Cancel: caller.cancel_action
                }
            });
        }
    }
} //end confirmationBox
global.testWidget = function() {
    return {
        create_message: function(msg) {
            var msg = confirmationBox();
            msg.message = msg;
            msg.render(this);
        },
        confirm_action: function() {
            //Do approved actions here and close the confirmation box
            //Currently not sure how to get the confirmation box at
            //this point
        },
        cancel_action: function() {
            //Close the confirmation box and register that action was 
            //cancelled with the widget. Like above, not sure how to get
            //the confirmation box  back to close it
        }
    }
}//end testWidget
})();
//Create the widget and pop up a test message
var widget = testWidget();
widget.create_message('You need to confirm this action to continue');

Attualmente sto solo cercando di fare qualcosa di semplice come chiudere la finestra dall'interno del widget, ma credo di aver avvolto il mio cervello in cerchi, in termini di ciò che sa cosa.Nessuno vuole aiutare a chiarire il mio cervello confuso?

Ciao Sam

Il codice risultante:

Ho pensato che potrebbe essere utile per le persone che trovano questo thread nei giorni successivi alla ricerca di una soluzione ad un problema simile per vedere il codice che hanno portato le utili risposte che ho avuto qui.

A quanto pare è stato abbastanza semplice, alla fine (come la maggior parte dei frustrante mente-grovigli sono).

 /**
 * Confirmation boxes are used to confirm a request by a user such as
 * wanting to delete an item
 */
 global.confirmationBox = function() {
    self = this;
    config = {
        container: '<div>',
        message: '', 
    }
    return {
        set_config:config,
        render_message: function(caller) {
            var jqContainer = $(config.container);
            jqContainer.attr('id', 'confirmation-dialog');
            jqContainer.append(config.message);
            jqContainer.dialog({
               buttons: {
                   'Confirm': function() {
                       caller.confirm_action(this);
                    },
                   Cancel: function() {
                       caller.cancel_action(this);
                   }
               }
           });
        }
    }
 } // end confirmationBox

 global.testWidget = function() {
    return {
        create_message: function(msg) {
            var msg = confirmationBox();
            msg.message = msg;
            msg.render(this);
        },
        confirm_action: function(box) {
            alert('Success');
            $(box).dialog('close'); 
        },
        cancel_action: function(box) {
            alert('Cancelled');
            $(box).dialog('close'); 
        }
    }
}//end testWidget
È stato utile?

Soluzione

Si potrebbe passare jqContainer di confermare/annullare le funzioni.

In alternativa, assegnare jqContainer come una proprietà del chiamante.Dal momento che l'confermare/annullare le funzioni sono chiamati metodi del chiamante, si avrà accesso ad esso tramite this.Ma che si limita a tracciare una finestra di dialogo per widget.

Altri suggerimenti

Provare qualcosa di simile a questo:

(function() {

    var global = this;
    /*****************This is new****************/
    var jqContainer;


    global.confirmationBox = function() {
    config = {
        container: '<div>',
        message:''
    }
    return {
        config: config,
        render: function(caller) { 

            // store the container in the outer objects scope instead!!!!
            jqContainer = $(config.container);

            jqContainer.append(config.message);
            jqContainer.dialog({
                buttons: {
                    'Confirm': caller.confirm_action,
                     Cancel: caller.cancel_action
                }
            });
        }
    }
} //end confirmationBox
global.testWidget = function() {
    return {
        create_message: function(msg) {
            var msg = confirmationBox();
            msg.message = msg;
            msg.render(this);
        },
        confirm_action: function() {
            //Do approved actions here and close the confirmation box
            //Currently not sure how to get the confirmation box at this point

            /*******Hopefully, you would have access to jqContainer here now *****/

        },
        cancel_action: function() {
            //Close the confirmation box and register that action was 
            //cancelled with the widget. Like above, not sure how to get
            //the confirmation box  back to close it
        }
    }
}//end testWidget
})();
//Create the widget and pop up a test message
var widget = testWidget();
widget.create_message('You need to confirm this action to continue');

Se questo non funziona, provare a definire un sistema di callback (confirm_action, cancel_action) come membri privati dell'oggetto.Ma si dovrebbe essere in grado di accedere all'esterno ambito dell'oggetto principale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top