0

我有一个用于在本地存储数据的“LocalStore”对象。它基于一个 Lawnchair 对象。

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 _.extend(来自 Underscore.js 库):

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

此代码用于将 Backbone.js Collection 对象更新为 Lawnchair。第一次为我的用户集合运行“保存”时,它正确保存并显示该对象是一个简单的键/值对,其中值是一个数组。

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.

4

1 回答 1

1

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...

于 2012-07-16T18:23:36.003 回答