AMD - Using ESRI JSAPI 1.3 classes

413
3
08-06-2012 06:39 PM
IhabEl_Attar
New Contributor
I followed the link below from ESRI help to use AMD to build a class that calls a find task. The link below uses the seatGeek api and not ESRI JSAPI classes.I am having issues getting the callback function processResults called. Does this programming pattern work with ESRI Javascript api ? I am 100 percent sure that the find task returns a response and I can see it in firebug. The callback function of the find task just never fires. If i create an instance of the class do find fires but the return is empty.Please advise



http://help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp/intro_javascript_classes.htm




dojo.provide("esric.CommonFrameWork");
dojo.require("esri.map");
dojo.require("esri.tasks.identify");
dojo.require("esri.tasks.find");
dojo.declare("esric.find", null, {

    findTask: null,
    findParams: null,
    esricMap: null,
    results: null,

    constructor: function (options) {
        //specify class defaults


        esricMap = options.aMap


        //Create Find Task using the URL of the map service to search
        findTask = new esri.tasks.FindTask("http://server1/arcgis/rest/services/esric/TLGIS/MapServer");

        //Create the find parameters
        findParams = new esri.tasks.FindParameters();
        findParams.returnGeometry = true;
        findParams.layerIds = [5];
        findParams.searchFields = ["STATION_JUNCTION_NAME"];
        findParams.outSpatialReference = options.aMap.spatialReference;

        // dojo.hitch is to provide the proper context so that processResults
        // will have access to the instance of this class

                this.processResults = dojo.hitch(this, this.processResults);


    },

    doFind: function () {

        findParams.searchText = "LARCHWOOD TS";
        var eventsResponse = findTask.execute(findParams);
        return eventsResponse.then(this.processResults, this.err);
       
    },

    processResults: function (response) {
        results = response;
        return response;
    },
    err: function (anError) {
        alert("error");
    }


});
0 Kudos
3 Replies
derekswingley1
Frequent Contributor
Please post your code showing how you create an instance of this class, how you call doFind and how you're saving the return value of doFind.

I recommend using this.<class-property> instead of only <class-property> inside your class methods as it's clearer and will help you avoid accidentally creating additional global variables. findTask, findParams, esricMap, and results are all examples of this.

Just to be clear, are you using version 1.3 of the API for 3.1?
0 Kudos
IhabEl_Attar
New Contributor
Please post your code showing how you create an instance of this class, how you call doFind and how you're saving the return value of doFind. 

I recommend using this.<class-property> instead of only <class-property> inside your class methods as it's clearer and will help you avoid accidentally creating additional global variables. findTask, findParams, esricMap, and results are all examples of this. 

Just to be clear, are you using version 1.3 of the API for 3.1?


Thanks for your response.I am using version 3.1 of the AGS JSAPI (sorry for the typo). I modified the code to use the this.<class-property> as you suggested.

I create an instance of the class in an init function that fires when the application is loaded
anEsricFind = new esric.find({ aMap: map, serviceUrl: mapServiceUrl });

I call the doFind when the user clicks on a button.

onclick="anEsricFind.doFind();"

I modified my class code a bit and attached it to my post (see attachment below). It now works for me and returns the response.

The problem was the timing of displaying the response. For example firing the following code on the onclick event will alert the results of the find task.

onclick="alert(dojo.toJson(anEsricFind.doFind().then(function(){ alert(anEsricFind.results); }), true));
0 Kudos
derekswingley1
Frequent Contributor
Glad you got it working, thanks for following up.
0 Kudos