简体   繁体   English

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

[英]how to load backbone initial data to another js file

here is my django rendered test page 这是我的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>

the above code is what I get so far, I get the backbone bootstrapped models as foo_models, but this variable can't be access in my app.js file , then I found this question , which use AMD approach,but my app is fairly small,all I need is get the initial data in another js file,so I don't want to add require.js and I don't want to make this foo_models variable global。 上面的代码是我所得到的,到目前为止,我得到的骨干自举模式为foo_models,但这个变量不能在我的app.js文件访问 ,后来我发现这个问题 ,它使用AMD的做法,但我的应用程序是相当很小,我所需要的只是在另一个js文件中获取初始数据,因此我不想添加require.js,也不想将此foo_models变量设置为全局变量。

so how can I do that? 那我该怎么办呢?

* EDITED * *编辑*

Just change these lines: 只需更改这些行:

<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>

to: 至:

<script >
    var raw_foo_models = {{django_rendered_json_vaiable}};
</script>
<script src="app.js"></script>

Then, add this line: 然后,添加以下行:

var foo_models = new fooCollection(raw_foo_models);

somewhere in your app (after fooCollection has been defined). 应用程序中的某个位置(定义了fooCollection之后)。

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

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