diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-09-06 14:52:13 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-09-06 14:52:13 -0400 |
| commit | 4d64e470a884d3c892e84be2005343e3b0c000a6 (patch) | |
| tree | f5a8bff142d4a55168caa08e6ede456a89dd4017 | |
| parent | f88f58a247ac263dc0178d8c515e471ffadc197b (diff) | |
post/put/delete methods
| -rw-r--r-- | themes/okadmin/public/js/okcms.js | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/themes/okadmin/public/js/okcms.js b/themes/okadmin/public/js/okcms.js index fe0ed69..9c8c6e4 100644 --- a/themes/okadmin/public/js/okcms.js +++ b/themes/okadmin/public/js/okcms.js @@ -1,7 +1,8 @@ + var OKCMS = (function(){ - var root = window.location.pathname.split("/")[1] - var dbPath = [ "", root, "index.json" ].join("/") + var adminRoot = window.location.pathname.split("/")[1] + var adminDB = [ "", adminRoot, "index.json" ].join("/") var OKCMS = function(opt){ this.opt = Object.assign({ @@ -10,9 +11,9 @@ var OKCMS = (function(){ this.fetch() } OKCMS.prototype.fetch = function(){ - var request = $.ajax({ - url: dbPath, - type: "get", + $.ajax({ + url: adminDB, + type: "GET", success: this.load.bind(this) }) } @@ -153,20 +154,51 @@ var OKCMS = (function(){ return this.keys(field.key) } } - OKCMS.prototype.save = function(type, data){ + OKCMS.prototype.save = function(type, data, cb){ + if (! data.id) { + this.create(type, data, cb) + } else { + this.update(type, data, cb) + } } - OKCMS.prototype.create = function(type, data){ + OKCMS.prototype.create = function(type, data, cb){ + var resource = this.getResource(type) if (! data.title) { throw new Error ("Title field is empty") } if (! data.id) { data.id = slugify( data.title ) } + $.ajax({ + url: [ "", adminRoot, type ].join("/"), + type: "POST", + success: function(){ + cb && cb() + } + }) } - OKCMS.prototype.update = function(type, data){ + OKCMS.prototype.update = function(type, data, cb){ + var resource = this.getResource(type) var id = data.id + + // /admin/:type/:id/ + $.ajax({ + url: [ "", adminRoot, type, id ].join("/"), + type: "PUT", + data: data, + success: function(){ + cb && cb() + } + }) } - OKCMS.prototype.destroy = function(type, id){ + OKCMS.prototype.destroy = function(type, id, cb){ + $.ajax({ + url: [ "", adminRoot, type, id ].join("/"), + type: "DELETE", + success: function(){ + cb && cb() + } + }) } function slugify (s){ |
