So I’m writing some coffeescript for a project and I’m trying to create some static properties in a class. I’ve been following another file in the codebase that does the same thing successfully, but mine isn’t working.
My Code
class Messages
@toggleUnreadConversations:()->
# This is the line in question, Messages is defined with all the
# functions but the property ViewOnlyUnread is undefined
Messages.ViewOnlyUnread = !Messages.ViewOnlyUnread
@init:->
@ViewOnlyUnread = false
Other code in the code base that successfully uses static properties
class Map
@CacheRealtor: (realtor) ->
realtor.realtor_id = parseInt(realtor.realtor_id)
# Here the static property IdToRealtorMap is defined
Map.IdToRealtorMap[parseInt(realtor.realtor_id)] = new Realtor()
@Init: ->
@IdToListingMap = []
@IdToRealtorMap = []
From what I can tell these init functions are being called the same way, when the page loads init is called. Both classes are static classes, there is never an instance of either of them being created. Does anyone have any idea to what could be the issue?
The
initfunction is setting an instance variable, but yourtoggleUnreadConversationsfunction is trying to reference it as though it a property of your class.You should use
@to refer to the instance variable thatinitsets: