从外部JSON中读取的主干脚本
Backbone script to read from external JSON
我想使用主干脚本显示JSON中的数据。
这是html页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="backbone.js"></script>
<script src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var Profile = Backbone.Model.extend();
var ProfileList = Backbone.Collection.extend({
model: Profile,
url: 'document.json'
});
var ProfileView = Backbone.View.extend({
el: "#profiles",
template: _.template($('#profileTemplate').html()),
initialize: function(){
this.listenTo(this.collection,"add", this.renderItem);
},
render: function () {
this.collection.each(function(model){
var profileTemplate = this.template(model.toJSON());
this.$el.append(profileTemplate);
}, this);
return this;
},
renderItem: function(profile) {
var profileTemplate = this.template(profile.toJSON());
this.$el.append(profileTemplate);
}
});
var profileList = new ProfileList();
var profilesView = new ProfileView({ collection: profileList });
profilesView.render();
});
</script>
</head>
<body>
<div id="profiles"></div>
<script id="profileTemplate" type="text/template">
<div class="profile">
<div class="info">
<div class="name">
<%= name %>
</div>
<div class="title">
<%= title %>
</div>
<div class="background">
<%= background %>
</div>
</div>
</div>
<br />
</script>
</body>
</html>
这是我的json数据:document.json
[
{
"id": "p1",
"name" : "AAAA",
"title" : "BBBB",
"background" : "CCCC"
},
{
"id": "p2",
"name" : "DDDD",
"title" : "EEEE",
"background" : "FFFF"
},
{
"id": "p3",
"name" : "GGGG",
"title" : "HHHH",
"background" : "IIII"
}
]
页面上没有显示任何内容。
我错过什么了吗?我一直试图在指定的HTML中的div元素上打印JSON数据。但是什么也没有显示。我试着调试代码,我可以在Backbone.Model之前插入警告语句。在文档中扩展。jquery中的ready但是当我把alert语句放进去的时候,它并没有出现。因此,Backbone.Model中的代码。extend一定是哪里出错了。谁来帮帮我?
你需要检查你的脚本加载顺序,jquery应该是第一位的,下划线其次(因为你使用的是uncore .js模板),然后骨干:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
如果这不能解决你的问题,那么你可以尝试发送一个静态引用到你的初始数据集,然后根据需要调用fetch()来获取更多的数据,或者你可以显式地调用fetch,如下所示:
var profileList = new ProfileList();
var profilesView = new ProfileView({collection: profileList});
profileList.fetch();
profileList.bind('reset', function () {
profilesView.render();
});
演示工作你应该按顺序加载脚本,jquery,下划线,然后主干
相关文章:
- 在JS或jQuery或任何客户端脚本的帮助下,通过查看源代码读取源代码
- Google 表格脚本 - “无法读取未定义的属性”(如果不是)
- Javascript:是否可以读取外部脚本的原始代码
- 获取 无法读取未定义的引导轮播脚本的属性“偏移宽度”
- zip.js读取zip中的文件名.(仅限客户端脚本)
- PhoneGap脚本可以正确读取联系人,直到我包含照片(头像)
- 如何读取<脚本>标签
- Java脚本将URL的响应读取到变量中
- 在应用脚本中读取哈希图
- 如何使用java脚本读取和解析文件
- 从 html 读取输入电子邮件地址并在 JSP 脚本中使用
- Ajax读取PHP脚本而不是JSON输出
- 使用 Java 脚本读取 HTML 表中输入标记中的值
- 未捕获的类型错误:无法读取未定义的属性“indexOf”(观察者脚本.js)
- txt文件的脚本读取列
- 无法从动态加载的脚本读取DOM属性
- 用Java脚本读取JSON文件
- 在Javascript中从脚本读取JSON
- 如何从javascript post调用python脚本读取数据
- 尝试从html页面和java脚本读取服务器上的文件