admin管理员组文章数量:1405539
While trying to learn Backbone.js, I've been trying to grab the content of a JSON file using the following code:
(function($){
var MyModel = Backbone.Model.extend();
var MyCollection = Backbone.Collection.extend({
model : MyModel,
url: '/backbone/data.json',
parse: function(response) {
console.log(response);
return response;
}
});
var stuff = new MyCollection;
console.log(stuff.fetch());
console.log(stuff.toJSON());
})(jQuery)
'stuff.fetch()' returns the entire object (with the data I'm after in responseText), 'stuff.toJSON' returns nothing ([]), but the console in the parse method is returning exactly what I want (the json object of my data).
I feel like I'm missing something obvious here, but I just can't seem to figure it out why I can't get the right data out. Could someone point me in the right direction or show me what I'm doing wrong here? Am I using a model for the wrong thing?
While trying to learn Backbone.js, I've been trying to grab the content of a JSON file using the following code:
(function($){
var MyModel = Backbone.Model.extend();
var MyCollection = Backbone.Collection.extend({
model : MyModel,
url: '/backbone/data.json',
parse: function(response) {
console.log(response);
return response;
}
});
var stuff = new MyCollection;
console.log(stuff.fetch());
console.log(stuff.toJSON());
})(jQuery)
'stuff.fetch()' returns the entire object (with the data I'm after in responseText), 'stuff.toJSON' returns nothing ([]), but the console in the parse method is returning exactly what I want (the json object of my data).
I feel like I'm missing something obvious here, but I just can't seem to figure it out why I can't get the right data out. Could someone point me in the right direction or show me what I'm doing wrong here? Am I using a model for the wrong thing?
Share asked Jun 7, 2012 at 4:29 BrianBrian 491 silver badge4 bronze badges1 Answer
Reset to default 6fetch
is a asynchronous call, so if you want to get the response, pass a success
callback into the arguments.
stuff.fetch({
success: function (collection, response) {
console.log(response);
}
})
More on Backbone.js Homepage
本文标签: javascriptBackbonejsGetting JSON back from urlStack Overflow
版权声明:本文标题:javascript - Backbone.js - Getting JSON back from url - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744911490a2631945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论