Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将主干初始数据加载到另一个js文件_Javascript_Django_Backbone.js_External - Fatal编程技术网

Javascript 如何将主干初始数据加载到另一个js文件

Javascript 如何将主干初始数据加载到另一个js文件,javascript,django,backbone.js,external,Javascript,Django,Backbone.js,External,这是我的django呈现测试页面 <!doctype html> <html lang="en"> <head> </head> <body> <div id="todo"> </div> <script type="text/template" id="item-template"> <div> <input id="todo_complete" type="checkbox"

这是我的django呈现测试页面

<!doctype html>
<html lang="en">
<head>
</head>
<body>
<div id="todo">
</div>
<script type="text/template" id="item-template">
<div>
  <input id="todo_complete" type="checkbox" <%= completed ? 'checked="checked"' : '' %>>
  <%- title %>
</div>
</script>
<script src="jquery.js"></script>
<script src="underscore.js"></script>
<script src="backbone.js"></script>

<script src="app.js"></script>
    //the app.js is my backbone app
<script >
    var foo_models = new fooCollection({{django_rendered_json_vaiable}})
    //this is how I get backbone collection initial data
</script>

</body>
</html>

//app.js是我的主干应用程序
var foo_models=newfoocollection({{django_rendered_json_vaiable}})
//这就是我获取主干收集初始数据的方式
上面的代码是我到目前为止得到的,我得到了作为foo_模型的主干引导模型, 但是这个变量不能在我的app.js文件中访问,然后我发现,它使用AMD方法,但我的应用程序相当小,我所需要的只是在另一个js文件中获取初始数据,所以我不想添加require.js,也不想使这个foo_models变量成为全局变量


那我该怎么做呢?

*编辑*

只需更改这些行:

<script src="app.js"></script>
    //the app.js is my backbone app
<script >
    var foo_models = new fooCollection({{django_rendered_json_vaiable}})
    //this is how I get backbone collection initial data
</script>

在你的应用程序中的某个地方(定义了
fooCollection
之后)。

如果不起作用,它会引发一个错误,说“fooCollection”是undeinedAh;我已经相应地修改了我的答案
<script >
    var raw_foo_models = {{django_rendered_json_vaiable}};
</script>
<script src="app.js"></script>
var foo_models = new fooCollection(raw_foo_models);