无法在主干中显示json内容
我的应用程序上一切运行良好,但是当我在浏览器控制台中手动添加对象时,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也应该发布您的 LibraryMynoteview。我在您的代码中发现的一件事是您的“mynote-template”不包含任何模板标签。
这应该是这样的:
可能是您当前正在页面中渲染空的 div 元素。我看到的另一件事是你在笔记中附加了一些“.edl”(应该是.el):
不确定这是否只是一个拼写错误。最后但并非最不重要的一点是,您的代码需要准备好 DOM。因此,请确保您在以下范围内初始化视图:
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.
This should be something like this:
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):
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: