Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with backbone-relational.js #53

Open
timblack1 opened this issue Sep 10, 2012 · 1 comment
Open

Compatibility with backbone-relational.js #53

timblack1 opened this issue Sep 10, 2012 · 1 comment

Comments

@timblack1
Copy link
Contributor

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
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant