How to consume the data in a function from the fetchData method?

559
6
Jump to solution
12-20-2017 10:57 AM
MartinOwens1
Occasional Contributor II

I'm using the Enhanced Search widget to pass a value from a feature using the publishData method to another widget.

Sending Widget:

        this.publishData({
          data: feature.attributes["PARID"],

         keepHistory: true

  })

I'm using the onReceiveData function in the other widget to grab the data. If I write to the console under this function it gets the value, but when I try to consume it in a different function it doesn't retun the data. I've tried setting it to a global variable and still nothing. I have no idea what is wrong:

Receiving widget:


startup: function() {
this.inherited(arguments);
this.fetchDataByName('eSearch');

//console.log('startup');
},

onReceiveData: function(name, widgetId, data, historyData) {
//filter out messages
if(name !== 'eSearch'){
return;
}
window.ParcelData = data;

console.log(ParcelData);                <THIS WORKS>
},
onOpen: function(){
console.log('onOpen');
require(["dijit/layout/TabContainer", "dijit/layout/ContentPane", "dojo/domReady!"], function(TabContainer, ContentPane){

var tc = new TabContainer({
style: "height: 100%; width: 100%;"
}, "tc1-prog");

var cp1 = new ContentPane({
title: "Parcel Info",
content: "<div id='parcelPropInfo'></div>"
});

tc.addChild(cp1);

var cp2 = new ContentPane({
title: "Building Info",
content: "<div id='parcelBldgInfo'></div>"
});

tc.addChild(cp2);
tc.startup();
});

// parcelPropInfo
dojo.ready(function(){
// Look up the node we'll stick the text under.
var targetNode = dojo.byId("parcelPropInfo");

// The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
var xhrArgs = {
//url: "./widgets/Demo/resources/parcelPropInfo.aspx?parid=A02000200230000400",
url: "./widgets/Demo/resources/parcelPropInfo.aspx?parid=" + ParcelData,  <THIS DOES NOT>
handleAs: "text",
load: function(data){
// Replace newlines with nice HTML tags.
//data = data.replace(/\n/g, "<br>");

// Replace tabs with spaces.
//data = data.replace(/\t/g, "   ");

targetNode.innerHTML = data;
//return data;
},

error: function(error){
targetNode.innerHTML = "An unexpected error occurred: " + error;
//return "An unexpected error occurred: " + error;
}
}

Any help would be appreciated

Thanks

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Martin,

   OK, Actually that makes sense. The startup function is not going to have this.ParcelData yet because it is still null when the widget is started up. How about moving your xhr stuff to the onReceiveData?

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Martin,

url: "./widgets/Demo/resources/parcelPropInfo.aspx?parid=" + ParcelData,  <THIS DOES NOT>

Shouldn't that be window.ParcelData not ParcelData?

MartinOwens1
Occasional Contributor II

I attempted that as well and still no success.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Martin,

   I don't think I have ever seen anyone use dojo.ready in a widget before. The issue is that that function will have a different scope then the rest of the widget code. Why are you not doing all that stuff (now in the dojo.ready function) inside the widgets startup? Also widget based global vars are created using this.ParcelData. Unless you need this var to be available inside the whole app then there is no need to use window.ParcelData.

0 Kudos
MartinOwens1
Occasional Contributor II

Adding the dojo.ready functions to the startup doesn't seem to have any effect on the result. I made the change on variable I think during troubleshooting obtaining the value. I've set it now using this.ParcelData. Would you have any other ideas on how to pass the object sting to the url the dojo function?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Martin,

   OK, Actually that makes sense. The startup function is not going to have this.ParcelData yet because it is still null when the widget is started up. How about moving your xhr stuff to the onReceiveData?

0 Kudos
MartinOwens1
Occasional Contributor II

You are the man Robert! Works like a charm. Thanks!

0 Kudos