0

I am new to coffeescript and I have a coffee script code as

getProviderListDisplayValues:(domainId) ->      
    displayValues = []
    $.ajax 
        contentType: 'application/json',
        url: "/Services/ListProviders?domainid=#{domainId}",
        success: (data) ->          
            for oneResponse in data
                displayValues.push oneResponse.name     
    displayValues

which is compiled to

CounselorHome.prototype.getProviderListValues = function(domainId) {
   var values;
   values = [];
   $.ajax({
     contentType: 'application/json',
     url: "/Services/ListProviders?domainid=" + domainId,
     success: function(data) {
       var oneResponse, _i, _len, _results;
       _results = [];
       for (_i = 0, _len = data.length; _i < _len; _i++) {
         oneResponse = data[_i];
         _results.push(values.push(oneResponse.id));
       }
       return _results;
     }
   });
   return values;
};

I just want to push values to values[] & displayValues[] but why is the _results[] array created? Does it hampers the browser efficiency? Is there any way removing such unnessary code? May be, by editing my coffee script.

EDIT : WORKING CONDITION

But when I put an alerting code as

 $.ajax 
        contentType: 'application/json',
        url: "/Services/ListProviders?domainid=#{domainId}",
        success: (data) ->          
            for oneResponse in data
                displayValues.push oneResponse.name     
 alert displayValues
 displayValues

This code works and I can retrieve the required data.

Jeetendra
  • 205
  • 3
  • 18
  • _ajax_ is _asynchronous_ so I think you're trying to do something that won't work as you expect. It looks like coffee notices this, and so never gives you `_results` in a referencable way – Paul S. Aug 28 '14 at 11:59
  • Yes, while reurning values[] it returns undefined for Google Chrome and null for FireFox. But I have written code to push all data to values[]. Hence, the problem. – Jeetendra Aug 28 '14 at 12:06

1 Answers1

2

Apart from that your code wouldn't work anyway, the _results are generated because of coffeescripts implicit function return values - and loops are only expressions as well that generate arrays. The docs state:

Sometimes functions end with loops that are intended to run only for their side-effects. Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like true — or null, to the bottom of your function.

So the javascript that you expected can be created by writing

…
    success: (data) ->
        for oneResponse in data
            displayValues.push oneResponse.name
        return
…

("Trailing return and return undefined are now optimized away." - since version 1.0.1);
See also Is there any way to not return something using CoffeeScript?

However, what you actually want is this:

getProviderListDisplayValues:(domainId) ->
    $.ajax
        contentType: 'application/json',
        url: "/Services/ListProviders?domainid=#{domainId}"
    .then (data) ->          
        for oneResponse in data
            oneResponse.name

…

getProviderListDisplayValues(…).then (displayValues) ->
    …
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I was a bit less strong with the _won't work as expected_ rather than _wouldn't work anyway_, because OP's code doesn't actually destroy the reference to `displayValues` (until it's converted), meaning that after _success_, it should be populated. It's just there is nothing letting the rest of the script know the success function has completed ... and coffee's conversion. – Paul S. Aug 28 '14 at 12:11
  • 1
    @PaulS.: Yeah, and that makes the `displayValues` array unusable - I was so bold because I expected the OP to use the array just after it was returned. The mistake is just too common :-/ – Bergi Aug 28 '14 at 12:17
  • If so, how to populate values[], because I need to return an array? – Jeetendra Aug 28 '14 at 12:32
  • You can't return an array which you are getting asynchronously from your function. That's just impossible (and using `alert` like in your edit only does some weird timing things that you don't want). The function in my answer does return a promise for the array. See the question I linked for further details. – Bergi Aug 28 '14 at 12:37