CoffeeScript Class Properties Within Nested Anonymous Functions

Posted by Aric on Stack Overflow See other posts from Stack Overflow or by Aric
Published on 2012-04-01T17:25:41Z Indexed on 2012/04/01 17:29 UTC
Read the original article Hit count: 413

I'm just taking a look at CoffeeScript for the first time so bare with me if this is a dumb question. I'm familiar with the hidden pattern methodology however I'm still wrapping my head around object prototypes.

I'm trying to create a basic class for controlling a section on my site. The problem I'm running into is losing defined class variables within a different scope. For example, the code below works fine and creates the properties within the object perfectly. However when I jump into a jQuery callback I lose all knowledge of the class variables storing some of the jQuery objects for multiple uses.

Is there a way to grab them from within the callback function?

class Session
    initBinds: ->
        @loginForm.bind 'ajax:success', (data, status, xhr) ->
            console.log("processed")
            return
        @loginForm.bind 'ajax:before', (xhr, settings) ->
            console.log @loader // need access to Session.loader
            return
        return
    init: ->
        @loginForm = $("form#login-form")
        @loader = $("img#login-loader")
        this.initBinds()
        return

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about coffeescript