无法在主干中显示json内容

发布于 2024-12-20 12:21:26 字数 1785 浏览 0 评论 0原文

我的应用程序上一切运行良好,但是当我在浏览器控制台中手动添加对象时,我在 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;
           }

});

everything is working well on my app, but when i manually add the objects in the browsers console, i'm getting the json model data in the Get but it doesn't render on the web page.

I'm calling a notes view in a list view to render into a UL.

This is my html Element

<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>

This is the Backbone View Script.

 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;
           }

});

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

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

发布评论

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

评论(1

沒落の蓅哖 2024-12-27 12:21:26

您也应该发布您的 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
});

You should have posted your LibraryMynoteview as well. One thing I have found in your code is that your "mynote-template" does not contain any template tags.

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

This should be something like this:

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

Could be that you are currently rendering empty div elements in your page. Another thing I saw was that you append some ".edl" to your notes (should be .el):

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

Not sure if this is only a typo. Last but not least your code requires the DOM to be ready. So make sure you init your view within:

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