1

I am trying to use CoffeeScript to setup an AJAX callback function like so:

The Pattern

function doAjax(callback)
{
  $.ajax(url, data)
    .done(function(){
       // ... do stuff here ...
       callback(true);
    }).fail(function(){
       callback(false);
    });
}

function doSomething()
{
  doAjax(function(result){
    if (result == true )
       console.log('success');
    else
       console.log('failed');
  });
}

I am using the following CoffeeScript to do this (this is within an object):

CoffeeScript

doAjax: (callback) ->
  $.getJSON(url)
    .done( (data) ->
      if something == true
        callback(true)
      else
        callback(false)
    ).fail( () ->
      callback(false)
    )

doSomething: () ->
  this.doAjax(function:(result)->
    if result == true
      console.log "true"
    else
      console.log "false"

It results in the following compiled JavaScript like this:

Compiled JS

MyObject.prototype.doAjax = function(callback) {

  return $.getJSON(url).done(function(data) {
    if (something == true) {
      callback(true);  // <--- The error happens here
    } else {
      callback(false);
    }
  }).fail(function() {
    callback(false);
  });
};

MyObject.prototype.doSomething = function() {

  return this.doAjax({
    "function": function(result) {
      var message;

      if (result === true) {

        return console.log("true");
      } else {
        return console.log("false");
      }
    }
  });
};

And I get the error (at the marked line above):

Uncaught TypeError: object is not a function 

What am I doing wrong in my CoffeeScript here?

5
  • You are not passing a function to doAjax, but an object. The error seems pretty clear. Commented Nov 12, 2014 at 5:42
  • Are you talking about this.doAjax(function:(result)->? Is that not how you define an anonymous function? Commented Nov 12, 2014 at 5:43
  • You defined anonymous functions in doAjax, you do it the same () -> Commented Nov 12, 2014 at 5:45
  • Ah! I don't know how I missed that. Thank you. If you want to put your comment as an actual answer I will accept and upvote. Commented Nov 12, 2014 at 5:46
  • 1
    There are answers already, I upvoted myself. Note that you can remove all those parentheses in CoffeeScript. Commented Nov 12, 2014 at 5:53

2 Answers 2

2

change this

this.doAjax(function:(result)->

to this

this.doAjax((result)->

functions in coffeescript are declared with () ->. function:() -> creates an object with a property called function that contains the actual function

Sign up to request clarification or add additional context in comments.

Comments

-1

Actually you are trying to callback function to perform some operation after successful ajax call, but you are calling the object as function.Define some function and use it to callback.thank you, for further assistance please report to me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.