coffeescript中的命名空间 [英] namespaces in 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
pre>
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
似乎是我的一个好的解决方案。
解决方案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 namedmodel
. 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 ofthis
to the target object in a block, rather than compiling to the problematic and deprecatedwith
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屋!