alekseykulikov / backbone-offline

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

Collection is empty on fetching. Errors on POST request. #41

Closed ghost closed 11 years ago

ghost commented 11 years ago

When fetch() method runs, Backbone.Collection performs query to the RESTful service successfully, but eventually collection is empty (contains single empty model). Also following error occurs:

Uncaught TypeError: Converting circular structure to JSON backbone_offline.js:184

When I am trying run create() method of the collection, following errors occur:

Uncaught TypeError: Cannot read property '_listenerId' of undefined backbone.js:219
POST https://api.mongolab.com/api/1/databases/billing-app/collections/invoices/null?apiKey=[key] 405 (Method Not Allowed) jquery.js:8434

Normally my app uses URL https://api.mongolab.com/api/1/databases/billing-app/collections/invoices instead of https://api.mongolab.com/api/1/databases/billing-app/collections/invoices/null to perform CREATE operation.

I am using Backbone 1.0 and MongoLab.com as a RESTful service, _id is replaced to id. Here is my source code:

Backbone.Model.prototype.parse = function(resp, options) {
    if (_.isObject(resp._id))  {
      resp.id = resp._id.$oid;
      delete resp._id;
    }
    return resp;
  }

  Backbone.Model.prototype.toJSON = function() {
    var attrs = _.omit(this.attributes, 'id');
    if (!_.isUndefined(this.id))  {
      attrs._id = { $oid: this.id };
    }
    return attrs;
  }

  // Define configuration.
  var appConfig = {
    baseURL: 'https://api.mongolab.com/api/1/databases/billing-app/collections/',
    addURL: '?apiKey=[key]'
  }

  // Define invoice model.
  var InvoiceModel = Backbone.Model.extend({
    url: function() {
      if (_.isUndefined(this.id)) {
        return appConfig.baseURL + 'invoices' + appConfig.addURL;
      }
      else {
        return appConfig.baseURL + 'invoices/' + encodeURIComponent(this.id) + appConfig.addURL;
      }
    },
  });

  // Define invoice collection.
  var InvoiceCollection = Backbone.Collection.extend({
    model: InvoiceModel,
    url: function() {
      return appConfig.baseURL + 'invoices' + appConfig.addURL;
    },

    // Enable offline storage.
    initialize: function() {
      this.storage = new Offline.Storage('InvoiceCollectionStorage', this, {autoPush: true});
    }
  });
JonathanHindi commented 11 years ago

@ask11, I confirm that when first fetching, the GET request is fired probably, but the collection is empty in memory, and also some dummy empty models are created in localStorage.

I can't get the whole plugin to work for me because of that. Thank You.

0rca commented 11 years ago

Backbone 1.0 didn't work for me, too – I'm using 0.9.10 in my app because of that. Although, I never tried to investigate what caused the bug.

alemangui commented 11 years ago

I noticed this is caused due to a change on Backbone's fetch, save, create and destroy function signature on version 1.0.0 - which now only receive the response as parameter. I took a sneak peek on the code and noticed this should be fixed for 0.5.0, am I right?

paynecodes commented 11 years ago

I assumed this wasn't working with 0.5.0, but it's actually an old version of the compiled JS that made it into the 0.5.0 tag. Could @ask11 update this for us?

alekseykulikov commented 11 years ago

Done in #51