Created
January 30, 2014 14:41
-
-
Save objectivehtml/8709951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| "use strict"; | |
| Timeblocker.Storage.Base = Marionette.Controller.extend({ | |
| // The core localStorageDb object | |
| api: false, | |
| // Table columns | |
| columns: [], | |
| // The database name | |
| database: 'Timeblocker', | |
| // Storage Engine (localStorage|sessionStorage) | |
| engine: localStorage, | |
| // The name of the table | |
| name: false, | |
| /* | |
| * Initialize the table | |
| * | |
| * @param object options | |
| * @return void | |
| */ | |
| initialize: function(options) { | |
| // Fill the object with the options | |
| this.fill(options); | |
| // Instantiate the API | |
| this.api = new localStorageDB(this.database, this.engine); | |
| // Is the table new | |
| if(this.isNew()) { | |
| // Create the table | |
| this.createTable(); | |
| // Commit to the db | |
| this.commit(); | |
| } | |
| }, | |
| /* | |
| * Commit the changes to the db | |
| * | |
| * @return void | |
| */ | |
| commit: function() { | |
| this.api.commit(); | |
| }, | |
| /* | |
| * Create the table | |
| * | |
| * @param array columns | |
| * @return void | |
| */ | |
| createTable: function(columns) { | |
| if(columns) { | |
| this.columns = columns; | |
| } | |
| this.api.createTable(this.name, this.columns); | |
| this.onTableCreate(); | |
| }, | |
| /* | |
| * Delete the rows from the table | |
| * | |
| * @param object query | |
| * @return void | |
| */ | |
| deleteRows: function(query) { | |
| this.api.deleteRows(this.name, query); | |
| }, | |
| /* | |
| * Assign properties by passing an object | |
| * | |
| * @param object data | |
| * @return void | |
| */ | |
| fill: function(data) { | |
| _.extend(this, data); | |
| }, | |
| /* | |
| * Insert data into the table | |
| * | |
| * @param object data | |
| * @return int | |
| */ | |
| insert: function(data) { | |
| return this.api.insert(this.name, data); | |
| }, | |
| /* | |
| * Insert or update data into the table | |
| * | |
| * @param object data | |
| * @param object query | |
| * @return int | |
| */ | |
| insertOrUpdate: function(data, query) { | |
| return this.api.insertOrUpdate(this.name, data, query); | |
| }, | |
| /* | |
| * Returns TRUE if the table is new | |
| * | |
| * @return bool | |
| */ | |
| isNew: function() { | |
| return this.api.isNew(); | |
| }, | |
| /* | |
| * On table creation callback | |
| * | |
| * @return void | |
| */ | |
| onTableCreate: function() {}, | |
| /* | |
| * Query the database | |
| * | |
| * @param mixed query | |
| * @return object | |
| */ | |
| query: function(data) { | |
| return this.api.query(this.name, data); | |
| }, | |
| /* | |
| * Get the row count | |
| * | |
| * @return int | |
| */ | |
| rowCount: function() { | |
| return this.api.rowCount(); | |
| }, | |
| /* | |
| * Serialize the data into a JSON array | |
| * | |
| * @return array | |
| */ | |
| serialize: function() { | |
| return this.api.serialize(); | |
| }, | |
| /* | |
| * Does the table exist | |
| * | |
| * @return bool | |
| */ | |
| tableExists: function() { | |
| return this.api.tableExists(); | |
| }, | |
| /* | |
| * Delete all records and reset ID to 0 | |
| * | |
| * @return bool | |
| */ | |
| truncate: function() { | |
| return this.api.truncate(); | |
| }, | |
| /* | |
| * Update rows found in the query with the passed data | |
| * | |
| * @param object data | |
| * @param mixed query | |
| * @return | |
| */ | |
| update: function(data, query) { | |
| return this.api.update(this.name, data, query); | |
| } | |
| }); | |
| }()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| "use strict"; | |
| Timeblocker.Storage.UserPreferences = Timeblocker.Storage.Base.extend({ | |
| columns: ['key', 'value'], | |
| name: 'preferences', | |
| set: function(key, value) { | |
| var data = { | |
| key: key, | |
| value: value | |
| }; | |
| var _return = this.insertOrUpdate({key: key}, data); | |
| this.commit(); | |
| return _return; | |
| }, | |
| get: function(key) { | |
| var query = this.query({key: key}); | |
| if(!query[0]) { | |
| return null; | |
| } | |
| return query[0].value; | |
| }, | |
| exists: function(key) { | |
| var query = this.get(key); | |
| if(query.length == 0) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| }); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment