【问题标题】:unable to display json content in backbone无法在主干中显示 json 内容
【发布时间】:2011-12-11 05:35:19
【问题描述】:

我的应用程序一切正常,但是当我在浏览器控制台中手动添加对象时,我在 Get 中获取了 json 模型数据,但它没有呈现在网页上。

我在列表视图中调用注释视图以呈现到 UL。

这是我的 html 元素

<html>

<head>

<title> Backbone Demo </title>
<%= stylesheet_link_tag    "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>


<script type="text/template" id="library-template">
<h1> My Notes List </h1>
<ul class="mynotes"></ul>

</script>


<script type="text/template" id="mynote-template">
<div class="mynote-title"> </div>
<div class="mynote-content"</div>
</script>


</head>
<body>

<div id="container">

</div>

</body>
</html>

这是主干视图脚本。

 window.LibraryView = Backbone.View.extend({
            tagName: 'section',
            className: 'mynotes',


           initialize: function() {
                  _.bindAll(this, 'render');
                  this.template = _.template($('#library-template').html());
                  this.collection.bind('reset', this.render);
           },

           render: function() {
                    var $mynotes,
                          collection = this.collection;

                    $(this.el).html(this.template({}));
                    $mynotes = this.$(".mynotes");
                    collection.each(function(mynote) {
                            var view = new LibraryMynoteview({
                                  model: mynote,
                                  collection: collection
                            });
                            $mynotes.append(view.render().edl);

                    });
                    return this;
           }

});

【问题讨论】:

  • 你能显示你的内容的格式吗?你的 json 数据是什么样子的?

标签: javascript ruby-on-rails json backbone.js


【解决方案1】:

您也应该发布您的 LibraryMynoteview。我在您的代码中发现的一件事是您的“mynote-template”不包含任何模板标签。

<script type="text/template" id="mynote-template">
  <div class="mynote-title"> </div>
  <div class="mynote-content"</div>
</script>

这应该是这样的:

<script type="text/template" id="mynote-template">
  <div class="mynote-title">{{title}}</div>
  <div class="mynote-content">{{content}}</div>
</script>

可能是您当前正在页面中呈现空的 div 元素。我看到的另一件事是你在笔记中附加了一些“.edl”(应该是.el):

$mynotes.append(view.render().edl);

不确定这是否只是一个错字。最后但并非最不重要的一点是,您的代码需要准备好 DOM。因此,请确保您在以下范围内初始化视图:

$(function(){
  // init here
});

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 2015-10-06
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2017-07-16
    • 2013-06-22
    • 2017-07-11
    • 2023-01-10
    相关资源
    最近更新 更多