在 Rails、Backbone 和 Mustache 中使用 JSON - 格式似乎不同

发布于 2024-11-17 18:36:06 字数 1640 浏览 2 评论 0原文

我正在尝试将 Rails 应用程序与backbone.js 和 Mustache 模板组合在一起。我发现主干所需的 JSON 与 Mustache 所需的 JSON 不兼容。 (我开始遵循本教程 Cloudedit -abackbone.js 教程示例,但我想在他使用 JST 的地方使用 Mustache。

对于主干,我们必须为我的模型设置 ActiveRecord::Base.include_root_in_json = false。名字和姓氏)rails 从 /people 发送的数据如下所示:

[{"firstname":"Jane","surname":"Jones"},{"firstname":"Janet","surname":"Jensen"}]

我的小胡子模板如下所示:

<h3><a href='#new'>Create New</a></h3>
<h4>People</h4>
<ul>
{{#people}}
<li>{{firstname}} {{surname}}</li>
{{/people}}
</ul>

从小胡子文档中我期望它想要看到的是

{"people":[{"firstname":"Jane","surname":"Jones"},{"firstname":"Janet","surname":"Jensen"}]}

编辑掉我将 JS 集合转换回 JSON并发送给小胡子。 不需要这样做。 Mustache.js 期望看到 js 对象,而不是 JSON 字符串。

当我到达 Mustache.to_html 时,我已经有了一个模型的主干集合。这些模型具有属性名字姓氏。在 firebug 中看起来像这样:

collection
  +_byCid
  +_byId
   length 2
  - models  [object { attributes=(...), more...}, object {attributes=(...), more...}]
    - 0 object { attributes=(...), more...}
      ....... some more properties of the object
        + attributes object {firstname="Janet", surname="Jensen"}

这里似乎有几个问题。 1. 没有提及藏品名称(人物)。我想我可以通过多种方式解决这个问题,至少可以在模板中使用 {{#models}}..{{/models}} 。

  1. 这些属性比 Mustache.js 看起来更深。当它开始尝试在对象中查找“firstname”标签时,它会查找 object['firstname'] 但没有找到它,但 object.attributes['firstname'] 具有正确的值。

看来我在这里很困惑......那么我做错了什么?我该如何解决它?

I am trying to put together a Rails app, with backbone.js and mustache templating. I am finding that the JSON required by backbone isn't compatible with the JSON required by Mustache. (I started out following this tutorial Cloudedit -a backbone.js tutorial by example, but I want to use Mustache where he is using JST.

For backbone we must set ActiveRecord::Base.include_root_in_json = false. For my model (person with firstname and surname) the data sent by rails from /people looks like this:

[{"firstname":"Jane","surname":"Jones"},{"firstname":"Janet","surname":"Jensen"}]

My mustache template looks like this:

<h3><a href='#new'>Create New</a></h3>
<h4>People</h4>
<ul>
{{#people}}
<li>{{firstname}} {{surname}}</li>
{{/people}}
</ul>

and from the mustache docs I expect that what it wants to see is

{"people":[{"firstname":"Jane","surname":"Jones"},{"firstname":"Janet","surname":"Jensen"}]}

Edited out me transforming the JS collection back to JSON and sending to Mustache.
Don't need to do that. Mustache.js expects to see js objects, not strings of JSON.

By the time I get to Mustache.to_html I have a backbone collection of models. The models have the attributes firstname and surname. It looks like this in firebug:

collection
  +_byCid
  +_byId
   length 2
  - models  [object { attributes=(...), more...}, object {attributes=(...), more...}]
    - 0 object { attributes=(...), more...}
      ....... some more properties of the object
        + attributes object {firstname="Janet", surname="Jensen"}

There seem to be a couple of problems here.
1. There is no mention of the name of the collection (people). I can get around that in various ways I guess, at least by using {{#models}}..{{/models}} in the template.

  1. The attributes are buried deeper than Mustache.js looks. When it gets down to trying to find the 'firstname' tag in the object, it looks for object['firstname'] and doesn't find it, but object.attributes['firstname'] has the correct value.

It seems I'm all mixed up here....So what am I doing wrong? And how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2024-11-24 18:36:06

我现在有了某种解决方案(感谢 这个问题)。

jsonForTemplate = JSON.parse(JSON.stringify(this.people)); //this works for a single item
context['people']=jsonForTemplate;    //need to add this for a collection
out = Mustache.to_html(tpl,context);

但感觉这种跳圈似乎没有必要。我会检查车把是否提供更好的东西。

I now have a solution of a sort (thanks to this question).

jsonForTemplate = JSON.parse(JSON.stringify(this.people)); //this works for a single item
context['people']=jsonForTemplate;    //need to add this for a collection
out = Mustache.to_html(tpl,context);

but it feels as though this sort of jumping-through-hoops shouldn't be necessary. I will check to see if handlebars offers anything better.

蓝眸 2024-11-24 18:36:06

一个更简单的解决方案是在集合上使用 toJSON,如此处所述 - http://documentcloud.github.com/backbone/#Collection-toJSON

out = Mustache.to_html(tpl, collection.toJSON);

A simpler solution would be to use toJSON on the Collection as documented here - http://documentcloud.github.com/backbone/#Collection-toJSON

out = Mustache.to_html(tpl, collection.toJSON);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文