diff options
| -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){ |
