2013-03-09 109 views
1

我越来越:KO未定义

Uncaught TypeError: Cannot call method 'applyBindings' of undefined

我看着在Chrome网络选项卡,我看到knockout.js被加载。 我可以在控制台标签上输入window.ko,并看到淘汰赛的代码

那么,为什么错误?

config.js:

var require = { 
    baseUrl: "/scripts" 
}; 

main.js:

require([ 
    "lib/knockout" 
], 
function (ko) { 
    ko.applyBindings(); 
}); 

的index.html:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="scripts/config.js"></script> 
    <script data-main="app/main.js" src="scripts/lib/require.js"></script>  
</head> 
<body> 
    <input type="text" data-bind="value: test" /> 
</body> 
</html> 
+0

敲除的AMD/RequireJS支持依赖于条件['如果(typeof运算定义=== '功能' &&限定[ 'AMD'])'](https://github.com/SteveSanderson/knockout /blob/master/build/fragments/amd-pre.js)。如果以前的CommonJS运行时测试没有通过,它将默认定义全局'window.ko'。 – 2013-03-09 23:25:46

回答

1

尝试从回调签名删除ko参数。

+0

这有效......但为什么? – 2013-03-09 23:03:49

+0

您正在使用传递给回调函数的'ko'而不是预期的'window.ko' – andri 2013-03-09 23:06:33

+0

是的,但我经常用require()和define()看到这一点,它们在回调中的顺序与define中的顺序相匹配? – 2013-03-09 23:07:30

2

您应该在requirejs中查找垫片配置并导出ko符号。

+0

是的,也许这是+1的正确方法 – 2013-03-09 23:11:19