backbone.drowsy.encorelab
v0.2.6
Published
Backbone Model for use with DrowsyDromedary (MongoDB) as a backend
Downloads
1
Readme
Backbone.Drowsy
Backbone.js Model + Collection classes for use with the DrowsyDromedary REST interface for MongoDB.
Usage
var myDrowsyServer = new Drowsy.Server('http://localhost:9292');
var myDatabase = myDrowsyServer.database('example_db');
// extend a base Drowsy.Document (i.e. Backbone.Model) for your needs
var MyModel = myDatabase.Document('example_collection').extend({
foobar: function() {
return this.get('foo') * 2;
}
});
var doc = new MyModel();
doc.on('sync', function (d) {
console.log("Doc is: "+d.toJSON());
});
doc.set('blah', 'moo');
doc.save();
doc.foobar();
// extend a base Drowsy.Collection for your needs
var MyCollection = myDatabase.Collection('example_collection').extend({
model: MyModel
});
var coll = new MyCollection();
coll.fetch();
// you can also fetch the list of databases on a server
myDrowsyServer.databases(function (dbs) {
(dbs[0] instanceof Drowsy.Database) === true
});
// and fetch the list of collections in a database
myDatabase.collections(function (colls) {
(colls[0] instanceof Drowsy.Collection) == true
});
See the example and test directories for more details.
Authentication
For basic HTTP authentication, run the following code prior to sending out any requests:
var username = "foo";
var password = "bar";
Backbone.$.ajaxSetup({
beforeSend: function(xhr) {
return xhr.setRequestHeader('Authorization',
'Basic ' + btoa(username + ':' + password));
}
});
See the AJAX configuration options for jQuery or Zepto, dependin on which Backbone sync backend you're using.
Automatically Broadcasting Changes
Backbone.Drowsy can automatically broadcast changes to Documents and Collections
through WakefulWeasel (i.e. using Faye). To enable
Wakeful functionality, load the wakeful.js
script:
<script src="wakeful.js"></script>
Then call the Wakeful.wake()
method on a Drowsy.Document
or Drowsy.Collection
instance, like so:
var doc = new MyModel();
var weaselUrl = "http://localhost:7777/faye";
doc.wake(weaselUrl);
.wake()
imbues the Document or Collection with special Backbone.sync
behaviour that automatically broadcasts changes to a Document or Collection
to all other Documents or Collections using that URL.
For example, calling doc.save()
would automatically push doc
's state to
any other doc with the same URL (i.e. to other browsers/clients).
TODO: write some more usage examples
In the meantime, see https://github.com/zuk/Backbone.Drowsy/blob/master/test/wakeful.test.coffee#L320
Browser or Node.js?
Backbone.Drowsy should work both in a browser and under node.js.
Running Tests/Specs
With node, cd
into the Backbone.Drowsy directory, then install dependencies using:
npm install
You should now be able to run all tests in test/
by running the following in the root Backbone.Drowsy directory:
cake test
By default the tests run against http://localhost:9292
for DrowsyDromedary and http://localhost:7777/faye
for
WakefulWeasel.
To test against a different DrowsyDromedary and WakefulWeasel instance, set the DROWSY_URL and WEASEL_URL
environment variables, like this:
DROWSY_URL=http://drowsy.example.com WEASEL_URL=http://weasel.example.com/faye cake test
You can also run the tests in the browser:
First, make sure the Backbone.Drowsy directory is accessible via a web browser. For example, you can serve the directory contents with Ruby's WEBrick. Just run the following in the root Backbone.Drowsy directory:
ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"
Then point your browser to http://localhost:8000/test.