Javascript传递函数作为对象影响范围

时间:2014-11-10 17:02:56

标签: javascript jquery dojo scope

我将函数作为参数传递,除非对象在全局范围内,否则我将丢失某些对象的范围。我想从全局范围中删除该对象。

这里aliasGrid在全球范围内我想不要全球化。但是,如果我 删除其全局范围我无法在deleteAliasName()函数下访问它。

全球范围声明

aliasGrid = new AliasGrid();
aliasGrid.initialize();

问题出在showConfirmationDialog('deleteAliasName', 'noResponseConfirmationDialog',aliasNotificationLabel);我将两个函数的名称作为字符串传递,然后当' '和' '单击确认对话框上的按钮。

这样可以正常工作,但是我希望deleteAliasName()是AliasGrid函数的一个属性,并在showConfirmationDialog('deleteAliasName', 'noResponseConfirmationDialog',aliasNotificationLabel);上作为参数传递,并在“是”的情况下执行。单击ComfirmationDialog按钮。

AliasGrid.js

function AliasGrid() {
    var aliasData= [];
    var aliasNotificationLabel = 'Alias Name';  
  return {   
    load: function(){
        //create grid
    },
    add: function(){
        //adds record to grid
    },

    getAliasData: function () {

      return this.aliasData;
    },
    delete: function () {
      try {
        var aliasNameGrid = dijit.byId('aliasNameGrid');
        var deleteAliasNameSelection = aliasNameGrid.selection;
        var deleteAliasNameCount = 0;

        for (var i in deleteAliasNameSelection) {
          deleteAliasNameCount++;        }

        if (deleteAliasNameCount > 0) {                
          showConfirmationDialog('deleteAliasName', 'noResponseConfirmationDialog',aliasNotificationLabel);         
        } else {
          showNotificationDialog('okResponseNotificationDialog', 'Select Record(s) For Deletion !', aliasNotificationLabel);
        }
      } catch (error) {
        showNotificationDialog('okResponseNotificationDialog', error, aliasNotificationLabel);
      }
    }
  };
};

确认对话

function showConfirmationDialog(yesFunction, noFunction, title) {
  var confirmationDialog = new dijit.Dialog({
    id: 'deleteGridRecordConfirmationId',
    title: "<img src='images/niblogo.png' alt='National Insurance Board'class='loginDialogLogo'/>" + title,
    content: '<table style= "width: 300px;">' + '<tr>' + '<th style="text-align:center; padding: 5px" colspan="2"><label> Are Your Sure ? </label></th>' + '</tr>' +
      '</table>' +
      '<div class="dijitDialogPaneActionBar" style="text-align:center;"><button id= yesBtnId onclick=' + yesFunction + ' >Yes</button><button id=noBtnId onclick=' + noFunction + '("deleteGridRecordConfirmationId")>No</button></div>'
  });

  confirmationDialog.show();
}

DeleteAliasName

function deleteAliasName() {
  try {

    var aliasData = aliasGrid.getAliasData();

    var aliasStore = new dojo.store.Observable(new dojo.store.Memory({
      data: aliasData,
      idProperty: "id"
    }));

    var grid = dijit.byId('aliasNameGrid');

    aliasStore.query({}).forEach(function (AliasName) {

      if (grid.selection[AliasName.id]) {

        aliasStore.remove(AliasName.id);
      }

    });

1 个答案:

答案 0 :(得分:1)

function showConfirmationDialog(yesFunction, noFunction, title) {
    var confirmationDialog = new dijit.Dialog({
        id: 'deleteGridRecordConfirmationId',
        title: "<img src='images/niblogo.png' alt='National Insurance Board'class='loginDialogLogo'/>" + title,
        content: '<table style= "width: 300px;">' + '<tr>' + '<th style="text-align:center; padding: 5px" colspan="2"><label> Are Your Sure ? </label></th>' + '</tr>' +
        '</table>' +
        '<div class="dijitDialogPaneActionBar" style="text-align:center;"><button id="yesBtnId">Yes</button><button id="noBtnId">No</button></div>'
    });

    var dijitDialog = $("div.dijitDialogPaneActionBar");
    dijitDialog.children("button#yesBtnId").click(yesFunction);
    dijitDialog.children("button#noBtnId").click(function()
    {
        noFunction("deleteGridRecordConfirmationId");    
    });
    confirmationDialog.show();
}

<强> AliasGrid:

function AliasGrid()
{
    var aliasData= [];
    var aliasNotificationLabel = 'Alias Name';
    var self = this;

    self.deleteAliasName = function() {
        try {

            var aliasData = self.getAliasData();

            var aliasStore = new dojo.store.Observable(new dojo.store.Memory({
                data: aliasData,
                idProperty: "id"
            }));

            var grid = dijit.byId('aliasNameGrid');

            aliasStore.query({}).forEach(function (AliasName) {

                if (grid.selection[AliasName.id]) {

                    aliasStore.remove(AliasName.id);
                }

            });
        }
    };

    return {
        load: function(){
            //create grid
        },
        add: function(){
            //adds record to grid
        },
        delete: function () {
            try {
                var aliasNameGrid = dijit.byId('aliasNameGrid');
                var deleteAliasNameSelection = aliasNameGrid.selection;
                var deleteAliasNameCount = 0;

                for (var i in deleteAliasNameSelection) {
                    deleteAliasNameCount++;        }

                if (deleteAliasNameCount > 0) {
                    showConfirmationDialog(self.deleteAliasName, 'noResponseConfirmationDialog',aliasNotificationLabel);
                } else {
                    showNotificationDialog(okResponseNotificationDialog, 'Select Record(s) For Deletion !', aliasNotificationLabel);
                }
            } catch (error) {
                showNotificationDialog(okResponseNotificationDialog, error, aliasNotificationLabel);
            }
        }
    };
};