1

我是 CoffeeScript 新手,遇到以下代码块的问题:

openShiftVariable = "foo"

class ServerEnvironment
    openShift: "OpenShift"
    cloud9: "Cloud9"
    environmentName: () -> 
        @openShift if openShiftVariable? else @cloud9 #fails
        #"#{@openShift}" if openShiftVariable? else "#{@cloud9}"
    constructor: () ->
        switch @environmentName()
            when @openShift
                console.log "OpenShift"
            when @cloud9
               console.log "Cloud9"

x = new ServerEnvironment()

也在:http: //jsfiddle.net/8NVqP/

我要做的是为 openShift 和 cloud9 定义一个常量,并在我的 switch 语句中使用它,以便我可以设置一些环境变量。我发现“environmentName”方法似乎不想返回字符串,我无法找到解决方法。

帮助表示赞赏!

编辑:2013 年 11 月 1 日

事实证明,Cloud9 在 OpenShift 中运行(doh!)这是上面的一个重新设计的版本,它可以工作:

class ServerEnvironment
   openShift: "OpenShift"
   cloud9: "Cloud9"
   environmentName: () ->
       if process.env.C9_PROJECT? then @cloud9 else @openShift
   constructor: () ->
       console.log "Detecting server environment..."
       @appPath = process.cwd()

       switch @environmentName()
           when @openShift
               console.log "OpenShift detected!"
               @redisURL = "the redis URL"
               @mongoURL = "the mongo URL"
               @nodeJSPort = (Number) process.env.OPENSHIFT_NODEJS_PORT
               @nodeJSIP = process.env.OPENSHIFT_NODEJS_IP
           when @cloud9
               console.log "Cloud9 detected!"
               @redisURL = "the redis URL"
               @mongoURL = "the mongo URL"
               @nodeJSPort = (Number) process.env.PORT
               @nodeJSIP = process.env.IP

global.ENV = new ServerEnvironment()
4

1 回答 1

1

改变:

@openShift if openShiftVariable? else @cloud9

为了:

if openShiftVariable? then @openShift else @cloud9
于 2013-11-01T01:00:10.680 回答