Skip to content

Instantly share code, notes, and snippets.

@tshm
Created March 20, 2012 12:45
Show Gist options
  • Save tshm/2134907 to your computer and use it in GitHub Desktop.
Save tshm/2134907 to your computer and use it in GitHub Desktop.

Revisions

  1. tshm revised this gist Apr 12, 2012. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions agility-localStorage.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,5 @@
    /*
    [MIT licensed](http://en.wikipedia.org/wiki/MIT_License)
    (c) [Toshihide Shimayama](http://github.com/tshm/todomvc/)
    */
    // custom agilityjs adapter for localstorage
    (function($$, console) {
  2. tshm revised this gist Apr 12, 2012. 1 changed file with 44 additions and 32 deletions.
    76 changes: 44 additions & 32 deletions agility-localStorage.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,44 @@
    // custom agility adapter for localStorage
    $$.adapter.localStorage = function(_params) {
    var key = (this._data.persist.baseUrl || '') + this._data.persist.collection;
    var value = localStorage[key];
    var items = (value && value.length > 0 ? JSON.parse(value) : []);
    switch (_params.type) {
    case 'GET':
    if (_params.id) { // normal get
    if (items[_params.id]) {
    _params.success(items[_params.id]);
    } else {
    _params.error();
    }
    } else { // gather call
    _params.success(items);
    }
    break;
    case 'DELETE':
    _params.data = undefined; // continue into POST case
    case 'PUT': // continue into POST case
    case 'POST':
    if (!_params.id) {
    _params.id = items.length;
    _params.data.id = _params.id;
    }
    items[_params.id] = _params.data;
    //_params.success({id:_params.id});
    localStorage[key] = JSON.stringify(items);
    break;
    }
    _params.complete();
    };
    /*
    [MIT licensed](http://en.wikipedia.org/wiki/MIT_License)
    (c) [Toshihide Shimayama](http://github.com/tshm/todomvc/)
    */
    // custom agilityjs adapter for localstorage
    (function($$, console) {
    'use strict';

    $$.adapter.localStorage = function(_params) {
    var storageKey = (this._data.persist.baseUrl || '') + this._data.persist.collection,
    storageStr = localStorage[storageKey],
    items = (storageStr ? JSON.parse(storageStr) : {});
    //
    if ('GET' === _params.type) {
    if (undefined !== _params.id) { // normal get
    if ('object' === typeof items[_params.id]) {
    _params.success(items[_params.id]);
    } else {
    _params.error();
    }
    } else { // gather call
    _params.success(items);
    }
    } else if ('DELETE' === _params.type) {
    delete items[_params.id];
    localStorage[storageKey] = JSON.stringify(items);
    } else if ('PUT' === _params.type || 'POST' === _params.type) {
    if (undefined === _params.id) {
    _params.id = (new Date()).getTime();
    _params.data.id = _params.id;
    }
    items[_params.id] = _params.data;
    //_params.success({id:_params.id});
    localStorage[storageKey] = JSON.stringify(items);
    } else {
    _params.error();
    }
    _params.complete();
    };

    })(window.agility, window.console);

  3. tshm revised this gist Mar 20, 2012. 1 changed file with 32 additions and 34 deletions.
    66 changes: 32 additions & 34 deletions agility-localStorage.js
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,32 @@
    // custom agility adapter for localStorage
    $$.adapter.localStorage = function(_params) {
    var key = (this._data.persist.baseUrl || '') + this._data.persist.collection;
    var value = localStorage[key];
    var items = (value && value.length > 0 ? JSON.parse(value) : []);
    console.log(_params, items);
    switch (_params.type) {
    case 'GET':
    if (_params.id) { // normal get
    if (items[_params.id]) {
    _params.success(items[_params.id]);
    } else {
    _params.error();
    }
    } else { // gather call
    console.log(items);
    _params.success(items);
    }
    break;
    case 'DELETE':
    _params.data = undefined; // continue into POST case
    case 'PUT': // continue into POST case
    case 'POST':
    if (!_params.id) {
    _params.id = items.length;
    _params.data.id = _params.id;
    }
    items[_params.id] = _params.data;
    //_params.success({id:_params.id});
    localStorage[key] = JSON.stringify(items);
    break;
    }
    _params.complete();
    }
    // custom agility adapter for localStorage
    $$.adapter.localStorage = function(_params) {
    var key = (this._data.persist.baseUrl || '') + this._data.persist.collection;
    var value = localStorage[key];
    var items = (value && value.length > 0 ? JSON.parse(value) : []);
    switch (_params.type) {
    case 'GET':
    if (_params.id) { // normal get
    if (items[_params.id]) {
    _params.success(items[_params.id]);
    } else {
    _params.error();
    }
    } else { // gather call
    _params.success(items);
    }
    break;
    case 'DELETE':
    _params.data = undefined; // continue into POST case
    case 'PUT': // continue into POST case
    case 'POST':
    if (!_params.id) {
    _params.id = items.length;
    _params.data.id = _params.id;
    }
    items[_params.id] = _params.data;
    //_params.success({id:_params.id});
    localStorage[key] = JSON.stringify(items);
    break;
    }
    _params.complete();
    };
  4. tshm created this gist Mar 20, 2012.
    34 changes: 34 additions & 0 deletions agility-localStorage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // custom agility adapter for localStorage
    $$.adapter.localStorage = function(_params) {
    var key = (this._data.persist.baseUrl || '') + this._data.persist.collection;
    var value = localStorage[key];
    var items = (value && value.length > 0 ? JSON.parse(value) : []);
    console.log(_params, items);
    switch (_params.type) {
    case 'GET':
    if (_params.id) { // normal get
    if (items[_params.id]) {
    _params.success(items[_params.id]);
    } else {
    _params.error();
    }
    } else { // gather call
    console.log(items);
    _params.success(items);
    }
    break;
    case 'DELETE':
    _params.data = undefined; // continue into POST case
    case 'PUT': // continue into POST case
    case 'POST':
    if (!_params.id) {
    _params.id = items.length;
    _params.data.id = _params.id;
    }
    items[_params.id] = _params.data;
    //_params.success({id:_params.id});
    localStorage[key] = JSON.stringify(items);
    break;
    }
    _params.complete();
    }