Backbone 和 CoffeeScript 入门

发布于 2024-10-20 16:26:42 字数 995 浏览 1 评论 0 原文

我认为这更像是一个 CoffeeScript 问题。我希望能够在 foo.coffee 中使用 Backbone 中的类文件。我尝试使用 -r 选项在运行 Backbone >coffee 命令:

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

编译器抱怨 Backbone 未定义。我确信这一定非常简单。很容易找到人们使用 CoffeeScript骨干在一起。我还尝试要求该类位于文件顶部,如下所示:

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

class foo extends Backbone.model

我可以将其写入 initialize 方法中的 console.log 。当我尝试将 this 写入 console.log 时,我只得到一个空对象 {}

谁能告诉我如何做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2024-10-27 16:26:42

如果您使用 CoffeeScriptBackbone.js,我建议查看早午餐
它可能会让你克服困难。

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

许仙没带伞 2024-10-27 16:26:42

你能提供更多你的代码吗?我无法重现您在initialize 中遇到的问题。这是我的代码,其中 backbone.jscoffee 文件位于同一目录中:

Backbone = require './backbone'

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

new foo

new foo 上,initialize被调用,输出为

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

关于 -r 的问题,它不起作用有两个原因:首先,-r 执行时

require '../backbone'

没有将其分配给任何东西。由于 Backbone 不创建全局变量(仅导出),因此必须在 required 时分配模块。

其次,将 -r-c 结合使用不会将 required 库添加到编译输出中。相反,它在编译期间需要它。实际上,-r 的存在只是为了让您可以扩展编译器本身——例如,向编译管道添加预处理器或后处理器——如 在 wiki 上有记录

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文