【问题标题】:Tumblr API / Dashes in property namesTumblr API / 属性名称中的破折号
【发布时间】:2013-12-02 21:46:54
【问题描述】:

我正在使用 Backbone 和 Marionette 来显示 tumblr 帖子列表。我想使用 JSON api 中可用的缩略图,但这给我带来了一个问题。我要获取的网址称为"photo-url-100": "http://blablablablabla"。通常在 JS 中我会使用parentObject['photo-url-100'],但是当我使用 Backbone / Marionette 遍历所有项目时,据我所知,没有可用的根节点(所有属性都是直接可用的,例如<%= id %>而不是<%= post.id %>。有人能指出我应该如何处理这个问题的正确方向吗?

一些澄清:我正在使用 MarionetteJS。根据 Tumblr 的文档,我已将他们的“API”(在我的情况下为 http://gentletruths.tumblr.com/api/read/json)作为脚本标签添加到我的标题中,有效地“引导”它。我将它作为一个集合处理,并将每个帖子作为一个模型。然后这个集合由Backbone.Marionette.CompositeView 处理,每个模型由Backbone.Marionette.ItemView 处理。所以在我的ItemView 模板(如下)中,id 有效(作为参考),但photo-url-100 无效。

<script id="instagram_item_template" type="text/template">
    <a href="#" class="photos__frame">
        <span class="caption"><%= id %></span>
        <img src="<%= photo-url-100 %>" alt="" class="photos__photo">
    </a>
</script>

我查阅了 Marionette 的带注释源(此处呈现函数:http://marionettejs.com/docs/backbone.marionette.html#section-188),但我不确定如何正确阅读并推断数据是如何可用的。

【问题讨论】:

  • 你能把你的代码中迭代完成的部分贴出来吗?

标签: json backbone.js tumblr marionette


【解决方案1】:

渲染_.template() 时,只需将parentObject 包裹在另一个对象中即可:

var compiled = _.template("<img id=\"<%= post.id %>\" src=\"<%= post['photo-url-100'] %>\">");
compiled({ 'post': parentObject });

编辑

您的模板:

<script id="instagram_item_template" type="text/template">
  <a href="#" class="photos__frame">
    <span class="caption"><%= post.id %></span>
    <img src="<%= post['photo-url-100'] %>" alt="" class="photos__photo">
  </a>
</script>

渲染模板:

Backbone.Marionette.ItemView.extend({
  tagName: "li",
  template: function(data){
    return _.template($('#instagram_item_template').html(), { 'post': data })
  }
});

【讨论】:

  • 我已经对我的问题进行了一些扩展以使事情更清楚,想再看看吗?
  • @Steven 你能贴出你用 tumblr 数据初始化集合的代码吗?
  • 我已经覆盖了模板函数,工作正常。这是我的 tumblr-data 初始化代码:gist.github.com/StevenLangbroek/7466b51e2db56ad898cb
猜你喜欢
  • 2021-03-26
  • 2012-08-31
  • 2013-02-04
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2021-04-03
相关资源
最近更新 更多