简体   繁体   English

Coffeescript:无法在其他文件中实例化类的对象

[英]Coffeescript: Unable to instantiate object of a class in different file

I'm unable to instantiate (or call their methods) classes present in other files in my protractor codes in testing. 在测试中,我无法实例化(我的量角器)代码中其他文件中存在的类(或调用它们的方法)。 Here are my files 这是我的档案

Input.coffee 输入咖啡

   class exports.Input

      @element = null

        constructor: (@type, @selector, @identifier) ->
        if @selector is "id"
          @element = element(By.id(@identifier))
        if @selector is "class"
          @element = element(By.css(@identifier))
        console.log @element

      click: ->
        @element.click()

      putText: (inputText) ->
        @element.sendKeys(inputText)  

test.coffee 测试咖啡

describe 'Testing protractor modules', ->
  it 'Testing demo and experimentations', ->
# Initialize all the DOM elements which the suite covers 
    initialize = require('..\\utils\\initialize.coffee').Initialize("https://url.com")    

    inputText = require('..\\utils\\input.coffee').Input("text", "id", "user_email")
    inputPassword = require('..\\utils\\input.coffee').Input("text", "id", "user_password")

    console.log inputText # This is printing undefined
    inputText.putText("analytics@grs.com")
    inputPassword.putText("analytics123")

initialize.coffee initialize.coffee

class exports.Initialize
  constructor: (@url) ->
    browser.ignoreSynchronization = true
    browser.get @url
    console.log "Initialize constructor" # This works   

Error: Cannot read property of undefined. 错误:无法读取未定义的属性。

The problem here is cause the required CoffeeScript files aren't transpiled to JavaScript. 这里的问题是由于所需的CoffeeScript文件未转换为JavaScript。 If you include CoffeeScript compiler in your HTML page it'll only automatically compile <script type="text/coffeescript"> elements at the start. 如果您在HTML页面中包含CoffeeScript编译器,则它只会在开始时自动编译<script type="text/coffeescript">元素。

I'd basically stop using require to include CoffeeScript files. 我基本上会停止使用require来包含CoffeeScript文件。

Although there's a utility that concatenates various CoffeeScript files into a single one, here https://github.com/fairfieldt/coffeescript-concat (quoted in this post , this is why this is a duplicate question), so you can easily compile the single file to JavaScript. 虽然有这么串接各种CoffeeScript的文件合并成一个单一的一个,这里一个实用https://github.com/fairfieldt/coffeescript-concat (引自这个岗位 ,这就是为什么这是一个重复的问题),所以你可以很容易地编译单个文件转换为JavaScript。

If you still want something similiar to require to load CoffeeScript files you'd need to make an asynchronous or synchronous HTTP request for such file, get the content, then transpile it to JS code. 如果你仍然想类似的东西给require加载你需要为这样的文件同步或异步HTTP请求的CoffeeScript文件,获取内容,然后transpile它JS代码。

Synchronous HTTP requests are deprecated, so you can't make something similiar for require , you'd need a callback function to be executed later... or too late, after the request has been done and after the CoffeeScript was compiled. 不建议使用同步HTTP请求,因此您不能为require做类似的事情,您需要稍后执行回调函数,或者在请求完成之后和CoffeeScript编译之后执行回调函数。

You'll also need to evaluate the compiled CoffeeScript with Function or eval . 您还需要使用Functioneval评估编译的CoffeeScript。 eval executes the code in the current scope it has been called, while Function executes in the global scope. eval在已调用的当前范围内执行代码,而Function在全局范围内执行。

Example using asynchronous HTTP requests and eval to evaluate the compiled CoffeeScript (include the CoffeeScript compiler first): 使用异步HTTP请求和eval评估编译后的CoffeeScript的示例(首先包括CoffeeScript编译器):

(Note: this example doesn't avoid a script to cache, this is a bit broad) (注意:这个例子不会避免脚本被缓存,这有点宽泛)

function getCoffee(filePath, callback) {
    var request = new XMLHttpRequest;
    request.open('get', filePath, true);

    request.onreadystatechange = function() {
        if (request.readyState === 4) {

            if (request.status === 200) {
                var compiled = CoffeeScript.compile(request.responseText);

                if (typeof callback === 'function') {
                    callback(compiled);
                }
            }
            else {
                /* Handle request error here */
            }
        }
    };

    request.send();
}
getCoffee "script.coffee",
          (compiled) ->
            eval compiled;

Note: I don't know the way how your require works because I don't know Angular.js, so you may handle the exports manually. 注意:我不知道您的require如何工作,因为我不知道Angular.js,因此您可以手动处理导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM