Get started with Backbone and CoffeeScript

前端 未结 2 1283
自闭症患者
自闭症患者 2021-02-14 05:56

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

相关标签:
2条回答
  • 2021-02-14 06:20

    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.

    0 讨论(0)
  • 2021-02-14 06:45

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

    0 讨论(0)
提交回复
热议问题