Accessing a JavaScript object outside of its containing JavaScript file within a Dojo

I am working on a Dojo application that includes a Dgrid. My objective is to send the javascript containing the dgrid data to the server. However, I am encountering difficulties in accessing the javascript object beyond the file where it is defined.

The application uses a dojo wizard, so when users click "next" on the wizard to proceed to the next page, I want to submit the data (javascript object) from the current page to the server for validation and processing.

I have encountered issues related to the scope of the javascript object. Even after moving the javascript object (studentData) to global scope, the problem persists. Below are snippets of code from the javascript file and the form wizard:

Javascript function creating dgrid:

//Imports Dojo items
require([ "dojo/parser", "dojo/_base/declare", "dojo/store/Memory",

], function(parser, declare, Memory, OnDemandGrid, ColumnSet, Selection,
        selector, Keyboard, DijitRegistry, editor, ColumnHider, ready, Dialog,
        on, registry, Observable, lang) {

    var studentData = [ {
        id : "1",
        age : "33",
        idtype : "1",
        first_name : "Edward",
        surname : "Davis"
    }, {
        id : "2",
        age : "41",
        idtype : "2",
        first_name : "Lewis",
        surname : "Holl"
    }, {
        id : "3",
        age : "59",
        idtype : "3",
        first_name : "Fred",
        surname : "James"
    } ];

    dojo.ready(function() {
        // Code
        function byId(Id) {

            return document.getElementById(Id);
        }

    });

    var aliasStore = new dojo.store.Observable(new Memory({
        data : aliasData,
        idProperty : "id"
    }));
    var CustomStudentGrid = declare([ OnDemandGrid, selector, Selection,
            Keyboard, editor, DijitRegistry, ColumnHider ]);
    var studentGrid = new CustomStudentGrid({
        store : studentData,
        columns : {
            id : {
                label : "Id",
                field : "id",
                hidden : true
            },

            idtype : {
                label : "Id Code",
                field : "idtype",
                hidden : true
            },

            chkBox : selector({}),

            first_name : {
                label : "First Names",
                field : "first_name"
            },
            surname : {
                label : "Surname",
                field : "surname"
            }

        },
        selectionMode : "none",
        loadingMessage : "Loading data...",
        noDataMessage : "No results found....",
        allowSelectAll : true
    }, "studentGrid");

    aliasNameGrid.on('dgrid-select', function(event) {

        for ( var i in selectedRows) {

            event.rows[i].element.className = "deleteRow";

        }

    });

    aliasNameGrid.on('dgrid-deselect', function(event) {

        for ( var i in deSelectedRows) {
            // alert('Deselected Rows are ' + i);
            event.rows[i].element.classList.remove('deleteRow');

        }
    });

    studentGrid.refresh();

});

Wizard Form:

'passFunction' is called when the user clicks "next" button, invoking 'SendForm' function

How can I access the studentData within the SendForm function or any other external javascript file? The studentData will undergo changes by the user on the grid.

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>

    function SendForm(dojo) {

        console.log('Entered...');
        console.log(studentData.id[0]);
        };
</script>

<title>Student</title>
</head>
<body class="claro">

<form data-dojo-type="dijit/form/Form" id="student_form" name="student_form">

<div dojoType="dojox.widget.WizardPane" id="StudentWizPane" passFunction="SendForm">
            <div data-dojo-type="dojox.layout.TableContainer"
                data-dojo-props="cols:1,customClass:'employee_labels', labelWidth:180"
                id="StudentContainer">

            <div id= "studentGrid"></div>

</div>  

<!-- Other pages -->

Firebug Error

Within the Script tab, I am encountering the following error:

Object { error="Mozilla error: invalid scope variables"}

Answer №1

If you desire to have the variable studentData accessible globally, simply eliminate the var keyword. Yet if your intention is to retrieve edited data from the grid, then a possible solution would be:

digi.byId("studentGrid").store.query({})...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to retrieve and update the data of all the child elements within a table row dynamically?

I have a table with the following header information. <table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th> Date ...

Troubleshooting bitrate and quality issues in AWS MediaConvert

Whenever I attempt to initiate a MediaConvert job in CBR, VBR, or QVBR mode with Bitrate or MaxBitrate exceeding 250,000, an error occurs. The message reads: "Unable to write to output file [s3:///videos//***/original.mp4]: [Failed to write data: Access D ...

Unable to designate the drop-down option as the default selection

Can anyone help me with setting a default drop-down value using JavaScript? I have been struggling to achieve this. You can find my code on jsFiddle <div ng-controller="csrClrt"> <div ng:repeat="(key, item) in items track by $index"> ...

When watching YouTube videos in full screen mode on F11, the IFrame zooms in excessively, causing the video quality to suffer

After using the same code as the website Virtual Vacation, I noticed that they are experiencing the same issue as me. I am considering reaching out to them to inform them about it, but I am struggling to find a solution on my own. Specifically on WINDOWS ...

Conceal the menu on jQuery click event anywhere on the page

There is a button on my interface that, when clicked, opens a menu. The button appears blue when selected and the menu opens, but when a menu item is chosen, the menu closes and the button returns to its original state. However, my problem arises when I ...

Encountered an issue while attempting to load the required module

I'm having trouble setting up Stripe for my app and getting errors when trying to implement the module. Typically, I would require the module at the top of the file in order to use it, but when I do this in the paymentCtrl file, it doesn't work a ...

Expanding the size of the number input in Twitter Bootstrap to accommodate changing content dimensions

I have a numeric input field that I want to customize in terms of width. I need the field to start with a minimum width so that -1, the default value, fits nicely inside. As the value changes, whether decreasing to -100 or increasing to -1,000 and beyond ...

What causes Bootstrap to malfunction when the route contains double slashes?

When using /something, everything works fine, but when switching to /something/somethingelse, Bootstrap fails to function. It seems that the number of "/" characters in the route is causing this issue, rather than the content inside the .ejs file. Here is ...

Is there an alternative method to including a PHP file from a remote server other than using the "<?php include(); ?>" syntax?

I have developed a PHP script that retrieves information from a MySQL database and I am looking to integrate this script (which contains extracted content from the database) onto various remote servers. These clients have websites built with Joomla/WordPre ...

Is it possible to repeatedly activate a function using onmousedown just like with onkeydown?

My goal is to have the onmousedown event trigger repeatedly when the left mouse button is held down, instead of just once upon clicking. Currently, it only fires a single time. wHandle.onmousedown = function (event) { console.log('mouse b ...

Issue with Material UI button not properly redirecting to specified path

Having an issue with a button that should redirect to a specific component with props on click. <Button variant="contained" color="primary" className={classes.button} endIcon={<SendIcon/>} onClick={() => { <Redirect ...

Set up a JavaScript function that triggers an alert if two numbers are determined to be equal

I created a code snippet that should display an alert message when a button is clicked, indicating whether two random numbers generated are equal or not. The random numbers must be integers between 1 and 6. I implemented this functionality in JavaScript bu ...

Is it possible to refrain from using the object variable name in an ng-repeat loop?

When utilizing an ng-repeat directive to loop through an array, the syntax requires ng-repeat="friend in friends". Inside the template, you can then use the interpolation operator like this {{friend.name}}. Is there a way to directly assign properties to ...

Problem with loading CSS and JavaScript files on Node.js server

Recently, I delved into learning Node.js and created a basic server setup like this: // workspace const p = document.querySelector('p'); p.textContent = 'aleluja'; html { font-size: 10px; } body { font-size: 2rem; } <!DOCTYPE ht ...

Using React to retrieve an object from a helper method through a GET request

I am in the process of turning a frequently used function in my application into a helper method that can be imported wherever it is needed. However, I am facing an issue where I am not getting a response from the function when calling it. I need to figure ...

What is the best way to automatically post to Slack at regular intervals?

In order to provide users with the option to choose when they receive information from my API, such as daily at 5PM or every Friday at 5PM, I have a configuration page. Once users set their preferred time, I want to send them a Slack message at that specif ...

Shade the entire td column in different colors depending on the characteristics of th

I am working with a table and here is the code for it: <table> <thead> <tr> <th style="width: 40%; text-align: center; vertical-align: center;">Nr.<br> crt.</th> <th style="font-weight: bold; wi ...

The main page's navbar dropdown menu fails to appear after a certain length

I've encountered a strange problem with my navbar. I utilized an online template, and the issue is that on the main page, the dropdown menu doesn't extend fully; it only goes as far as the length of the navigation bar (Refer to the image below). ...

The XMLHttpRequest function successfully logs data in the console, but does not return the data within the function itself

I find it puzzling that the console.log statement at the end of the code snippet displays the data as expected, while the return optiondata line does not. function populate_selectbox() { var ajaxRequest; try { // Opera 8.0+, Firefox, S ...

The raycaster in Three.js seems to be having trouble selecting the correct object

Hey everyone, I'm currently working on selecting objects using a raycaster and I want to change the material of the first selected object. Everything works smoothly until I pick the object - when I select the first element, only one object changes. I ...