2013-10-18 43 views
4

我試圖執行使用RequireJS(2.1.8)一個基本的應用程序,WireJS(0.10.2)和PhantomJS(1.9.2)一RequireJS/WireJS應用:運行使用PhantomJS

  • 當使用PhantomJS運行應用程序(這是我的目標),WireJS無法加載(請參閱下面的錯誤)。
  • 當使用Chrome運行應用程序時,它會正確完成。

請幫忙指出缺少的一部分,WireJS下PhantomJS正常運行。

以下是我的應用程序文件。

1)app.html

<!DOCTYPE html> 

<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>SaphirJS.core</title> 
<script data-main="app" src="../../../target/deps/require-0.0.1/2.1.8/require.js"> </script> 
</head> 
<body> 
</body> 
</html> 

2)app.js

"use strict"; 

require.config({ 
    baseUrl: ".", 

    packages: [ 
     { name: 'wire', location: '../../../target/deps/wire-0.0.1/0.10.2', main: 'wire' }, 
     { name: 'when', location: '../../../target/deps/when-0.0.1/2.4.1', main: 'when' }, 
     { name: 'meld', location: '../../../target/deps/meld-0.0.1/1.3.0', main: 'meld' } 
    ] 
}); 

require(["wire!wireContext"], function(wireContext) { 
    alert(wireContext.message); 
}); 

3)wireContext.js

define({ 
    message: "Hello World!" 
}); 

4)幻像runner.js

(function() { 
    'use strict'; 

    var args = require('system').args, 
     timeoutRef = undefined, 
     timeLimit = 10000; 

    // arg[0]: scriptName, args[1...]: arguments 
    if (args.length !== 2) { 
     console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite]'); 
     phantom.exit(1); 
    } 

    var url = args[1], 
     page = require('webpage').create(); 

    // Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`) 
    page.onConsoleMessage = function(msg) { 
     console.log(msg); 
    }; 

    page.onInitialized = function() { 
     timeoutRef = setTimeout(function(){ 
      console.error('Test Run Failed. Timeout Exceeded. Took longer than '+ timeLimit/1000 +' seconds.'); 
      phantom.exit(1); 
     }, timeLimit); 
    }; 

    page.onAlert = function(message) { 
     clearTimeout(timeoutRef); 
     phantom.exit(0); 
    }; 

    page.open(url, function(status) { 
     if (status !== 'success') { 
      console.error('Unable to access network: ' + status); 
      phantom.exit(1); 
     } 
    }); 
})(); 

5)PhantomJS

TypeError: 'undefined' is not a function (evaluating 'Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty)') 

    <path-to-deps>/wire-0.0.1/0.10.2/lib/object.js:13 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:1635 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:871 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:1142 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:779 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:1169 in callGetModule 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:1529 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:1656 
Error: Load timeout for modules: wire!wireContext_unnormalized2 
http://requirejs.org/docs/errors.html#timeout 

    <path-to-deps>/require-0.0.1/2.1.8/require.js:138 in defaultOnError 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:536 in onError 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:691 in checkLoaded 
    <path-to-deps>/require-0.0.1/2.1.8/require.js:710 

Test Run Failed. Timeout Exceeded. Took longer than 10 seconds. 
+0

PhantomJS不支持 'Function.prototype.bind' 但新版本。這是什麼導致了失敗。 – Younes

回答

5

你是對的,尤尼斯下運行的應用程序時使用錯誤。出於某種原因,PhantomJS不支持Function.prototype.bind

您可以使用cujoJS/polykriskowal/es5-shim來填充Function.prototype.bind

+0

謝謝你的提示。接受答案並解決問題。 – Younes

1

正如在問題的評論中提到的,這個問題的根源在於PhantomJS沒有實現'Function.prototype.bind()'函數。

正如PhantomJS和WireJS的人所建議的那樣,這個問題可以通過ES5 polyfill來解決。 MDN建議的實施並沒有幫助,因爲它是規範的部分實施。包含在CujoJS/PolyJS中的實現已經解決了我的問題。現在,WireJS對PhantomJS很滿意。

以下是app.js

"use strict"; 

require.config({ 
    baseUrl: ".", 

    packages: [ 
     { name: 'wire', location: '../../../target/deps/wire-0.0.1/0.10.2', main: 'wire' }, 
     { name: 'when', location: '../../../target/deps/when-0.0.1/2.4.1', main: 'when' }, 
     { name: 'meld', location: '../../../target/deps/meld-0.0.1/1.3.0', main: 'meld' }, 
     { name: 'poly', location: '../../../target/deps/poly-0.0.1/0.5.2', main: 'poly' } 
    ] 
}); 

require(["poly/function", "wire!appWireContext"], function(poly, wireContext) { 
    alert(wireContext.message); 
}); 

乾杯