I am developing a project wherein I require a global object to be instantiated with initial values, updated via an Angular Service, and then referenced again from within its native file with updated values.
The ideal workflow would look something like this:
- User enters input and causes event
- Service reacts to event and makes changes to global object values accordingly
- User causes event to update information
- Update event calls a function
- Function references global object with updated values and submits changes accordingly
Currently, the service appropriately calls the global function and logs out the object to confirm the values have been changed. However, when I attempt to reference the updated object from within its native file otherwise, the console logs out the same object without its new values.
I have tried updating the values using both simplified js files and Angular services and both yield the same issue. Ive also tried referencing the object using various syntax including always pointing to window.object or just object. The same result persists.
I will show some code below to help better outline the context.
The function which is supposed to reference the updated values is called "prepareExternalInterface" and is nested as follows:
PLEASE NOTE: I have deleted some code in the interest of clarity.
window.addEventListener('load', function() {
function prepareExternalInterface(app) {
// register functions in the app.ExternalInterface to call them from Puzzles
app.ExternalInterface.updateModel = function(){
console.log("Attempting to update model:")
console.dir(window.nexus)
let resizeModel= function(height,width,depth){
v3d.puzzles.procedures.resizeModel(height,width,depth)
}
resizeModel(nexus.heightShapeFactor,nexus.widthShapeFactor, nexus.depthShapeFactor);
let displayCurrentDoor= function(door){
v3d.puzzles.procedures.displayCurrentDoor(door)
}
displayCurrentDoor(nexus.currentDoor);
let displayPrintPocket=function(selected){
if (selected){ v3d.puzzles.procedures.showPrintPocket() } else { v3d.puzzles.procedures.hidePrintPocket() }
}
displayPrintPocket(nexus.drawingPocket);
let displayRainSheild=function(selected){
if (selected){ v3d.puzzles.procedures.showRainSheild() } else { v3d.puzzles.procedures.hideRainSheild() }
}
displayRainSheild(nexus.rainSheild);
}
}
The object it is attempting to reference is called "nexus" and is defined as a singleton in the global scope as follows. PLEASE NOTE: I have deleted some code in the interest of clarity.
var nexus = {
heightShapeFactor: 0,
widthShapeFactor: 0,
depthShapeFactor: 0,
door: true,
drawingPocket:false,
noPlate:false,
rainSheild: false,
customLatch: false,
twoPTN: false,
threePTN: false,
currentDoor: 'NEMA Door',
nexusImport(nexusUpdate,selectedAccessories,selectedParameters){
this.heightShapeFactor = nexusUpdate.heightShapeFactor;
this.widthShapeFactor = nexusUpdate.widthShapeFactor;
this.depthShapeFactor = nexusUpdate.depthShapeFactor;
for(let i=0;i<selectedAccessories.length;i++){
switch(selectedAccessories[i]){
case'Drawing Pocket':
console.log("Drawing Pocket selection has been imported to the Nexus")
this.drawingPocket=true;
break;
case'No Plate':
console.log("No Plate selection has been imported to the Nexus")
this.noPlate=true;
break;
case'NEMA Rain Sheild' :
console.log("NEMA Rain Sheild selection has been imported to the Nexus")
this.rainSheild=true;
break;
case'Custom Latch':
console.log("Custom Latch selection has been imported to the Nexus")
this.customLatch=true;
this.selectDoor(selectedParameters)
}
}
console.log(nexus)
}
}
When I start my program, the initial settings are as shown above for the nexus object. After the settings are imported using nexusImport it logs out the object with updated values. Then the event is triggered which calls the external interface which also logs out the nexus object. Here is a snippet of the console log:
No Plate selection has been imported to the Nexus
N12_Model.js:44 {heightShapeFactor: 0.17, widthShapeFactor: 0.33, depthShapeFactor: 0.6, door: true, drawingPocket: true, …}
N12_Model.js:285 Attempting to update model
N12_Model.js:286 {heightShapeFactor: 0, widthShapeFactor: 0, depthShapeFactor: 0, door: true, drawingPocket: false, …}
Any indicators on where to start tracing this issue or alternate methods of defining and updating the nexus object would be very much appreciated. Or maybe I'm just not getting the syntax correct. I am fairly new to javascript. Anything helps.