1

DISCLAIMER - I've reviewed the existing SO entries and cobbled together something that should work but still does not.

I have the following function. Basically it is sending a pair of values to a webservice with the results coming back in JSON:

getPicklist: function () {
        var xhrArgs = {
            url: 'myUrl',
            postData: dojo.toJson({
                'opportunityId': 'myOppId',
                'loggedInUserId': 'myUserId' //App.context.user.$key
            }),
            headers: {
                "Content-Type": "application/json"
            }
        }
        var deferred = dojo.xhrPost(xhrArgs);
        deferred.then(
                    function (data) {
                        var jsonResponse = dojo.fromJson(data);
                        picklistName = jsonResponse.PicklistName;

                        if (!picklistName) {
                            picklistName = "defaultPickListName";
                        }
                        return picklistName;
                    },
                function (error) {
                    alert("Could not load picklist " + error);
                });
        ;
        //return picklistName; -- null
    }

My understanding after reading this: anonymous js function with xhrpost dojo not returning data

Was that adding a variable outside of this function scope, along with using dojo.deferred, would fix the issue. I tried placing a var outside of the function, and assigned the object to the picklistName variable.

However, I still cannot get this function's result (the picklistName variable).

Can someone please clarify what I am doing wrong, and how I can fix it?

EDIT - After making the changes Thomas Upton suggested, I am closer but I am getting a strange error.

I added the following function after getPicklist:

    returnPicklistName: function () {
        this.getPicklist().then(function (picklistName) {
            return picklistName;
        })
    },

Because all I really want is the picklist (there's JSON that I want really but I will settle just for the picklist for now).

This throws the following error in Chrome dev tools - Uncaught TypeError: Object [object Object] has no method 'getPicklist'.

What else did I miss? Thanks.

Community
  • 1
  • 1
Tek
  • 31
  • 3
  • not sure why you have the dojo.toJson function in the args, its not needed. – tik27 Sep 10 '13 at 22:20
  • Also depending on how you are using the getPickList function, the deferred as you are using it wont work. because the getPickList function will have exited long before the deferred then is executed. Are you having a problem with the ajax function, or not getting the value as a return – tik27 Sep 10 '13 at 22:36
  • @tik27 getting the value as a return. I am calling this in the context of a JS framework (argos-sdk) and trying to dynamically name a textbox. According to the docs for it the way to do this is by something like this.getPicklist().bindDelegate(). However, the function itself always returned a null string, so I figured I had some scoping issue. – Tek Sep 10 '13 at 22:40
  • What version of dojo are you using? – Thomas Upton Sep 11 '13 at 22:04

1 Answers1

1

Instead of returning picklistName at the end of getPicklist, you need to return a promise -- here, the result of then() -- and add a callback that will receive the picklistName when the deferred resolves.

getPicklist: function () {
    // ...
    var deferred = dojo.xhrPost(xhrArgs);
    return deferred.then(
        function(data) { /* get picklistName from data */ return picklistName; },
        function(error) { /* ... */ }
    );
}

Then, when you call getPicklist:

this.getPicklist()
    .then(function(picklistName) {
        // Use picklistName here
    });
Thomas Upton
  • 1,853
  • 12
  • 22
  • I made the improvements you suggested but I am still getting an error. The second function you indicate at the bottom of your response returns an anonymous object but I am still missing something. Thanks for any additional insight. – Tek Sep 11 '13 at 14:33
  • If the error handler is being called, that means something went wrong in either the request or the callback. I'd start debugging by logging `data` in the callback to see if it's actually firing and move forward from there. – Thomas Upton Sep 11 '13 at 21:58
  • Unless you meant the second code block, in which case I'm not sure what you mean by "returns an anonymous object." Can you clarify? – Thomas Upton Sep 11 '13 at 22:33