I’m new to CoffeeScript, and I seem to be having trouble with the syntax for calling methods.
Here’s the Card model:
class exports.Card extends Backbone.Model
defaults:
pip: '4'
suit: '♠'
color: 'b'
rows: ->
rows =
'4': [2, 0, 2]
rows[@pip]
And the relevant portion of the template:
<ul class="col cols-<%= @card.rows()[0] %>">
which is giving me the error Uncaught TypeError: Object #<Object> has no method 'rows'
Specifically, I’m wondering if I’m using incorrect syntax for the rows method of Card or if I’m just misunderstanding something. Thanks in advance!
Update:
For some reason, @card.property always works fine, but @card.any_method() never does. I’ve gotten around this at the moment by using properties, but I’d love it if someone was able to explain this behavior. Thanks again!
Update 2:
I’m using http://brunchwithcoffee.com if it’s a help to anyone, and here’s the main.coffee file to show how the @card instance is being created and passed to the view.
window.app = {}
app.routers = {}
app.models = {}
app.collections = {}
app.views = {}
Card = require('models/card_model').Card
MainRouter = require('routers/main_router').MainRouter
HomeView = require('views/home_view').HomeView
CardView = require('views/card_view').CardView
# app bootstrapping on document ready
$(document).ready ->
app.initialize = ->
app.routers.main = new MainRouter()
app.views.home = new HomeView()
app.views.card = new CardView(model: new Card(color: 'r', suit: '♥', pip: '7'))
app.routers.main.navigate 'home', true if Backbone.history.getFragment() is ''
app.initialize()
Backbone.history.start()
Your method calling syntax is correct. The relevant rules for CoffeeScript are:
Parenthesis are optional for method calls invoked with arguments ie
or
Parenthesis are required for method calls invoked with no arguments ie
To see how this works try running the following code on the ‘Try CoffeeScript’ editor on the CoffeeScript site:
Since your method call syntax is correct it seems likely that the problem is that the @card variable is not an instance of the exports.Card class.