Created
April 27, 2011 03:48
-
-
Save mrjjwright/943684 to your computer and use it in GitHub Desktop.
Backbone playing with Dnode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load the remote RB db object, our connection to the backend SQLite or MongoDB | |
| DNode.connect (rbdb) -> | |
| RB.db = rbdb | |
| # Fetch the logged in Backbone user if any | |
| RB.user.fetch success: -> Backbone.history.loadUrl() | |
| # Overwrite the Backbone sync method to use Dnode and rbdb | |
| # The Recordbook database has a MongoDB compatible API, even if it sits on SQLite | |
| Backbone.sync = (method, model, success, error) -> | |
| collectionName = model.collectionName() | |
| # Backbone Models have a "hasChanged" function, distinguishing them from Collections | |
| isCollection = if model.hasChanged? then false else true | |
| # A generic response handler using Node style callbacks to invoke Backbone success/error handlers | |
| handleResponse = (err, obj) -> | |
| if err? then return error(err) | |
| return success(obj) | |
| switch method | |
| when "create" | |
| RB.db.insert collectionName, model.attributes().toJSON(), handleResponse | |
| when "read" | |
| findFunct = if isCollection then RB.db.find else RB.db.findOne | |
| findFunct.call RB.db, collectionName, {}, handleResponse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment