janmonschke / backbone-couchdb

A couchdb connector for backbone with support for real time changes.
http://janmonschke.com/projects/backbone-couchdb.html
358 stars 54 forks source link

Compatibility with backbone-relational.js #53

Open timblack1 opened 12 years ago

timblack1 commented 12 years ago

I'm trying to use backbone-relational (https://github.com/PaulUithol/Backbone-relational) with backbone-couchdb.js. I want to use backbone-couchdb.js's ability to automatically save model object changes to the database, but it does not work when I combine backbone-couchdb.js with backbone-relational.js. I think that somehow one is overwriting the other's changes to Backbone.Model. So I tried the following:

=== modified file '_attachments/script/lib/backbone-couchdb.js' --- _attachments/script/lib/backbone-couchdb.js 2012-09-10 21:56:57 +0000 +++ _attachments/script/lib/backbone-couchdb.js 2012-09-10 23:14:04 +0000 @@ -222,7 +222,7 @@ } };

-Backbone.Model = (function(_super) { +Backbone.RelationalModel = (function(_super) {

__extends(Model, _super);

@@ -248,7 +248,7 @@

return Model;

-})(Backbone.Model); +})(Backbone.RelationalModel);

Backbone.Collection = (function(_super) {

But the model objects still don't automatically save to the database.

Is there another way to get backbone-couchdb.js's automatic saving to work again?

timblack1 commented 11 years ago

I think I was confused before. Setting

    db:{
        changes:true
    }

on each model object updates it in the browser when it changes on the server, and setting

    initialize: function(){
        // Make models save themselves immediately when their attributes change
        Backbone.RelationalModel.prototype.initialize.apply(this, arguments);
        this.on("change", function (model, options) {
            if (options && options.save === false) return;
            model.save();
        });
    },

on each model object saves it to the database when it changes in the browser.

I'm still working on figuring out the best way to integrate backbone-relational with backbone-couchdb, but what works for me for now is that in backbone-couchdb.coffee, I replace Backbone.Model with Backbone.RelationalModel, then compile backbone-couchdb.coffee to backbone-couchdb.js, and use my new modified version. Then I load backbone-relational.js first, and backbone-couchdb.js second (using the shim config in RequireJS, so backbone-couchdb.js exports "Backbone" and I require backbone-couchdb.js to get Backbone into my modules). I'd prefer to monkey-patch backbone-couchdb.js in RequireJS's shim config, or rewrite both to be able to be compatible with each other, but haven't figured out how to do that yet.