├── package.json ├── LICENSE ├── vue-localforage.js └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-localforage", 3 | "version": "0.2.4", 4 | "description": "A simple Vue plugin wrapped from localForage.", 5 | "main": "vue-localforage.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/shidianxia/vue-localforage.git" 12 | }, 13 | "keywords": [ 14 | "Vue", 15 | "localForage" 16 | ], 17 | "author": "shidianxia", 18 | "dependencies": { 19 | "vue": "^1.0.20", 20 | "localforage": "^1.4.0" 21 | }, 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/shidianxia/vue-localforage/issues" 25 | }, 26 | "homepage": "https://github.com/shidianxia/vue-localforage#readme" 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /vue-localforage.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * vue-localforage v0.2.4 3 | * shidianxia@gmail.com 4 | * Released under the MIT License. 5 | */ 6 | 'use strict' 7 | 8 | var localForage = require('localforage') 9 | 10 | function VueLocalForage (Vue) { 11 | 12 | Vue.prototype.$getItem = function (key, callback) { 13 | return localForage.getItem(key, callback) 14 | } 15 | 16 | Vue.prototype.$setItem = function (key, value, callback) { 17 | return localForage.setItem(key, value, callback) 18 | } 19 | 20 | Vue.prototype.$removeItem = function (key, callback) { 21 | return localForage.removeItem(key, callback) 22 | } 23 | 24 | Vue.prototype.$clearStorage = function () { 25 | localForage.clear() 26 | } 27 | 28 | Vue.prototype.$lengthOfStorage = function (callback) { 29 | return localForage.length(callback) 30 | } 31 | 32 | Vue.prototype.$keyInStorage = function (keyIndex, callback) { 33 | return localForage.key(keyIndex, callback) 34 | } 35 | 36 | Vue.prototype.$keysInStorage = function (callback) { 37 | return localForage.keys(callback) 38 | } 39 | 40 | Vue.prototype.$iterateStorage = function (iteratorCallback, callback) { 41 | return localForage.iterate(iteratorCallback, callback) 42 | } 43 | 44 | Vue.prototype.$setStorageDriver = function (driverName) { 45 | localForage.setDriver(driverName) 46 | } 47 | 48 | Vue.prototype.$storageConfig = function (options) { 49 | localForage.config(options) 50 | } 51 | 52 | var functions = [ 53 | '$getItem', 54 | '$setItem', 55 | '$removeItem', 56 | '$clearStorage', 57 | '$lengthOfStorage', 58 | '$keyInStorage', 59 | '$iterateStorage', 60 | '$setStorageDriver', 61 | '$storageConfig' 62 | ] 63 | 64 | Vue.localForage = {} 65 | functions.forEach(function (name) { 66 | Vue.localForage[name] = Vue.prototype[name] 67 | }) 68 | } 69 | 70 | VueLocalForage.version = '0.2.4' 71 | 72 | module.exports = VueLocalForage 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | *This package is looking for maintainer! If you are interested, please open an issue and leave your comment.* 2 | 3 | # vue-localforage 4 | A simple Vue plugin wrapped from localForage. 5 | 6 | ### Install 7 | 8 | ```bash 9 | npm install vue-localforage 10 | ``` 11 | 12 | ```js 13 | import VueLocalForage from 'vue-localforage' 14 | Vue.use(VueLocalForage) 15 | ``` 16 | 17 | ### Get item in localStorage 18 | 19 | ```js 20 | this.$getItem(key, callback) 21 | ``` 22 | 23 | ### Set item in localStorage 24 | 25 | ```js 26 | this.$setItem(key, value, callback) 27 | ``` 28 | 29 | ### Remove item in localStorage 30 | 31 | ```js 32 | this.$removeItem(key, callback) 33 | ``` 34 | 35 | ### Clean all the object in localStorage 36 | 37 | ```js 38 | this.$clearStorage 39 | ``` 40 | 41 | ### Gets the number of keys in localStorage 42 | 43 | ```js 44 | this.$lengthOfStorage 45 | ``` 46 | 47 | ### Get the name of a key based on its ID 48 | 49 | ```js 50 | this.$keyInStorage(keyIndex, callback) 51 | ``` 52 | 53 | ### Get the list of all keys in localStorage 54 | 55 | ```js 56 | this.$keysInStorage(callback) 57 | ``` 58 | 59 | ### Iterate over all value/key pairs 60 | 61 | ```js 62 | this.$iterateStorage(function (value, key, iterationNumber) { 63 | //console.log all the kay/value pairs 64 | console.log([key, value]); 65 | }, function (err) { 66 | if (!err) { 67 | console.log('Iteration has completed'); 68 | } 69 | }) 70 | ``` 71 | 72 | ### Force usage of a particular driver or drivers, if available. 73 | 74 | ```js 75 | this.$setStorageDriver(localforage.LOCALSTORAGE) 76 | ``` 77 | 78 | By default, localForage selects backend drivers for the datastore in this order: 79 | 80 | 1. IndexedDB 81 | 2. WebSQL 82 | 3. localStorage 83 | 84 | If you would like to force usage of a particular driver you can use $setStorageDriver() with one or more of the following parameters. 85 | 86 | localforage.INDEXEDDB 87 | 88 | localforage.WEBSQL 89 | 90 | localforage.LOCALSTORAGE 91 | 92 | 93 | ### Set and persist localForage options. 94 | 95 | ```js 96 | this.$storageConfig({ 97 | driver: localforage.LOCALSTORAGE, 98 | name: 'Name-of-localStorage' 99 | }); 100 | ``` 101 | 102 | This must be called before any other calls to localForage are made, but can be called after localForage is loaded. 103 | 104 | ##### driver 105 | The preferred driver(s) to use. Same format as what is passed to `setStorageDriver()`, above. 106 | Default: `[localforage.INDEXEDDB, localforage.WEBSQL, localforage.LOCALSTORAGE]` 107 | ##### name 108 | The name of the database. May appear during storage limit prompts. Useful to use the name of your app here. In localStorage, this is used as a key prefix for all keys stored in localStorage. 109 | Default: `'localforage'` 110 | ##### size 111 | The size of the database in bytes. Used only in WebSQL for now. 112 | Default: `4980736` 113 | ##### storeName 114 | The name of the datastore. In IndexedDB this is the dataStore, in WebSQL this is the name of the key/value table in the database. Must be alphanumeric, with underscores. Any non-alphanumeric characters will be converted to underscores. 115 | Default: `'keyvaluepairs'` 116 | ##### version 117 | The version of your database. May be used for upgrades in the future; currently unused. 118 | Default: `1.0` 119 | ##### description 120 | A description of the database, essentially for developer usage. 121 | Default: `''` 122 | 123 | 124 | --------------------------------------------------------------------------------