0

这是我的 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>

上面的代码是我到目前为止得到的,我将主干引导模型作为 foo_models,但是在我的 app.js 文件中无法访问这个变量,然后我发现了这个问题,它使用 AMD 方法,但我的应用程序相当小,我只需要在另一个js文件中获取初始数据,所以我不想添加require.js,我不想让这个foo_models变量成为全局变量。</p>

那我该怎么做呢?

4

1 回答 1

0

* 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).

于 2013-04-25T16:24:00.643 回答