0

I have the following code withhn a module

var def = $.Deferred();

$.getJSON("http://localhost:62588/api/Values/getXMLData")
 .done(function(json){
      def.resolve($.parseJSON(json));
});

return def;

I then have to call it from another module so it completes, before calling a processing the returned data.

repository.getUserPolicies().done(function (userPolicies) {
    process(userPolicies);
});

This works well but I don't yet quite understand how the deferred object works yet.

I have read that you can use getJSON's deferred object but not sure if that's exactly what I'm doing here?

I was wondering if there are any disadvantages of this approach?

Can it be done more elegantly?

Thanks

davy
  • 4,474
  • 10
  • 48
  • 71

1 Answers1

6

No, this is not a good use of $.Deferred, as $.getJSON already has a deferred object built in with the added support of promises etc. in newer versions of jQuery.

You can use that built in deferred object, and just return the ajax call, and attach the done() function to that directly:

var repository = {
    getUserPolicies: function() {
        return $.getJSON("http://localhost:62588/api/Values/getXMLData");
    }
}

repository.getUserPolicies().done(process);

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • I think you're missing one important detail. Are you going to call `parseJSON` in every done callback? And what if later `getUserPolicies` will get response as xml or bson (or whatever)? – meze Dec 08 '12 at 22:00
  • That's not really relevant to the question, as the `getUserPolicies` method clearly contains a `$.getJSON` call, and only retrieves JSON. The logic for parsing data seems to be in the `process()` function, so that would probably be the place to figure out how to process the returned data, and if different formats are in use figure out what format it received etc. – adeneo Dec 08 '12 at 22:01
  • It is relevant. A user of `getUserPolicies` shouldn't care how the method does its job. And my first question remains unaswered. – meze Dec 08 '12 at 22:04
  • Thanks, that is much better. And, I will move the offending parseJSON into the appropriate place when I figure out where that is :) – davy Dec 08 '12 at 22:08
  • 1
    @davy don't blindly follow such answers. It's still better in some cases to make it explicitly and better rewrite the second example as `repository.getUserPolicies().done(process);` – meze Dec 08 '12 at 22:12
  • 1
    There seems to be some confusion, I kept the `$.parseJSON` as it was already there, but I'll remove it, and this is'nt an issue at all as `$.getJSON` uses `$.parseJSON` internally and already does this for you anyway, so the returned data will always be an object, or in case someone tries to send XML or any other datatype, the returned data will be null and void, making this discussion pointless. – adeneo Dec 08 '12 at 22:20
  • @meze - thanks again. Yeah, as I said in the question, I thought you could use the built in methods but couldn't get it to work. I was very close before changing to the explicit version. I have picked up some good points from both your comments and should now be better prepared to choose the correct method - after a little more study... – davy Dec 08 '12 at 22:23