BackboneJs:如何在页面标记中引导我的数据,以及何时将它们分配给我的集合
所以, 构建一个使用多个(现在是 2 个)全局集合的应用程序, 它是文档和患者的目录,它们之间存在关系,但不像 1 个文档或属于 1 个患者的文档列表那样,所以它们实际上是 2 个独立的集合,
我的应用程序的结构非常相似的模块系统这里是如何描述的: http://weblog.bocoup.com/organizing-your-backbone -js-application-with-modules
backbone.js 文档提到了引导,要
<script>
Accounts.reset(<%= @accounts.to_json %>);
</script>
在 Rails 应用程序中执行类似的操作,但是我需要在 asp.net MVC3 中以不同的方式执行此操作,很可能我只会打印出我的 json 字符串,而无需 <%= %>这不是剃刀视图引擎风格),
但我的问题是,
这个 Accounts.reset(...data...);
只是浮动在我的标记中的某个位置,它不是以任何方式我的模块系统结构很好,有没有办法很好地做到这一点? 我可以从模块中的哪里获取数据?
还有另一个附带问题,假设我的主干应用程序中有一条路线 http://example.com/#documents
有人直接调用此链接,在执行路线本身之前,我的应用程序是否会按时准备好数据(来自引导程序)?
So,
building an application that uses multiple (2 for now) global collections,
it is a catalog of both documents and patients, they have relations, but not as in, 1 document or a list of documents belonging to 1 patient, so they are in fact 2 separate collections,
my app is structured in a module system very similar to how it is described here:
http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules
the backbone.js documentation says about bootstrapping, to do something like this,
<script>
Accounts.reset(<%= @accounts.to_json %>);
</script>
that is within a Rails application, i would however need to do it differently in asp.net MVC3, most likely i would just print out my json string without the <%= %> which isn't razor view engine style)
but my question here is,
this Accounts.reset(...data...);
is just floating somewhere in my markup, it is not in any way nicely structured in my module system, isn't there a way to nicely do this?
where as i can get the data, from within my module?
and another side question, suppose i have a route in my backbone app http://example.com/#documents
and someone calls this link directly, will my app have the data ready (from the bootstrap) on time, before the route itself is executed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我倾向于设置
application
对象 - 一个封装启动应用程序所需的所有代码的对象。然后,我将参数传入该对象的构造函数,以便应用程序对象可以使用它们。将预加载的 JSON 数据传递到应用程序对象中是我所做的事情之一,以保持代码的合理性和封装性。通常情况是这样的:
然后在需要启动应用程序的页面中,我这样做:
这当然是 Rails 版本。只需将
<%= ... %>
替换为 ASP.NET MVC 中 Razor 语法的版本即可。I tend to setup
application
objects - an object that encapsulates all of the code needed to start my application. I then take parameters into that object's constructor, so that they can be used by the app object. Passing pre-loaded JSON data into the app object is one of the things I do, to keep the code sane and encapsulated.It goes something like this, usually:
And then in my page that needs to start up the app, I do this:
That's the rails version of course. Just replace the
<%= ... %>
with your version for the Razor syntax in ASP.NET MVC.如果在 Rails 上:除了 Dericks 答案之外,您可能还想使用 Gon 来“将 Rails 变量获取到js”。
然后你会像这样初始化你的应用程序:
if on Rails: in addition to Dericks answer you might want to use Gon to "get your Rails variables in your js".
then you would initialize your app like that:
德里克就是一个很好的例子!对于 MVC 3+ 使用以下语法:
The excellent example from Derick! For MVC 3+ use this syntax: