简体   繁体   English

Javascript跟踪函数数据

[英]Javascript keeps track of functions data

I created this function: 我创建了这个功能:

window.showModal = function(args){
    var defaults = {
        type: 'success',
        title: 'Informazioni',
        info: 'Testo di informazioni',
        cancel: 'Annulla',
        nocancel: false,
        confirm: 'Ok',
        action: false,
        isAjax: false,
        goTo: false,
    }

    args = args || defaults;
    for (var opt in defaults) {
        if (defaults.hasOwnProperty(opt) && !args.hasOwnProperty(opt)) {
            args[opt] = defaults[opt];
        }
    }

    alert(args['action'])

    $('#modal_generic_cancel').show();

    $('#modal_generic_title').addClass('uk-text-'+args['type']).html(args['title']);
    $('#modal_generic_info').html(args['info']);
    $('#modal_generic_cancel').html(args['cancel']);
    $('#modal_generic_confirm').html(args['confirm']);

    if(args['nocancel']){
        $('#modal_generic_cancel').hide();
    }

    if(args['action'] == false){
        $('#modal_generic_confirm').addClass('uk-modal-close');
        $('#modal_generic_cancel').hide();
    }

    if(args['action'] && !args['isAjax']){
        $('#modal_generic_confirm').click(function(){
            window.location = base_url()+args['goTo'];
        });
    }
    if(args['action'] && args['isAjax']){
        $('#modal_generic_confirm').click(function(){
            alert(args['action'])
        });
    }

    UIkit.modal("#modal_generic").show();
}

For some reason when I call this function: 由于某些原因,当我调用此函数时:

showModal({type:'danger', title:'Errore', info: "Funzione modifica cliente non ancora implementata."});

The "action" is not defined, therefore is false . 未定义“操作”,因此为false If I click on the next button: 如果我单击下一步按钮:

showModal({type:'danger', title:'Eliminare il cliente?', info:'Siete sicuri di voler eliminare il cliente selezionato?', cancel:'No', confirm:'Si, elimina', action:'tables/deleteCustomer/'+customer_id, isAjax:true, goTo:'main/show/'+customer_id})

Than click back to the first one, action is set to: 与单击回到第一个相比, action设置为:

tables/deleteCustomer/38

Instead of taking the default{} value (false) it keeps the one of the other button. 而不是采用default{}值(false),而是保留另一个按钮中的一个。

Here a fiddle: https://jsfiddle.net/mrweb/x3fLq6o5/1/ 这里是一个小提琴: https : //jsfiddle.net/mrweb/x3fLq6o5/1/

Your defaults object is global. 您的defaults对象是全局对象。 To prevent creating global objects (plus other advantages) pay attention to the "use strict" mode. 为防止创建全局对象(以及其他优点),请注意“严格使用”模式。

As I see you are using jQuery then I promote the cloning objects technique by using the jQuery.extend() method. 如我所见,您使用的是jQuery,然后通过使用jQuery.extend()方法来推广克隆对象技术。

Also I promote the use of IIFE to create isolated scopes, where you can specify the context in which your methods are linked. 另外,我提倡使用IIFE创建隔离的作用域,您可以在其中指定方法链接的上下文。

Learn more about MODULE PATTERN 进一步了解MODULE PATTERN

//begin IIFE
(function (context) {

    "use strict";

    var defaults = {
        type: 'success',
        title: 'Informazioni',
        info: 'Testo di informazioni',
        cancel: 'Annulla',
        nocancel: false,
        confirm: 'Ok',
        action: false,
        isAjax: false,
        goTo: false
    };

    context.showModal = function (args) {
        //Merge the contents of two or more objects together into the first object;
        //that is, defaults and args into the options
        var options = $.extend({}, defaults, args);

        //TODO: use options as the new options passed to the function
        //e.g. options.isAjax; options.goTo, ...
    };

}(window));
//window is imported as the context object;
//you can switch the window object to your own namespace object.

//usage
window.showModal({/* options */});

Also take into account that every time you invoke the showModal method you are rebinding new handlers to the click event. 还应考虑到,每次调用showModal方法时,都会将新的处理程序重新绑定click事件。 To prevent that, you can detach existing event handlers by calling the .off() method, eg 为了避免这种情况,您可以通过调用.off()方法来分离现有的事件处理程序,例如

$('#modal_generic_confirm').off("click.myns").on("click.myns", eventHandler);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM