admin管理员组文章数量:1353331
OK. I'm starting out on Backbone.js, and trying to do some very simple things. Here's a very simple Model and Collection.
// my model
Friend = Backbone.Model.extend({});
// my collection
Friends = Backbone.Collection.extend({});
// instantiate friends and add some friends objects!
var friends = new Friends();
friends.add([
{name: "James"},
{name: "Michael"}
]);
console.log(friends.length) // prints out 2! which is correct!
Above example is fine. But now I want to initialize my collection from server, which returns the exact same JSON object.
// my model
Friend = Backbone.Model.extend({});
// my collection
Friends = Backbone.Collection.extend({
url: 'http://localhost/myapp/get_users'
});
// instantiate friends and add some friends objects!
var friends = new Friends();
friends.fetch();
console.log(friends.length) // prints out 0! WHY???
I've been looking at my Chrome inspection panel for both of them and regardless, I have no idea why one from the server is not working?
FYI, on the server side, I have CodeIgniter 2.02, using Phil Stuegeon's REST API to return a JSON data.
I've also tried a simple function on my PHP side, like
function get_users() {
echo '{name:"James"}, {name:"Michael"}';
}
But without any success.
What am I doing wrong here?
OK. I'm starting out on Backbone.js, and trying to do some very simple things. Here's a very simple Model and Collection.
// my model
Friend = Backbone.Model.extend({});
// my collection
Friends = Backbone.Collection.extend({});
// instantiate friends and add some friends objects!
var friends = new Friends();
friends.add([
{name: "James"},
{name: "Michael"}
]);
console.log(friends.length) // prints out 2! which is correct!
Above example is fine. But now I want to initialize my collection from server, which returns the exact same JSON object.
// my model
Friend = Backbone.Model.extend({});
// my collection
Friends = Backbone.Collection.extend({
url: 'http://localhost/myapp/get_users'
});
// instantiate friends and add some friends objects!
var friends = new Friends();
friends.fetch();
console.log(friends.length) // prints out 0! WHY???
I've been looking at my Chrome inspection panel for both of them and regardless, I have no idea why one from the server is not working?
FYI, on the server side, I have CodeIgniter 2.02, using Phil Stuegeon's REST API to return a JSON data.
I've also tried a simple function on my PHP side, like
function get_users() {
echo '{name:"James"}, {name:"Michael"}';
}
But without any success.
What am I doing wrong here?
Share Improve this question asked Aug 13, 2011 at 14:16 ericbaeericbae 9,64426 gold badges77 silver badges109 bronze badges3 Answers
Reset to default 4Have you forgot the [] to make it an array?
echo '[{name:"James"}, {name:"Michael"}]';
Also you check the length in the onsuccess method you can pass to fetch. At the moment you check the length directly after fetching, so your not sure if the result is still loaded.
friends.fetch(
{success:function(){
console.log(friends.length)
}}
);
fetch() is an asynchronous call, so you'll be printing to the console before the fetch() has returned to populate your collection
To verify, put a breakpoint on the console.log(friends.length) line. The breakpoint will give the fetch() time to happen
I think the JSON you're passing to .add()
is invalid. You've got:
[
{name: "James"},
{name: "Michael"}
]
But you need to quote the keys too:
[
{"name": "James"},
{"name": "Michael"}
]
本文标签:
版权声明:本文标题:javascript - Printing Backbone.js collection through console.log(), what's wrong with this? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743901443a2558747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论