无法弄清楚Django,knockout和requirejs如何互相振奋。我尝试的一切都得到'ReferenceError:ko未定义'或类似的消息。我当前的代码看起来像这样:
base.html文件:
<script src="{{ STATIC_URL }}js/require.js"></script>
<script type="text/javascript">
var SiteConfig = { baseStaticUrl: "{{ STATIC_URL }}" };
require(['{{ STATIC_URL }}js/require_config.js'], function (require_config) {
{% block require_javascript %}
{% endblock require_javascript %}
});
</script>
require_config.js:
require.config({
"baseUrl": SiteConfig.baseStaticUrl,
"paths": {
'jquery': SiteConfig.baseStaticUrl + 'js/jquery-1.11.0.min',
'knockout': SiteConfig.baseStaticUrl + 'js/knockout-3.3.0',
'bootstrap': SiteConfig.baseStaticUrl + 'bootstrap/js/bootstrap.min',
'underscore': SiteConfig.baseStaticUrl + 'js/underscore-1.8.3.min'
},
"shim": {
'jquery': {
'exports': '$'
},
'knockout': {
'exports': 'ko'
},
'bootstrap': {
'deps': ['jquery']
},
'underscore': {
'deps': ['jquery'],
'exports': '_'
}
}
});
require(
['jquery', 'knockout', 'bootstrap', 'underscore']
);
page.html中:
{% block require_javascript %}
require.config({
"paths": {
'flot': '{{STATIC_URL}}flot/jquery.flot.min',
'multiselect': '{{STATIC_URL}}js/bootstrap-multiselect'
'mything': '{{STATIC_URL}}mything/js/api',
'page': '{{STATIC_URL}}patientplans/js/page'
},
"shim": {
'page': {
'deps': ['flot', 'multiselect', 'mything']
}
}
});
require(['flot', 'multiselect', 'mything', 'page']);
{% endblock require_javascript %}
page.js:
// Some knockout stuff here
...
$(document).ready(function(){
require(['knockout', 'jquery', 'js/MyViewModel'], function(ko, $, MyViewModel) {
ko.applyBindings(new MyViewModel());
});
});
MyViewModel.js:
define(['knockout', 'jquery'], function(ko, $) {
return function MyViewModel(){
var self = this;
...
}
});
现在,我的想法是可以通过全局变量ko
访问淘汰赛,但无论我尝试什么,这都不会出现在window
中。
朝正确的方向推进会很棒。