coffeescript中的命名空间 [英] namespaces in coffeescript

查看:174
本文介绍了coffeescript中的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用命名空间作为一个在javascript中使用关键字with,但CoffeeScript报告这是一个保留的关键字,拒绝编译是否有任何方式,我可以使用命名空间在cs?



特别是,我想动态包含一个CoffeeScript文件(可信源),比如为数据库模式加载模型,但是我想让包含的脚本访问本地命名空间。 / p>




编辑



我想做什么。我设置了一个web框架,将目录树映射到基于express和mongoose的应用程序。例如,有一个子目录models包含一个文件'user.coffee',代码如下:

  name:
type:String
unique:on
个人资料:[个人资料]


$ b b

其中配置文件是位于名为 model 的本地对象中的类。当用户模型正在加载时,我想要它访问位于我的本地模型存储中的模型类。



我现在的工作是将 model.Profile 写入文件'user.coffee'。希望这很清楚我的意思。






第二编辑 b

以下是我使用与 c>

结合使用的方法:



user.coffee



  name:
type:String
unique:on
个人资料:[@profile]

profile.coffee

  content:String 

以下是动态加载方式:

  for fs.readdirSync#{base} / models
m = path.basename fm,'.coffee'
schema [m] =(() - >
new Schema coffee.eval(
fs.readFileSync#{base} / models /#{fm},'utf8'
) bare:on
).call模型
mongoose.model m,schema [m]
model [m] = mongoose.model m
pre>

似乎是我的一个好的解决方案。

解决方案

fork的CoffeeScript支持语法;请参阅 https://github.com/satyr/coco/wiki/additions 。但是,该语法只是将这个的值设置到块中的目标对象,而不是编译到有问题的和已弃用的关键字。



假设你想在CoffeeScript中模仿Coco的语法。你会这样做:

  withObj =(obj,func) - > func.call obj 

然后你可以写

  withObj =(obj,func) - > func.call obj 
withObj annoyingly.lengthy.obj.reference, - >
@foo ='bar'
@bar ='baz'

当然,在这种简单的情况下,最好使用一个效用函数像jQuery或Underscore的 extend

  _。extend annoyingly.lengthy.obj.reference,foo:'bar',bar:'baz'


I'd like to use namespaces as one would in javascript by using the keyword "with", but CoffeeScript reports this as a reserved keyword and refuses to compile is there any way I could use namespaces in cs?

In particular, I want to include a CoffeeScript file dynamically (trusted source), like loading models for a database schema, but I want the included script to have access to a local namespace.


Edit:

Here's what I want to do. I am setting up a web framework that maps the directory tree to an application based on express and mongoose. For example, there's a sub-directory 'models' that contains a file 'user.coffee' with code like this inside:

name:
    type: String
    unique: on
profiles: [ Profile ]

Whereby Profile is a class that sits in a local object named model. When the user model is being loaded, I wanted it to access the model classes that sit in my local model store.

My work-around for now was to write model.Profile into the file 'user.coffee'. Hope it is clear what I mean.


2nd Edit

Here's how I did it without using with:

user.coffee

name:
    type: String
    unique: on
profiles: [ @profile ]

profile.coffee

content: String

And here's how it's dynamically loaded:

for fm in fs.readdirSync "#{base}/models"
    m = path.basename fm, '.coffee'
    schema[m] = (()->
        new Schema coffee.eval (
            fs.readFileSync "#{base}/models/#{fm}", 'utf8'
        ), bare: on
    ).call model
    mongoose.model m, schema[m]
    model[m] = mongoose.model m

Seems an okay solution to me.

解决方案

The Coco fork of CoffeeScript supports a with syntax; see https://github.com/satyr/coco/wiki/additions. However, that syntax simply sets the value of this to the target object in a block, rather than compiling to the problematic and deprecated with keyword.

Let's say that you wanted to emulate Coco's with syntax in CoffeeScript. You'd do something like this:

withObj = (obj, func) -> func.call obj

Then you could write

withObj = (obj, func) -> func.call obj
withObj annoyingly.lengthy.obj.reference, ->
  @foo = 'bar'
  @bar = 'baz'

Of course, in such simple cases, it's better to use a utility function like jQuery or Underscore's extend:

_.extend annoyingly.lengthy.obj.reference, foo: 'bar', bar: 'baz'

这篇关于coffeescript中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆