diff --git a/examples/mustache-example/app.js b/examples/mustache-example/app.js index f180e6b..a5fc9cc 100644 --- a/examples/mustache-example/app.js +++ b/examples/mustache-example/app.js @@ -3,9 +3,13 @@ var Sara = require('../../lib/sara') // Our app var TodoList = module.exports = new Sara() + .storage(require('../../lib/adapters/mongodb')) + .layout('./templates/layout.html') + .initialize(function () { + var Todo = require('./models/todo') , TodoView = require('./views/todo') , AboutView = require('./views/about') @@ -19,4 +23,5 @@ var TodoList = module.exports = new Sara() return AboutView.render() } }) + }) diff --git a/examples/mustache-example/templates/todo.html b/examples/mustache-example/templates/todo.html index d725f99..212a4ac 100644 --- a/examples/mustache-example/templates/todo.html +++ b/examples/mustache-example/templates/todo.html @@ -3,7 +3,7 @@ {{#todos}}
  • diff --git a/examples/mustache-example/views/about.js b/examples/mustache-example/views/about.js index 109828d..1496745 100644 --- a/examples/mustache-example/views/about.js +++ b/examples/mustache-example/views/about.js @@ -1,7 +1,8 @@ var TodoList = require('../app') + , $ = require('jquery') var AboutView = module.exports = new TodoList.View('Todo', { render: function (document) { - document.querySelector('main').innerHTML = 'About my app.' + $(document).find('main').html('About my app.') } }) diff --git a/examples/mustache-example/views/todo.js b/examples/mustache-example/views/todo.js index 7c9f6c0..f677a06 100644 --- a/examples/mustache-example/views/todo.js +++ b/examples/mustache-example/views/todo.js @@ -1,30 +1,25 @@ var TodoList = require('../app') , Mustache = require('mustache') , TodoController = require('../controllers/todo') - , _ = require('../../../lib/sara').Utils , Todo = require('../models/todo') + , $ = require('jquery') var TodoView = module.exports = new TodoList.View('Todo', { template: TodoList.template('todo', '../templates/todo.html') , render: function (document) { - Todo.all().on('add remove changeAny', render.bind(this)) - render.bind(this)() - function render() { - Todo.all().forEach(function (todo) { - todo.key = todo.id() - }) + $(document).find('main').html(Mustache.render(this.template.toString(), { todos: Todo.all(), completed: Todo.completed() })) - // render - document.querySelector('main').innerHTML = Mustache.render(this.template.toString(), { todos: Todo.all(), completed: Todo.completed() }) + with (TodoController) { + $(document).find('div button').click(clear) + $(document).find('form').submit(create) + $(document).find('input[type="checkbox"]').click(toggleChecked) + } + + return render.bind(this) + } - // events - document.querySelector('div button').onclick = TodoController.clear - document.querySelector('form').onsubmit = TodoController.create - _(document.querySelectorAll('input[type="checkbox"]')).forEach(function (input) { - input.onclick = TodoController.toggleChecked - }) - } + Todo.all().on('add remove changeAny', render.call(this)) } }) diff --git a/lib/sara/model.js b/lib/sara/model.js index 1502da9..aad4826 100644 --- a/lib/sara/model.js +++ b/lib/sara/model.js @@ -28,12 +28,14 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize var app = this.constructor.app + schema.id = 0 + // Our constructor var Constructor = _.createNamedFunction(name, function (data) { _(this).extend(this.constructor._schema) + var last = this.constructor.last() + this.id = last ? last.id + 1 : 1 _(this).extend(data) - - // Init event var initialize = this.constructor._initialize if (initialize) initialize(); }) @@ -100,10 +102,15 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize .add(function find(id) { var records = this.all() , i = records.length - while (i--) if (records[i].id() === parseInt(id)) return records[i] + while (i--) if (records[i].id === parseInt(id)) return records[i] return null }) + .add(function last() { + var records = this.all() + return records[records.length - 1] + }) + /** * Find a record by its attributes * @param {Object} - object - An object of the attributes to query for. @@ -139,17 +146,13 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize var collection = this.constructor.all() , last - if (typeof this.id() !== 'number') { + if (typeof this.id !== 'number') { last = collection[collection.length - 1] } collection.add(this) }) - .method(function id() { - return this.constructor.all().indexOf(this) - }) - /** * Save the model instance to memory. * @returns {undefined} @@ -168,7 +171,7 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize } // Write to disk - db.find({ id: this.id() }, function (err, docs) { + db.find({ id: this.id }, function (err, docs) { if (err) throw err if (!docs.length) { @@ -176,7 +179,7 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize if (err) throw err }) } else { - db.update({ id: this.id() }, this.data(), {}, function () { + db.update({ id: this.id }, this.data(), {}, function () { if (err) throw err }) } @@ -191,7 +194,7 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize }).method(function set(name, value, forward) { if (forward !== false) forward = true this[name] = value - db.update({ _id: this._id }, this.data(), {}, function () { + db.update({ id: this.id }, this.data(), {}, function () { }) this.constructor.all().trigger('changeAny') @@ -201,7 +204,7 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize resource: this.constructor.name , clientId: this.constructor.app.clientId , type: 'change' - , id: this.id() + , id: this.id , name: name , value: value })) @@ -227,13 +230,13 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize resource: this.constructor.name , clientId: this.constructor.app.clientId , type: 'remove' - , id: this.id() + , id: this.id })) } collection.remove(this) - db.remove({ _id: this._id }, function (err) { if (err) throw err }) + db.remove({ id: this.id }, function (err) { if (err) throw err }) }) .method('_events', { diff --git a/package.json b/package.json index 7cdde15..5d9a20a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sara", - "version": "0.0.1", + "version": "0.1.0", "main": "./lib/sara.js", "dependencies": { "connect": "2.12.0",