CoffeeScript and backbone.js Learnings

Created: 2020-02-06, Last Updated: 2023-02-04

Here is a listing of things I learned while trying to fight backbone and CoffeeScript

@ is this

@ is specific to CoffeeScript. It translates to this in JavaScript. One case that kept tripping me up was trying to access attributes in a backbone model. This is done with @get('my_attribute')

The ?

Turns out ? in CoffeeScript expands to quite a bit in JavaScript.

if elvis?

turns into

if (typeof elvis !== "undefined" && elvis !== null) {)

So use your ?'s.

Passing a hash to a create a backbone model

Something we were trying to do was pass in something to a model so that its source url would change depending on the params. What we learned was that passing a hash when creating a model will automagically set the key/values as attributes in your model instance. You don't even need to define a initialize method.

my_model = App.Models.MyModels({ name: 'something', cake: 'is a lie'})