Skip to content

Commit

Permalink
Closed #65: using ids again, version bump!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Gariety committed Feb 16, 2014
1 parent 161b9f1 commit 4047ffd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
5 changes: 5 additions & 0 deletions examples/mustache-example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -19,4 +23,5 @@ var TodoList = module.exports = new Sara()
return AboutView.render()
}
})

})
2 changes: 1 addition & 1 deletion examples/mustache-example/templates/todo.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{#todos}}
<li>
<label>
<input type="checkbox" id="{{key}}" {{#completed}}checked{{/completed}} />
<input type="checkbox" id="{{id}}" {{#completed}}checked{{/completed}} />
<span {{#completed}}class="completed"{{/completed}}>{{title}}</span>
</label>
</li>
Expand Down
3 changes: 2 additions & 1 deletion examples/mustache-example/views/about.js
Original file line number Diff line number Diff line change
@@ -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.')
}
})
27 changes: 11 additions & 16 deletions examples/mustache-example/views/todo.js
Original file line number Diff line number Diff line change
@@ -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))
}
})
31 changes: 17 additions & 14 deletions lib/sara/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand All @@ -168,15 +171,15 @@ 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) {
db.insert(this.data(), function (err, docs) {
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
})
}
Expand All @@ -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')
Expand All @@ -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
}))
Expand All @@ -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', {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sara",
"version": "0.0.1",
"version": "0.1.0",
"main": "./lib/sara.js",
"dependencies": {
"connect": "2.12.0",
Expand Down

0 comments on commit 4047ffd

Please sign in to comment.