Roberto Pezzali Roberto Pezzali - 9 months ago 55
CoffeeScript Question

Return an has with a coffeescript class

I want to wrap some pliugin options in a js class using Coffeescript.

In plain JS I have

toastr.options = {
"closeButton" : false,
"debug" : false,
"positionClass" : "toast-bottom-right",
"onclick" : null,
"showDuration" : "300",
"hideDuration" : "1000",
"timeOut" : "8000",
"extendedTimeOut" : "1000",
"showEasing" : "swing",
"hideEasing" : "linear",
"showMethod" : "fadeIn",
"hideMethod" : "fadeOut"

With Coffeescript

class @ToastrOptions
constructor: ->
'closeButton': false
'debug': false
'positionClass': 'toast-bottom-full-width'
'onclick': null
'showDuration': '300'
'hideDuration': '1000'
'timeOut': '8000'
'extendedTimeOut': '1000'
'showEasing': 'swing'
'hideEasing': 'linear'
'showMethod': 'fadeIn'
'hideMethod': 'fadeOut'

toastr.options = new ToastrOptions

When I check
the has is blank {}. Why?


Code below defines ToastrOptions class. It has toHash method defined which returns plain js object with predefined values. Referring to your code - you put this plain object definition directly in constructor method which couldn't work, because a result of new AnyClass is always a new instance of this class, no matter what you put in constructor method.

Code below tested on

class ToastrOptions
  toHash: ->
    closeButton: false
    debug: false
    positionClass: 'toast-bottom-full-width'
    onclick: null
    showDuration: '300'
    hideDuration: '1000'
    timeOut: '8000'
    extendedTimeOut: '1000'
    showEasing: 'swing'
    hideEasing: 'linear'
    showMethod: 'fadeIn'
    hideMethod: 'fadeOut'

console.log(new ToastrOptions().toHash())