Coffeescript getting proper scope from callback method

Posted by pandabrand on Stack Overflow See other posts from Stack Overflow or by pandabrand
Published on 2012-04-01T05:22:53Z Indexed on 2012/04/01 5:29 UTC
Read the original article Hit count: 150

I've searched for this and can't seem to find an successful answer, I'm using a jQuerey ajax call and I can't get the response out to the callback.
Here's 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've tried a dozen variations and I can't figure out how to write this?

Thanks.

© Stack Overflow or respective owner

Related posts about jquery-ajax

Related posts about scope