Code Monkey home page Code Monkey logo

backbone.drowsy's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.