alekseykulikov / backbone-offline

[Deprecated] Allows your Backbone.js app to work offline
MIT License
720 stars 56 forks source link

Add Django TastyPie support #32

Open iturpablo opened 11 years ago

iturpablo commented 11 years ago

I found a problem when working with Django-TastyPie, it doesn't store the objects in DB.

The json produced by tasty includes a meta with information and put the information inside the objects tag (example bellow):

{ "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 3 }, "objects": [{ "body": "Welcome to my blog!", "id": "1", "pub_date": "2011-05-20T00:46:38", "resource_uri": "/api/v1/entry/1/", "slug": "first-post", "title": "First Post", "user": "/api/v1/user/1/" }, So in order to make backbone offline work, I have to do this hack:

Linea ->279 Sync.prototype.full = function(options) { var this = this; if (options == null) { options = {}; } return this.ajax('read', this.collection.items, .extend({}, options, { success: function(response, status, xhr) { var item, _i, _len; //Pablos hack _this.storage.clear(); _this.collection.items.reset([], { silent: true }); for (_i = 0, _len = response.objects.length; _i < _len; _i++) {//how is was: for (_i = 0, _len = response.length; _i < _len; _i++) item = response.objects[_i]; // How it was: item = response[_i]; _this.collection.items.create(item, { silent: true, local: true, regenerateId: true }); } if (!options.silent) { _this.collection.items.trigger('reset'); } if (options.success) { return options.success(response); } } })); }; Linea -> 308

What do you think about this problem.

Congrats on such a great library.

dendrochronology commented 11 years ago

Pablo,

I'm pretty sure this doesn't have anything to do with backbone.offline. If I recall correctly, backbone's native collection parse method only works if you give it an array of objects. It seems that TastyPie is returning an object with an attribute with an array of objects; it's nested one level too deep, and backbone doesn't understand where to find the data.

I ran into a similar problem using backbone with Google's Feed API. I had to override the parse method for my collection, telling it to return response.entries, which was the actual array of results.

Try adding this to your collection:

    parse : function(response){
        return response.objects;  
   } 

It's pretty easy to override the collection parse method to match whatever data structure you're dealing with. For example, if there are other tasty bits in the meta object of your results, you could deal with that in parse as well, just be sure you return a valid results array in the end.

iturpablo commented 11 years ago

Hi Aaron, Thanks !! for the reply.

Try your suggestion and debug it but the parser is executed after backbone offline stores it in local storage.. any ways I don't want to loose my meta data information witch used for pagination.

dendrochronology commented 11 years ago

Hmm. Methinks we'll need input from the author, on how he expects that processing chain to work.

@Ask11 - what are your thoughts?

alekseykulikov commented 11 years ago

Hi guys, backbone.offline is REST-centric plugin, as a default Backbone.sync too. Everything in your app should works as a classic REST. Solution with parse is good to parse response, but meta information have to store separately, maybe @iturpablo should create another collection for that or handle it by hands.