Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get started with Backbone and CoffeeScript

I think this is more of a CoffeeScript question. I want to be able to use classes from Backbone in a foo.coffee file. I tried using the -r option to require Backbone when running the coffee command:

coffee -r "../backbone" -c foo.coffee

The compiler complained that Backbone was undefined. I'm sure that this must be pretty simple. It's easy to find examples of people using CoffeeScript and Backbone together. I also tried requiring the class at the top of the file like so:

Backbone.model = require('../../backbone').Model

class foo extends Backbone.model

I could write it to console.log in the initialize method. When I tried writing this to console.log, I just got an empty object {}.

Can anyone tell me how to get this going?

like image 629
Paul Avatar asked Mar 05 '11 02:03

Paul


People also ask

Is Backbone JS frontend or backend?

Backend Synchronization BackboneJS is use with the front-end and back-end systems, allows the synchronization with the backend to provide support to RESTful APIs.

Should I learn backbone JS?

The developers should make use of Backbone JS while developing a single-page Java application. Backbone JS features Model View Framework, which allows much more than structuring JavaScript architecture. It will help the developers eliminate several issues that they might be facing while developing apps.

How do I set up backbone JS?

Downloading the UI library from its official website Development Version − Right click on this button and save as and you get the full source JavaScript library. Production Version − Right click on this button and save as and you get the Backbone-min. js library file which is packed and gzipped.

What is Backbone code?

Backbone. js is a JavaScript library, among many others, that is gaining special attention in the web development community because of its ease of use and the structure that it provides to JavaScript applications.


2 Answers

If you're using CoffeeScript and Backbone.js, I recommend checking out Brunch. It may just get you past your difficulties.

like image 194
34m0 Avatar answered Oct 25 '22 03:10

34m0


Could you provide more of your code? I wasn't able to replicate the issue you had with initialize. Here's my code, with backbone.js in the same directory as the coffee file:

Backbone = require './backbone'

class foo extends Backbone.Model
  initialize: ->
    console.log this

new foo

On new foo, initialize is called and the output is

{ attributes: {},
  _escapedAttributes: {},
  cid: 'c0',
  _previousAttributes: {} }

As to the issue with -r, there are two reasons it doesn't work: First, -r performs

require '../backbone'

without assigning it to anything. Since Backbone doesn't create globals (only exports), the module has to be assigned when it's required.

Second, using -r in conjunction with -c doesn't add the required library to the compiled output. Instead, it requires it during compilation. Really, -r only exists so that you can extend the compiler itself—for instance, adding a preprocessor or postprocessor to the compilation pipeline—as documented on the wiki.

like image 22
Trevor Burnham Avatar answered Oct 25 '22 02:10

Trevor Burnham