Javascript Backbone.js-can';t获取数据,或绑定到收集时的获取事件?
我使用的是jQuery 1.7.1、下划线1.3.1和主干0.9.1。这是我的主干代码,全文如下:Javascript Backbone.js-can';t获取数据,或绑定到收集时的获取事件?,javascript,backbone.js,Javascript,Backbone.js,我使用的是jQuery 1.7.1、下划线1.3.1和主干0.9.1。这是我的主干代码,全文如下: $(function(){ window.Student = Backbone.Model.extend({ }); window.Students = Backbone.Collection.extend({ model: Student, }); window.AllStudents = new Students(); AllStudents.url = "/
$(function(){
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.url = "/init.json";
AllStudents.bind('reset', function() {
console.log('hello world');
});
AllStudents.fetch();
AllStudents.fetch({ url: "/init.json", success: function() {
console.log(AllStudents);
}, failure: function() {
console.log('failure');
}});
AllStudents.fetch({ url: "/init.json" }).complete(function() {
console.log(AllStudents);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div class="container"></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.1/backbone-min.js"></script>
<script src="/js/test.js"></script>
</body>
</html>
在第三个.fetch()
调用中,甚至只出现一个控制台语句,然后它就是一个空对象
我很困惑。我做错了什么?如何绑定到重置事件,并处理所获取的数据
这是JSON文件:
[
{ text: "Amy", freq_2011: 5 },
{ text: "Angeline", freq_2011: 26 },
{ text: "Anna", freq_2011: 55 }
]
我已经检查了JSON文件是否被用作application/JSON,我还可以看到它被XHR请求提取了三次
更新:这是我的HTML,全文如下:
$(function(){
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.url = "/init.json";
AllStudents.bind('reset', function() {
console.log('hello world');
});
AllStudents.fetch();
AllStudents.fetch({ url: "/init.json", success: function() {
console.log(AllStudents);
}, failure: function() {
console.log('failure');
}});
AllStudents.fetch({ url: "/init.json" }).complete(function() {
console.log(AllStudents);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div class="container"></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.1/backbone-min.js"></script>
<script src="/js/test.js"></script>
</body>
</html>
试验
其他人能重现这个问题吗?主干网有一个很好的函数,它可以被重写调用,一旦调用成功的获取,它就会将获取的数据作为参数。加载JSON后,您可以使用它来处理JSON,例如:
window.Students = Backbone.Collection.extend({
model: Student,
parse: function(response) {
console.log(response,response.results);
return response.results;
}
});
您是否尝试过在集合本身的定义中设置url参数?是的,没有帮助:(请其他人尝试一下代码,看看他们是否可以重现问题?我最终通过使用引导模型解决了这个问题:不过,我仍然完全不知道出了什么问题。