Permalink
Browse files

init commit

  • Loading branch information...
mkuklis committed Oct 2, 2011
0 parents commit 817812d07400053d478972454e12239d408dbac5
Showing with 253 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. 0 README
  3. +29 −0 backbone-nowjs.coffee
  4. +16 −0 config/config.js
  5. +16 −0 package.json
  6. +54 −0 public/backbone-nowjs.js
  7. +64 −0 server.js
  8. 0 views/index.html
  9. +73 −0 views/layout.html
@@ -0,0 +1 @@
+node_modules
0 README
No changes.
@@ -0,0 +1,29 @@
+Backbone.nowjsConnector =
+ extractName: (model) ->
+ name = if _.isFunction(model.url) then model.url() else model.url
+ s = name.split("/")
+ l = s.length
+ name = if l % 2 then s[l - 2] else s[l - 1]
+
+class Backbone.Collection extends Backbone.Collection
+ initialize: ->
+ @listen()
+ listen: ->
+ name = Backbone.nowjsConnector.extractName(@)
+ # nowjs callbacks
+ now[name] = {
+ update: (model, options) =>
+ this.get(model.id).set(model, options)
+ create: (model, options) =>
+ this.add(model, options)
+ delete: (model, options) =>
+ this.remove(model, options)
+ read: (data, options) =>
+ this[if options?.add then 'add' else 'reset'](data, options);
+ }
+
+Backbone.sync = (method, model, options) ->
+ name = Backbone.nowjsConnector.extractName(@)
+ delete options.success
+ delete options.error
+ now.sync method, name, model.attributes, options
@@ -0,0 +1,16 @@
+exports.common = {};
+
+exports.dev = {
+ twitter_user: 'username',
+ twitter_password: 'password'
+};
+
+exports.test = {
+ twitter_user: 'username',
+ twitter_password: 'password'
+};
+
+exports.production = {
+ twitter_user: 'username',
+ twitter_password: 'password'
+};
@@ -0,0 +1,16 @@
+{ "name": "backbone-nowjs"
+ , "version": "0.1.0"
+ , "description": "Backbone connector for nowjs"
+ , "keywords": ["backbone", "nowjs", "connector"]
+ , "author": "mkuklis <michal.kuklis@gmail.com>"
+ , "directories": { "lib": "./lib/" }
+ , "dependencies": {
+ "now": "0.7"
+ , "express": "2.2.2"
+ , "ejs": "0.4.1"
+ , "settings": "0.0.1"
+ }
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
+

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,64 @@
+var sys = require("sys")
+ , uuid = require("node-uuid")
+ , express = require('express')
+ , nowjs = require("now")
+ , port = parseInt(process.env.VCAP_APP_PORT || process.env.PORT || 8080)
+ // settings
+ , Settings = require('settings')
+ , config = __dirname + '/config/config.js'
+ , settings = new Settings(config).getEnvironment(process.env.NODE_ENV || "dev");
+
+// express
+var app = express.createServer();
+app.register('.html', require('ejs'));
+app.set('views', __dirname + '/views');
+app.set('view engine', 'html');
+app.use(express.static(__dirname + '/public'));
+app.get('/', function(req, res) {
+ res.render('index.html');
+});
+
+app.listen(port);
+var everyone = nowjs.initialize(app);
+
+var collection = [];
+var crud = {
+ update: function (model) {
+ collection.forEach(function (item, index) {
+ if (item.id === model.id) {
+ collection[index] == model;
+ return true;
+ }
+ });
+ return model;
+ },
+ create: function (model) {
+ model.id = uuid();
+ collection.push(model);
+ return model;
+ },
+ delete: function (model) {
+ collection.forEach(function (item, index) {
+ if (item.id === model.id) {
+ collection.splice(index, 1);
+ return true;
+ }
+ });
+ return model
+ },
+ read: function () {
+ return collection;
+ }
+};
+
+everyone.now.sync = function (method, name, model, options) {
+ var data = crud[method](model, options);
+ if (method === "read") {
+ this.now[name][method](data, options);
+ }
+ else {
+ console.log(name)
+ console.log(method)
+ everyone.now[name][method](data, options);
+ }
+}
No changes.
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>nowjs test</title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
+ <script src="http://documentcloud.github.com/underscore/underscore.js"></script>
+ <script src="backbone.js"></script>
+ <script src="/nowjs/now.js"></script>
+ <script src="backbone-nowjs.js"></script>
+ <script>
+ var CarModel = Backbone.Model.extend({
+ collection: CarCollection,
+ initialize: function () {
+ }
+ });
+
+ var CarCollection = Backbone.Collection.extend({
+ url: "/cars",
+ model: CarModel
+ });
+
+ $(document).ready(function() {
+ var collection = new CarCollection();
+
+ collection.bind("add", function (model) {
+ $('#cars').append("<div>" + model.get('id') + "</div>");
+ });
+
+ collection.bind("remove", function (model) {
+ $("#cars").find("div").last().remove();
+ });
+
+ collection.bind("reset", function (collection) {
+ collection.each(function (model) {
+ $('#cars').append("<div>" + model.get('id') + "</div>");
+ });
+ });
+
+ now.ready(function () {
+ collection.fetch();
+ });
+
+ $("#fetch").click(function () {
+ $('#cars').html("");
+ collection.fetch();
+ });
+
+ $("#create").click(function () {
+ var m1 = new CarModel();
+ m1.set({color: "red"});
+ collection.create(m1);
+ });
+
+ $("#update").click(function () {
+ var m = collection.at(0);
+ m.save({color: "blue"});
+ });
+
+ $("#destroy").click(function () {
+ var m = collection.last();
+ m.destroy();
+ });
+ });
+ </script>
+ </head>
+ <body>
+ <input id="fetch" type="button" value="Fetch" />
+ <input id="create" type="button" value="Create" />
+ <input id="update" type="button" value="Update" />
+ <input id="destroy" type="button" value="Destroy" />
+ <div id="cars"></div>
+ </body>
+</html>

0 comments on commit 817812d

Please sign in to comment.