Skip to content

Commit

Permalink
work on jquery rest
Browse files Browse the repository at this point in the history
  • Loading branch information
weepy committed Mar 2, 2011
1 parent c78fb34 commit d52c2bf
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 97 deletions.
144 changes: 47 additions & 97 deletions lib/stores/jquery_rest.js
@@ -1,104 +1,54 @@
function merge(self, o) {
for(var i in o) self[i] = o[i]
}
var utils = require("../utils")

function Task(o) {
this.original = {}
merge(this, o)
merge(this.original, o)
return
}

Task.prototype.changed = function(prop) {
if(prop) return this.original[prop] != this[prop]
exports.mixin = function(model) {
var fn = model.prototype

var o = {}
for(var i in Task.properties) {
if(this.original[i] != this[i])
o[i] = [this.original[i], this[i]]
fn._destroy = function(cb) {
var self = this
$.post(model.url + "/destroy", {json: this.toJSON()}, function(text) {
cb(text == "ok")
})
}
return o
}

Task.bulkNew = function(a) {
var ret = []
for(var i=0; i<a.length;i++)
ret[i] = new Task(a[i])
return ret
}

// Task.prototype.doneClass = function() {
// return this.done ? "done" : ""
// }
// Task.prototype.date = function() {
// return "2nd Nov"
// }

// Task.prototype.$day = function() {
// return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][this.day]
// }

Task.prototype.letter = function() {
return this.$day().charAt(0)
}

Task.properties = {
id: { type: "number", auto_inc: true },
user_id: {type: "string", required: true},
text: {type: "string", required: true },
week: { type: "number", required: true},
day: { type: "number", required: true},
done: { type: "number", "default": 0 },
created_at: { type: "date" },
updated_at: { type: "date" }
fn._persist = function(cb) {
var self = this
$.post(model.url + "/save", {json: JSON.stringify(this.toJSON())}, function(text) {
var o = JSON.parse(text)
task.merge(o)
cb()
})
}

// Task.prototype.save = function(cb) {
// var o = {}, data
// var props = ["id", "done", "text"]
//
// for(var i=0; i < props.length;i++) {
// if(data = this[props[i]] == null) continue
// o[props[i]] = data
// }
// $.post("/tasks/save", {json: JSON.stringify(o)} function(data) {
// var task = new Task(data)
// cb(task)
// })
// }



Task.prototype.toData = function() {
var o = {}
for(var name in Task.properties) {
if(!(name in this)) continue
o[name] = this[name]
}
return o
}

Task.prototype.toJSON = function() {
return JSON.stringify(this.toData())
}



Task.prototype.destroy = function(cb) {
var task = this
$.post("/tasks/destroy", {json: this.toJSON(), current_user: "jonah"}, function(text) {
cb(text == "ok")
})
}


Task.prototype.save = function(cb) {
var task = this
$.post("/tasks/save", {json: this.toJSON(), current_user: "jonah"}, function(text) {
var o = JSON.parse(text)
task.saved = true
merge(task, o)
merge(task.original, o)
cb(task)
})
fn._saveStack = [
fn.validate,
fn._persist,
fn._saved
]


// model.find = function(id, cb) {
// $.get(model.url , {}, function(text) {
// cb(model.DB[id])
// }
// }
//
// model.count = function(cb) {
// var i=0;
// for(var id in model.DB) i++
// cb(i)
// }
//
// model.exists = function(id, cb) {
// cb(!!model.DB[id])
// }
//
// model.loadFromIds = function(ids, cb) {
// var ret=[];
// for(var id in ids) ret.push(model.DB[id])
// cb(ret)
// }
//
// model.all = function(cb) {
// cb(model.DB)
// }
}
68 changes: 68 additions & 0 deletions test/lib/app.js
@@ -0,0 +1,68 @@

function Task(o) {
this.original = {}
merge(this, o)
merge(this.original, o)
return
}

Task.properties = {
id: { type: "number", auto_inc: true },
user_id: {type: "string", required: true},
text: {type: "string", required: true },
week: { type: "number", required: true},
day: { type: "number", required: true},
done: { type: "number", "default": 0 },
created_at: { type: "date" },
updated_at: { type: "date" }
}

// Task.prototype.save = function(cb) {
// var o = {}, data
// var props = ["id", "done", "text"]
//
// for(var i=0; i < props.length;i++) {
// if(data = this[props[i]] == null) continue
// o[props[i]] = data
// }
// $.post("/tasks/save", {json: JSON.stringify(o)} function(data) {
// var task = new Task(data)
// cb(task)
// })
// }



Task.prototype.toData = function() {
var o = {}
for(var name in Task.properties) {
if(!(name in this)) continue
o[name] = this[name]
}
return o
}

Task.prototype.toJSON = function() {
return JSON.stringify(this.toData())
}



Task.prototype.destroy = function(cb) {
var task = this
$.post("/tasks/destroy", {json: this.toJSON(), current_user: "jonah"}, function(text) {
cb(text == "ok")
})
}


Task.prototype.save = function(cb) {
var task = this
$.post("/tasks/save", {json: this.toJSON(), current_user: "jonah"}, function(text) {
var o = JSON.parse(text)
task.saved = true
merge(task, o)
merge(task.original, o)
cb(task)
})
}

0 comments on commit d52c2bf

Please sign in to comment.