Permalink
Browse files

added gitignore and dependencies in package.json

  • Loading branch information...
1 parent a38305c commit 9713891301fb623e195bd04b82dd63a3ad9dad88 weepy@github.com committed Apr 12, 2011
Showing with 134 additions and 100 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 docs/index.html
  3. +77 −39 lib/core.js
  4. +3 −2 lib/edge_query.js
  5. +0 −8 lib/stores/memory.js
  6. +29 −34 lib/stores/redis.js
  7. +1 −7 lib/stores/rest.js
  8. +2 −1 lib/utils.js
  9. +2 −1 package.json
  10. +11 −1 test/test_memory.js
  11. +5 −5 test/test_redis.js
  12. +1 −0 test/test_rest.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -370,7 +370,7 @@
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">validate</span> = <span class="keyword">function</span>(<span class="variable">cb</span>) {
<span class="this">this</span>.<span class="variable">errors</span> = []
- <span class="this">this</span>.<span class="variable">_property_validate</span>()
+ <span class="this">this</span>.<span class="variable">_run_default_validations</span>()
<span class="this">this</span>.<span class="variable">trigger</span>(&<span class="variable">quot</span>;<span class="variable">saving</span>&<span class="variable">quot</span>;).<span class="variable">complete</span>(<span class="keyword">function</span>() {
<span class="variable">cb</span>.<span class="variable">call</span>(<span class="this">this</span>, !<span class="this">this</span>.<span class="variable">in_error</span>())
})
@@ -383,7 +383,7 @@
</p>
</td>
<td class="code">
-<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">_property_validate</span> = <span class="keyword">function</span>() {
+<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">_run_default_validations</span> = <span class="keyword">function</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">var</span> <span class="variable">p</span> = <span class="variable">properties</span>[<span class="variable">i</span>]
<span class="keyword">if</span>(<span class="variable">p</span>.<span class="variable">required</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>[<span class="variable">i</span>] == <span class="keyword">null</span>)
View
@@ -47,7 +47,8 @@ module.exports = function(type, properties, store) {
*/
Model.type = type
Model.properties = properties
-
+ var fn = Model.prototype
+
/* Create a sychronized object (typically it would be one that been retrieved from a database)
*/
Model.load = function(props) {
@@ -86,7 +87,7 @@ module.exports = function(type, properties, store) {
* Synchonize an object
* This copies the local properties onto a _private_ _.properties object
*/
- Model.prototype._synchronize = function() {
+ fn._synchronize = function() {
this._.properties = {}
for(var k in properties) this._.properties[k] = this[k]
}
@@ -95,7 +96,7 @@ module.exports = function(type, properties, store) {
*
* @param {string} _optional_ if null, we'll check the whole function, else it will check only this property
*/
- Model.prototype.modified = function(prop) {
+ fn.modified = function(prop) {
var o = {}, modified
if(prop) {
@@ -114,13 +115,13 @@ module.exports = function(type, properties, store) {
/** Has this model been saved? I.e. is it _not_ modified
*/
- Model.prototype.saved = function() {
+ fn.saved = function() {
return !this.modified()
}
/** Update the model with the params and save it
*/
- Model.prototype.update = function(params, cb) {
+ fn.update = function(params, cb) {
params || (params == {})
Model.find(params.id, function(m) {
@@ -139,28 +140,42 @@ module.exports = function(type, properties, store) {
*
* @param {Function} optional callback. Returns the saved object
*/
- Model.prototype.save = function(cb) {
+ fn.save = function(cb) {
var self = this
+ self.errors = []
+
+ utils.achain.call(self, self._saveStack, [], function(err, results) {
+ if(!err) self._synchronize()
+ cb && cb.call(self, self)
+ })
+ }
+
+ fn._get_id = function(cb) {
+ // ignore ...
+ cb()
+ }
+
+ fn._trigger_creating = function(cb) {
if(this._.properties.id == null) {
this._.creating = true
- this.trigger("creating").complete(run_save_stack)
+ this.trigger("creating").complete(cb)
} 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)
- })
+ cb()
}
+
}
+ fn._trigger_saving = function(cb) {
+ this.trigger("saving").complete(function() {
+ cb.call(this, !this.in_error())
+ })
+ }
+
/** Add an error
*/
- Model.prototype.error = function(error) {
+ fn.error = function(error) {
this.errors || (this.errors = [])
this.errors.push(error)
}
@@ -170,7 +185,7 @@ module.exports = function(type, properties, store) {
* @param {Function} optional callback - returns if it was successful or not
* if successful triggers "destroyed"
*/
- Model.prototype.destroy = function(cb) {
+ fn.destroy = function(cb) {
var self = this
Model.destroy(this.id, function(ok) {
if(ok) self.trigger("destroyed")
@@ -182,7 +197,7 @@ module.exports = function(type, properties, store) {
* NB Does not return a string
* NB Date types are converted to numbers
*/
- Model.prototype.toJSON = function() {
+ fn.toJSON = function() {
var o = {}
for(var name in properties) {
if(name in this) {
@@ -198,13 +213,13 @@ module.exports = function(type, properties, store) {
/** Returns a JSON string representation
*/
- Model.prototype.toString = function() {
+ fn.toString = function() {
return JSON.stringify(this.toJSON())
}
/** Does this model have errors ?
*/
- Model.prototype.in_error = function() {
+ fn.in_error = function() {
return this.errors && this.errors.length != 0
}
@@ -213,7 +228,7 @@ module.exports = function(type, properties, store) {
* @param {Object} properties
* @param {Object} optional alternative for "this"
*/
- Model.prototype.merge = function(props, x) {
+ fn.merge = function(props, x) {
for(var name in properties)
if(name in props) (x || this)[name] = props[name]
return this
@@ -223,50 +238,60 @@ module.exports = function(type, properties, store) {
* performs some simple checks for 'required' and 'format' properties
* calls a "saving" callback, which on completion checks that the number of errors is zero and runs the callback
*/
- Model.prototype.validate = function(cb) {
+ fn.validate = function(cb) {
this.errors = []
- this._property_validate()
+ this._run_default_validations()
this.trigger("saving").complete(function() {
cb && cb.call(this, !this.in_error())
})
}
/** Simple Validation for 'required' and 'format' properties
*/
- Model.prototype._property_validate = function() {
+ fn._run_default_validations = function(cb) {
for(var i in properties) {
var p = properties[i]
if(p.required && this[i] == null)
this.error(i + " is required")
if(p.format && this[i] && !this[i].match(p.format))
this.error(i + " is bad format")
}
+ cb && cb()
}
/** @private
* Trigger relevant callbacks and synchronize if succesful
*/
- Model.prototype._finalize_save = function(cb) {
+ fn._trigger_saved = function(cb) {
if(this.in_error()) {
this.trigger("error", "saving failed").complete(function(){
cb && cb(false)
})
} else {
this._synchronize()
- this.trigger("saved").complete(function() {
-
- if(this._.creating) {
- delete this._.creating
- this._.created = true
- this.trigger("created").complete(cb)
- } else {
- delete this._.created
- cb.call(this)
- }
- })
+ this.trigger("saved").complete(cb)
+ }
+ }
+
+ fn._trigger_created = function(cb) {
+ if(this._.creating) {
+ delete this._.creating
+ this._.created = true
+ this.trigger("created").complete(cb)
+ } else {
+ delete this._.created
+ cb.call(this)
}
}
+ fn._post_persist = function (next) {
+ next()
+ }
+
+ fn._check_errors = function(cb) {
+ cb && cb.call(this, !this.in_error())
+ }
+
/** @private
* Load the revelant store for this model
*/
@@ -278,6 +303,19 @@ module.exports = function(type, properties, store) {
if(store) Model._set_store(store)
+
+ fn._saveStack = [
+ fn._trigger_creating,
+ fn._trigger_saving,
+ fn._run_default_validations,
+ fn._check_errors,
+ fn._get_id,
+ fn._persist,
+ fn._post_persist,
+ fn._trigger_saved,
+ fn._trigger_created
+ ]
+
/** Events
* ------
@@ -293,14 +331,14 @@ module.exports = function(type, properties, store) {
@param {Function} callback to run
@param {boolean} is the callback async?
*/
- Model.bind = Model.prototype.bind = function(ev, callback, async) {
+ Model.bind = fn.bind = function(ev, callback, async) {
if(async) callback.async = true
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) {
+ Model.unbind = fn.unbind = function(ev, callback) {
if (!ev)
this._.callbacks = {}
else if (!callback)
@@ -323,7 +361,7 @@ module.exports = function(type, properties, store) {
@param {string} name of event
@param [further arugments]
*/
- Model.prototype.trigger = function(ev) // further args
+ fn.trigger = function(ev) // further args
{
var global = Model._.callbacks[ev] || []
local = this._.callbacks[ev] || [],
View
@@ -1,7 +1,7 @@
-function EdgeQuery(x, self) {
+function EdgeQuery(x, self, client) {
this.key = x
this.self = self
- this.client = self.client || self.constructor.client
+ this.client = client || self.client || self.constructor.client
}
EdgeQuery.prototype.range = function(low, high) {
@@ -79,6 +79,7 @@ EdgeQuery.prototype.all = function(cb) {
else cb.call(context, data)
})
+
this.client[method].apply(this.client, args)
}
@@ -17,14 +17,6 @@ exports.mixin = function(Model) {
cb(true)
}
- fn._saveStack = [
- fn.validate,
- fn._get_id,
- fn._persist,
- fn._finalize_save
- ]
-
-
Model.find = function(id, cb) {
cb(Model.DB[id])
}
Oops, something went wrong.

0 comments on commit 9713891

Please sign in to comment.