Permalink
Browse files

v0.4.0

  • Loading branch information...
1 parent 461273a commit c8e1284987352b6bb080f9640038cb8395ceec3b weepy@github.com committed Mar 15, 2011
Showing with 82 additions and 44 deletions.
  1. +7 −0 History.md
  2. +0 −1 TODO.md
  3. +12 −12 docs/index.html
  4. +38 −22 lib/core.js
  5. +3 −2 lib/stores/redis.js
  6. +1 −0 lib/stores/rest.js
  7. +1 −1 package.json
  8. +2 −0 test/lib/app.js
  9. +12 −4 test/lib/task.js
  10. +1 −1 test/test_core.js
  11. +5 −1 test/test_rest.js
View
7 History.md
@@ -1,3 +1,10 @@
+0.0.4
+=====
+
+ * ignore required on auto_inc
+ * added "creating" and "created" events
+ * moved some properties to a hidden _ property
+
0.0.3
=====
View
1 TODO.md
@@ -1 +0,0 @@
-* ignore required on auto_inc
View
24 docs/index.html
@@ -131,15 +131,15 @@
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Model</span> = <span class="keyword">function</span>(<span class="variable">props</span>, <span class="variable">_sync</span>) {
<span class="variable">props</span> = <span class="variable">props</span> || {}
- <span class="this">this</span>.<span class="variable">_properties</span> = {} <span class="comment">// this._properties contains the synchronized version of the properties in the Store</span>
+ <span class="this">this</span>.<span class="variable">_.properties</span> = {} <span class="comment">// this._.properties contains the synchronized version of the properties in the Store</span>
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">k</span> <span class="keyword">in</span> <span class="variable">properties</span>) {
<span class="keyword">var</span> <span class="variable">p</span> = <span class="variable">properties</span>[<span class="variable">k</span>]
<span class="keyword">if</span>(<span class="variable">k</span> <span class="keyword">in</span> <span class="variable">props</span>) <span class="this">this</span>[<span class="variable">k</span>] = <span class="variable">props</span>[<span class="variable">k</span>]
<span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">'default'</span> <span class="keyword">in</span> <span class="variable">p</span>) <span class="this">this</span>[<span class="variable">k</span>] = <span class="variable">p</span>[<span class="string">'default'</span>]
}
- <span class="this">this</span>.<span class="variable">_callbacks</span> = {} <span class="comment">// this._callbacks contains event bindings relevant to this instance</span>
+ <span class="this">this</span>.<span class="variable">_.callbacks</span> = {} <span class="comment">// this._.callbacks contains event bindings relevant to this instance</span>
<span class="keyword">if</span>(<span class="variable">props</span>.<span class="variable">errors</span>) <span class="this">this</span>.<span class="variable">errors</span> = <span class="variable">props</span>.<span class="variable">errors</span>
<span class="this">this</span>.<span class="variable">trigger</span>(&<span class="variable">quot</span>;<span class="variable">initialize</span>&<span class="variable">quot</span>;)
<span class="keyword">if</span>(<span class="variable">_sync</span>) <span class="this">this</span>.<span class="variable">_synchronize</span>()
@@ -211,14 +211,14 @@
<span class="keyword">var</span> <span class="variable">o</span> = {}, <span class="variable">modified</span>
<span class="keyword">if</span>(<span class="variable">prop</span>) {
- <span class="variable">modified</span> = <span class="this">this</span>.<span class="variable">_properties</span>[<span class="variable">prop</span>] != <span class="this">this</span>[<span class="variable">prop</span>]
- <span class="keyword">return</span> <span class="variable">modified</span> ? [<span class="this">this</span>.<span class="variable">_properties</span>[<span class="variable">prop</span>], <span class="this">this</span>[<span class="variable">prop</span>]] : <span class="variable">false</span>
+ <span class="variable">modified</span> = <span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">prop</span>] != <span class="this">this</span>[<span class="variable">prop</span>]
+ <span class="keyword">return</span> <span class="variable">modified</span> ? [<span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">prop</span>], <span class="this">this</span>[<span class="variable">prop</span>]] : <span class="variable">false</span>
}
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">properties</span>) {
- <span class="keyword">if</span>(<span class="this">this</span>.<span class="variable">_properties</span>[<span class="variable">i</span>] !== <span class="this">this</span>[<span class="variable">i</span>]) {
+ <span class="keyword">if</span>(<span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">i</span>] !== <span class="this">this</span>[<span class="variable">i</span>]) {
<span class="variable">modified</span> = <span class="variable">true</span>
- <span class="variable">o</span>[<span class="variable">i</span>] = [<span class="this">this</span>.<span class="variable">_properties</span>[<span class="variable">i</span>], <span class="this">this</span>[<span class="variable">i</span>]]
+ <span class="variable">o</span>[<span class="variable">i</span>] = [<span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">i</span>], <span class="this">this</span>[<span class="variable">i</span>]]
}
}
<span class="keyword">return</span> <span class="variable">modified</span> ? <span class="variable">o</span> : <span class="variable">false</span>
@@ -404,7 +404,7 @@
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">bind</span> = <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">bind</span> = <span class="keyword">function</span>(<span class="variable">ev</span>, <span class="variable">callback</span>, <span class="variable">async</span>) {
<span class="keyword">if</span>(<span class="variable">async</span>) <span class="variable">callback</span>.<span class="variable">async</span> = <span class="variable">true</span>
- <span class="keyword">var</span> <span class="variable">list</span> = <span class="this">this</span>.<span class="variable">_callbacks</span>[<span class="variable">ev</span>] || (<span class="this">this</span>.<span class="variable">_callbacks</span>[<span class="variable">ev</span>] = [])
+ <span class="keyword">var</span> <span class="variable">list</span> = <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || (<span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] = [])
<span class="variable">list</span>.<span class="variable">push</span>(<span class="variable">callback</span>)
}</code></pre>
</td>
@@ -416,11 +416,11 @@
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">unbind</span> = <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">unbind</span> = <span class="keyword">function</span>(<span class="variable">ev</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (!<span class="variable">ev</span>)
- <span class="this">this</span>.<span class="variable">_callbacks</span> = {}
+ <span class="this">this</span>.<span class="variable">_.callbacks</span> = {}
<span class="keyword">else</span> <span class="keyword">if</span> (!<span class="variable">callback</span>)
- <span class="this">this</span>.<span class="variable">_callbacks</span>[<span class="variable">ev</span>] = []
+ <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] = []
<span class="keyword">else</span> {
- <span class="keyword">var</span> <span class="variable">list</span> = <span class="this">this</span>.<span class="variable">_callbacks</span>[<span class="variable">ev</span>] || []
+ <span class="keyword">var</span> <span class="variable">list</span> = <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || []
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">list</span>.<span class="variable">length</span>; <span class="variable">i</span>++) {
<span class="keyword">if</span> (<span class="variable">callback</span> === <span class="variable">list</span>[<span class="variable">i</span>]) {
@@ -445,8 +445,8 @@
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">trigger</span> = <span class="keyword">function</span>(<span class="variable">ev</span>) <span class="comment">// further args </span>
{
- <span class="keyword">var</span> <span class="variable">global</span> = <span class="class">Model</span>.<span class="variable">_callbacks</span>[<span class="variable">ev</span>] || []
- <span class="variable">local</span> = <span class="this">this</span>.<span class="variable">_callbacks</span>[<span class="variable">ev</span>] || [],
+ <span class="keyword">var</span> <span class="variable">global</span> = <span class="class">Model</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || []
+ <span class="variable">local</span> = <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || [],
<span class="variable">list</span> = <span class="variable">global</span>.<span class="variable">concat</span>(<span class="variable">local</span>)
<span class="variable">args</span> = <span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="number integer">1</span>),
<span class="variable">self</span> = <span class="this">this</span>
View
60 lib/core.js
@@ -29,15 +29,14 @@ module.exports = function(type, properties, store) {
*/
var Model = function(props, _sync) {
props = props || {}
- this._properties = {} // this._properties contains the synchronized version of the properties in the Store
-
+ this._ = {properties: {}} // store private state
for(var k in properties) {
var p = properties[k]
if(k in props) this[k] = props[k]
else if('default' in p) this[k] = p['default']
}
- this._callbacks = {} // this._callbacks contains event bindings relevant to this instance
+ this._.callbacks = {} // this._.callbacks contains event bindings relevant to this instance
if(props.errors) this.errors = props.errors
this.trigger("initialize")
if(_sync) this._synchronize()
@@ -85,11 +84,11 @@ module.exports = function(type, properties, store) {
/** @private
* Synchonize an object
- * This copies the local properties onto a _private_ _properties object
+ * This copies the local properties onto a _private_ _.properties object
*/
Model.prototype._synchronize = function() {
- this._properties = {}
- for(var k in properties) this._properties[k] = this[k]
+ this._.properties = {}
+ for(var k in properties) this._.properties[k] = this[k]
}
/** Has this instance been modified (i.e not in sync wit the Store). Returns null if there's no difference
@@ -100,14 +99,14 @@ module.exports = function(type, properties, store) {
var o = {}, modified
if(prop) {
- modified = this._properties[prop] != this[prop]
- return modified ? [this._properties[prop], this[prop]] : false
+ modified = this._.properties[prop] != this[prop]
+ return modified ? [this._.properties[prop], this[prop]] : false
}
for(var i in properties) {
- if(this._properties[i] !== this[i]) {
+ if(this._.properties[i] !== this[i]) {
modified = true
- o[i] = [this._properties[i], this[i]]
+ o[i] = [this._.properties[i], this[i]]
}
}
return modified ? o : false
@@ -142,10 +141,21 @@ module.exports = function(type, properties, store) {
*/
Model.prototype.save = function(cb) {
var self = this
- utils.achain.call(self, self._saveStack, [], function(err, results) {
- if(!err) self._synchronize()
- cb && cb.call(self, self)
- })
+
+ if(this._.properties.id == null) {
+ this._.creating = true
+ this.trigger("creating").complete(run_save_stack)
+ } else {
+ delete this._.creating
+ run_save_stack()
+ }
+
+ function run_save_stack() {
+ utils.achain.call(self, self._saveStack, [], function(err, results) {
+ if(!err) self._synchronize()
+ cb && cb.call(self, self)
+ })
+ }
}
/** Add an error
@@ -243,7 +253,13 @@ module.exports = function(type, properties, store) {
})
} else {
this._synchronize()
- this.trigger("saved").complete(cb)
+ this.trigger("saved").complete(function() {
+ if(this._.creating) {
+ delete this._.creating
+ this.trigger("created").complete(cb)
+ } else
+ cb.call(this)
+ })
}
}
@@ -266,7 +282,7 @@ module.exports = function(type, properties, store) {
* callback container
*/
- Model._callbacks = {}
+ Model._ = {callbacks:{}}
/* Bind to all instances of Model or just this instance
@param {string} name of event, e.g. "saving"
@@ -275,18 +291,18 @@ module.exports = function(type, properties, store) {
*/
Model.bind = Model.prototype.bind = function(ev, callback, async) {
if(async) callback.async = true
- var list = this._callbacks[ev] || (this._callbacks[ev] = [])
+ var list = this._.callbacks[ev] || (this._.callbacks[ev] = [])
list.push(callback)
}
/* Unbind to all instances of Model or just this instance */
Model.unbind = Model.prototype.unbind = function(ev, callback) {
if (!ev)
- this._callbacks = {}
+ this._.callbacks = {}
else if (!callback)
- this._callbacks[ev] = []
+ this._.callbacks[ev] = []
else {
- var list = this._callbacks[ev] || []
+ var list = this._.callbacks[ev] || []
for (var i = 0; i < list.length; i++) {
if (callback === list[i]) {
@@ -305,8 +321,8 @@ module.exports = function(type, properties, store) {
*/
Model.prototype.trigger = function(ev) // further args
{
- var global = Model._callbacks[ev] || []
- local = this._callbacks[ev] || [],
+ var global = Model._.callbacks[ev] || []
+ local = this._.callbacks[ev] || [],
list = global.concat(local)
args = Array.prototype.slice.call(arguments, 1),
self = this
View
5 lib/stores/redis.js
@@ -31,7 +31,6 @@ exports.mixin = function(Model) {
}
fn._get_id = function(next) {
-
var self = this
if(this.id == null && Model.properties.id.auto_inc) {
Model.client.INCR(Model.key() + ":id", function(err, data) {
@@ -171,8 +170,10 @@ exports.mixin = function(Model) {
Model.add_edge = Model.prototype.add_edge = function(type, id, score, cb) {
if(arguments.length == 3 && typeof score == "function") {
- cb = score; score = 0
+ cb = score; score = 0
}
+ if(arguments.length == 2) score = 0
+
var fscore = parseFloat(score) || score/1
if(isNaN(fscore)) {
console.log("ERROR: score: " + score + " is NaN")
View
1 lib/stores/rest.js
@@ -5,6 +5,7 @@ exports.mixin = function(Model) {
fn._persist = function(cb) {
var self = this
+
Model.ajax.post(Model.url + "/save", {json: JSON.stringify(this.toJSON())}, function(o) {
self.merge(o)
cb(o)
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "mmmodel",
- "version": "0.0.3",
+ "version": "0.0.4",
"description": "Homer's favorite Javascript ORM",
"keywords": ["javascript", "orm", "redis", "rest"],
"author": "Jonah Fox <jonah@boodigital.com>",
View
2 test/lib/app.js
@@ -20,7 +20,9 @@ app.get("/tasks", function(req, res) {
// save / update
app.post("/tasks/save", function(req, res) {
var o = JSON.parse(req.param("json"))
+
var task = new Task(o)
+
task.save(function(ok) {
res.send(task.toJSON())
})
View
16 test/lib/task.js
@@ -10,18 +10,26 @@ module.exports = function(store) {
Task.bind("saving", function updateCreatedAt(done) {
this.created_at || (this.created_at = new Date())
- delete this._saved
+ delete this.__test_saved
done()
}, true) // async
- Task.bind("saved", function setSaved() {
- this._saved = true
+ Task.bind("saved", function() {
+ this.__test_saved = true
}) // async
Task.bind("initialize", function(done) {
- this.test = 123
+ this.__test_initialize = true
done()
}, true)
+ Task.bind("creating", function() {
+ this.__test_creating = true
+ })
+
+ Task.bind("created", function() {
+ this.__test_created = true
+ })
+
return Task
}
View
2 test/test_core.js
@@ -11,7 +11,7 @@ exports.load = function(done) {
task = new Task(o)
assert.eql(task.user, "billy")
assert.eql(task.keywords, ["books"])
- assert.eql(task.test, 123)
+ assert.eql(task.__test_initialize, true)
done()
}
View
6 test/test_rest.js
@@ -16,7 +16,7 @@ function params(o) {
Task.ajax = {
ajax: function(url, data, method, callback) {
url += "?" + params(data)
- is.response(app, { url: url, method: method, data: "_" }, function(res) {
+ is.response(app, { url: url, method: method }, function(res) {
callback(JSON.parse(res.body))
})
},
@@ -35,8 +35,11 @@ exports.test_non_existing_find = function(done) {
})
}
+
exports.test_create = function(done) {
Task.create({user: "jonah"}, function(task) {
+ task.__test_creating.should.eql(true)
+ task.__test_created.should.eql(true)
task.id.should.be.eql(1)
done()
})
@@ -51,6 +54,7 @@ exports.test_find = function(done) {
})
}
+
exports.test_update = function(done) {
Task.find(1, function(task) {
task.user = "bob"

0 comments on commit c8e1284

Please sign in to comment.