0
$ ->
   window.app.helpers = new class Helpers

    constructor: (@name) ->
      @slideRecipeId     = $("#slideRecipe")
      @specialities      = ["A", "B", "C"]

      setTimeout @countUpRecipes, 3000

    countUpRecipes: ->
      @slideRecipeId.html(@specialities[Math.floor(Math.random() * @specialities.length)])

The problem is that on load I get these errors:

Uncaught TypeError: Cannot read property 'length' of undefined 

What's wrong with the code ? Thanks.

Alex
  • 2,036
  • 4
  • 22
  • 31
  • if you `console.log @specialities` in the `countUpRecipes` function, what do you get? – Blaine Kasten Aug 15 '13 at 18:59
  • 1
    `countUpRecipes: =>` see http://coffeescript.org/#fat-arrow – asawyer Aug 15 '13 at 19:04
  • Check your indentation on the functions here on stackoverflow they look to be indented 1 space while everything else is 2 spaces. – Grant Aug 15 '13 at 19:04
  • asawyer is correct, use a fat arrow in the definition of countUpRecipes. @asawyer you should post that as an answer, possibly with a little bit of explanation. – Nitzan Shaked Aug 15 '13 at 19:07
  • [Avoid that `new class {…}` pattern!](http://stackoverflow.com/questions/10406552/is-it-right-to-think-of-a-javascript-function-expression-that-uses-the-new-key) Is that supposed to be a singleton? – Bergi Aug 15 '13 at 19:31

1 Answers1

1

You need to bind the context when calling @countUpRecipes:

setTimeout => 
    @countUpRecipes
, 3000
pdoherty926
  • 9,895
  • 4
  • 37
  • 68