0

Tastypie provides a RESTful API for django projects so I can use Backbone.js. When I hit the url to get a collection of resources, tastypie includes data about pagination, which I am unable to access. I have a Backbone View where I initialize a collection in the initialize function, then render:

MyView = Backbone.View.extend({
  ...
  initialize: function() {
     this.collection = new MyCollection;
     this.render();
  }
  ...
  render: function() {
     console.log(this.collection); // this.colllection.toJSON() returns []
     console.log(this.model);  // this.model.toJSON() returns the object
  }
});

The link for the next page is contained in the meta attribute of this.collection, but I cannot access it. Calling toJSON() on the collection returns []. The issue is that the console.log(this.collection) gives this:

> child
_byCid: Object
_byId: Object
_callbacks: Object
length: 3
meta: Object
models: Array[3]
toJSON: function (key) {
__proto__: ctor

The url I want is inside the meta attribute of this.collection (so I can see it!), but I can't access it. Calling toJSON works on the model, but not the collection. How I can access the attributes of the collection?

1 Answers1

1

Can be as simple as this.collection.meta?

Update

Also you have to use console.log carefully and don't trust on it when you are debugging not simple objects, check:

In your code try:

this.collection.fetch({
  success: function( collection ) { 
    console.log( "collection.meta", collection.meta ) 
  } 
});
Community
  • 1
  • 1
fguillen
  • 36,125
  • 23
  • 149
  • 210
  • I was hoping so, but that gives me undefined. – Rahul Pandey Aug 16 '12 at 15:43
  • I think your Collection is still not fetched when you try my solution. Try `this.collection.fetch({success: function( collection ) { console.log( "collection.meta", collection.meta ) } })`. You are trusting in `console.log` and you shouldn't: http://stackoverflow.com/questions/11459244/backbone-js-empty-array-attribute/11463190#11463190 | http://stackoverflow.com/questions/9911637/backbone-js-model-get-returning-undefined-even-though-i-can-see-the-attribut – fguillen Aug 16 '12 at 16:10
  • You're exactly right. Inside of the fetch, I'm able to access the attributes. I will be more careful with console. If you want to add another answer about this, I can accept. – Rahul Pandey Aug 18 '12 at 08:19
  • @user875700 I've just updated this answer with the information that finally was helpful for you. – fguillen Aug 18 '12 at 08:31