Search code examples
javascriptlawnchair

Lawnchair.js not updating data


I've got a "LocalStore" object for storing data locally. It's based around a Lawnchair object.

var LocalStore = function(name) {
    var that = this;
    that.name = name;

    that.lawnchair = Lawnchair({ name: that.name }, function(store) {
        this.before('save', function(record){
            console.log("saving " + that.name);
            console.log(record);
        });

        this.after('save', function(record){
            console.log("saved " + that.name);
            console.log(record);
            that.getData(function(records){
                console.log("now it's this");
                console.log(records);
            });
        });
    });

    that.getData = function(callback) {
        that.lawnchair.get(that.name, callback);
    };
};

LocalStore is then extended with _.extend(from the Underscore.js library) with this method:

save: function(collection, callback) {
        this.lawnchair.save({ key:this.name, value: collection }, function(record) {
            callback(record);
        });
    }

This code is used to update a Backbone.js Collection object to Lawnchair. The first time "save" runs for my Users Collection it saves correctly and shows that the object is a simple key/value pair where value is an Array.

Later in my code when a User selects a Default Project, I modify the Users Collection and call "save" again with an updated "defaultProjectId" on the User. The code runs error free, but the after('save') code for Lawnchair runs and shows me that:
- The record object returned is a key/value pair where value is a full Backbone.js Collection with the defaultProjectId property set correctly.
- The getData method that grabs the latest from the Database still shows as a key/value pair with value a simple Array and defaultProjectId is set incorrectly.

I'm at a loss as what to do. It should just be simply calling "lawnchair.save" updates the record, but it just doesn't do it.


Solution

  • Could you try this jsfiddle?

    http://jsfiddle.net/QUgtg/1/

    I have recreated your code. Instead of a backbone collection, I am passing in an array of objects. This seems to work. You can see the logging output in Firebug.

    I have used my own extend code to add the save(). Though honestly, I don't see why you would want to do it that way, instead of just adding a property to the prototype. Your code may differ in that aspect.

    If what I have posted works on your end, could you modify that code to show what are you doing differently? If possible, recreate the issue on jsfiddle...