Skip to content

Commit

Permalink
version 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pinguinjkeke committed Oct 16, 2017
1 parent 2d3e1d2 commit c1bd2bd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-localstorage",
"version": "0.4.2",
"version": "0.5.0",
"description": "Vue.js localStorage plugin with types support",
"main": "vue-localstorage.js",
"authors": [
Expand Down
41 changes: 36 additions & 5 deletions dist/vue-local-storage.es2015.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vue-local-storage v0.4.2
* vue-local-storage v0.5.0
* (c) 2017 Alexander Avakov
* @license MIT
*/
Expand Down Expand Up @@ -74,13 +74,16 @@ class VueLocalStorage {
* @param {function} type
* @param {*} defaultValue
*/
addProperty (key, type, defaultValue) {
addProperty (key, type, defaultValue = undefined) {
type = type || String;

this._properties[key] = { type };

if (!window.localStorage[key] && defaultValue !== null) {
window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue);
window.localStorage.setItem(
key,
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue
);
}
}

Expand Down Expand Up @@ -148,14 +151,42 @@ var index = {
}

const name = options.name || 'localStorage';
const bind = options.bind;

Vue.mixin({
created () {
beforeCreate () {
if (this.$options[name]) {
Object.keys(this.$options[name]).forEach((key) => {
const [type, defaultValue] = [this.$options[name][key].type, this.$options[name][key].default];
const config = this.$options[name][key];
const [type, defaultValue] = [config.type, config.default];

VueLocalStorage$1.addProperty(key, type, defaultValue);

const existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key);

if (!existingProp) {
const prop = {
get: () => Vue.localStorage.get(key, defaultValue),
set: val => Vue.localStorage.set(key, val),
configurable: true
};

Object.defineProperty(VueLocalStorage$1, key, prop);
Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue);
} else if (!Vue.config.silent) {
console.log(`${key}: is already defined and will be reused`);
}

if ((bind || config.bind) && config.bind !== false) {
this.$options.computed = this.$options.computed || {};

if (!this.$options.computed[key]) {
this.$options.computed[key] = {
get: () => Vue.localStorage[key],
set: (val) => { Vue.localStorage[key] = val; }
};
}
}
});
}
}
Expand Down
41 changes: 37 additions & 4 deletions dist/vue-local-storage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vue-local-storage v0.4.2
* vue-local-storage v0.5.0
* (c) 2017 Alexander Avakov
* @license MIT
*/
Expand Down Expand Up @@ -82,12 +82,17 @@ VueLocalStorage.prototype.remove = function remove (lsKey) {
* @param {*} defaultValue
*/
VueLocalStorage.prototype.addProperty = function addProperty (key, type, defaultValue) {
if ( defaultValue === void 0 ) defaultValue = undefined;

type = type || String;

this._properties[key] = { type: type };

if (!window.localStorage[key] && defaultValue !== null) {
window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue);
window.localStorage.setItem(
key,
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue
);
}
};

Expand Down Expand Up @@ -156,18 +161,46 @@ var index = {
}

var name = options.name || 'localStorage';
var bind = options.bind;

Vue.mixin({
created: function created () {
beforeCreate: function beforeCreate () {
var this$1 = this;

if (this.$options[name]) {
Object.keys(this.$options[name]).forEach(function (key) {
var ref = [this$1.$options[name][key].type, this$1.$options[name][key].default];
var config = this$1.$options[name][key];
var ref = [config.type, config.default];
var type = ref[0];
var defaultValue = ref[1];

VueLocalStorage$1.addProperty(key, type, defaultValue);

var existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key);

if (!existingProp) {
var prop = {
get: function () { return Vue.localStorage.get(key, defaultValue); },
set: function (val) { return Vue.localStorage.set(key, val); },
configurable: true
};

Object.defineProperty(VueLocalStorage$1, key, prop);
Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue);
} else if (!Vue.config.silent) {
console.log((key + ": is already defined and will be reused"));
}

if ((bind || config.bind) && config.bind !== false) {
this$1.$options.computed = this$1.$options.computed || {};

if (!this$1.$options.computed[key]) {
this$1.$options.computed[key] = {
get: function () { return Vue.localStorage[key]; },
set: function (val) { Vue.localStorage[key] = val; }
};
}
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/vue-local-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue-localstorage",
"description": "Vue.js localStorage plugin with types support",
"version": "0.4.2",
"version": "0.5.0",
"author": {
"name": "Alexander Avakov",
"email": "[email protected]"
Expand Down

0 comments on commit c1bd2bd

Please sign in to comment.