Coffeescript gets the proper amount from the callback method

I searched this and cannot find a successful answer, I am using jQuery ajax call and I can not get the answer to the callback.

Here is my coffeescript code:

initialize: (@blog, @posts) ->
    _url = @blog.url
    _simpleName = _url.substr 7, _url.length
    _avatarURL = exports.tumblrURL + _simpleName + 'avatar/128'
    $.ajax
        url: _avatarURL
        dataType: "jsonp"
        jsonp: "jsonp"
        (data, status) => handleData(data)

handleData: (data) =>
    console.log data
    @avatar = data

Here's the compiled JS:

  Blog.prototype.initialize = function(blog, posts) {
    var _avatarURL, _simpleName, _url,
      _this = this;
    this.blog = blog;
    this.posts = posts;
    _url = this.blog.url;
    _simpleName = _url.substr(7, _url.length);
    _avatarURL = exports.tumblrURL + _simpleName + 'avatar/128';
    return $.ajax({
      url: _avatarURL,
      dataType: "jsonp",
      jsonp: "jsonp"
    }, function(data, status) {
      return handleData(data);
    });
  };

  Blog.prototype.handleData = function(data) {
    console.log(data);
    return this.avatar = data;
  };

I tried a dozen variations and I can’t figure out how to write it down?

Thank.

+5
source share
2 answers

Your arguments are incorrect, you pass the callback as the second parameter to $.ajax. You must pass it as success:in parameters or add it to the pending Ajax object.

Since handleData looks like it is bound to an object, most likely thisyou need to prefix it with @.

URL- , API URL- , - .

$.ajax _avatarURL,
  dataType: "jsonp"
  jsonp: "jsonp"
  success: (data, status) => @handleData(data)

$.ajax _avatarURL,
  dataType: "jsonp"
  jsonp: "jsonp"
.done (data) => @handleData(data)
+2

handleData Blog , , , , :

(data, status) => @handleData(data)
+2

All Articles