如何加载在Django骨干自举模式 [英] How to load bootstrapped models of backbone in Django

查看:83
本文介绍了如何加载在Django骨干自举模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载骨干应用程序时(我的应用程序就意味着页面加载),我需要的初步收集数据和骨干文档说:

when backbone app was loaded(for my app it means the page loaded),I need the initial collection data, and backbone docs says:

<script>
  var accounts = new Backbone.Collection;
  accounts.reset(<%= @accounts.to_json %>);
  var projects = new Backbone.Collection;
  projects.reset(<%= @projects.to_json(:collaborators => true) %>);
</script>

我不明白这一点。剂量是否意味着我应该呈现与初始数据的页面像

I don't understand this. Dose it mean I should render the page with the initial data like

{{collection_initial_data}}

我使用的后端Django的,所以我怎么以上code转化为Django模板,

I am using django on backends, so how do I translate the above code into django template, something like

 <script>
  var accounts = new Backbone.Collection;
  accounts.reset({{ @accounts.to_json }});
  var projects = new Backbone.Collection;
  projects.reset({{ @projects.to_json(:collaborators => true) }});
</script>

反正我真的很困惑越来越骨干收集的原始数据。

anyway I am really confused about getting the initial data of backbone collection.

推荐答案

在约引导骨干会谈,他们在谈论这样的:

When Backbone talks about bootstrapping, they're talking about this:

http://documentcloud.github.io/backbone/#FAQ-bootstrap

在换句话说,他们说你应该写出来你的模型的内容在你的Django HTML模板,一个脚本标签中。

In other words, they're saying you should write out the contents of your models in your Django HTML template, inside a script tag.

例如,假设你的Django上下文都有一个名为foo_models变量,它包含了所有的富模型的JSON重新presentation。你想要编辑您的Django模板做一些事情,如:

For instance, let's say your Django context has a variable called "foo_models", which contains the JSON representation of all of your foo models. You'd want to edit your Django template to do something like:

<script>
var foo_models = new FooCollection({{foo_models}});
</script>

在这种方式,你能保证你的数据准备好,因为它得到加载的页面上,你的JS code可以使用JS foo_models 访问数据。

In this way you can guarantee that your data is ready on the page as it gets loaded, and your JS code can use the JS foo_models to access that data.

现在,你的困惑似乎是如何使 foo_models 的Python / Django的变量摆在首位。我建议你​​查看逻辑这样做,没有模板,因为你可以使用Python和它的库的全功率序列化JSON。但是,如果你想这样做,在你看来,你可以用一个语法,它类似于你指定什么pretty这样做;检查出的第二个答案,这太问题:

Now, your confusion seems to be with how to make the foo_models Python/Django variable in the first place. I'd recommend doing this in your View logic, not in the template, as there you can use the full power of Python and its libraries to serialize the JSON. However, if you do want to do it in your view, you can do so with a syntax that's pretty similar to what you specified; check out the second answer to this SO question:

<一个href=\"http://stackoverflow.com/questions/3345076/django-parse-json-in-my-template-using-javascript\">Django: JSON解析用我的模板的Javascript

这篇关于如何加载在Django骨干自举模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆