Эх сурвалжийг харах

fix(allow methods to be stored on persisted stores)

Ryan Chandler 4 жил өмнө
parent
commit
0cbf855eda

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.js.map


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.module.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.module.js.map


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.umd.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.umd.js.map


+ 7 - 1
examples/index.html

@@ -12,6 +12,9 @@
         <div x-data>
             <code>Persisted</code>
             <input type="text" x-model="$store.persisted.example">
+            <button type="button" @click="$store.persisted.method()">
+                Call method on persisted store
+            </button>
         </div>
 
         <script>
@@ -23,7 +26,10 @@
             })
 
             Spruce.store('persisted', {
-                example: 'Hello'
+                example: 'Hello',
+                method() {
+                    console.log('test')
+                }
             }, true)
         </script>
     </body>

+ 8 - 27
src/index.js

@@ -1,4 +1,4 @@
-import { domReady } from './utils'
+import { domReady, getMethods } from './utils'
 import { createObservable } from './observable'
 
 const Spruce = {
@@ -26,9 +26,7 @@ const Spruce = {
                 this.updateSubscribers()
 
                 this.disableReactivity = true
-
                 this.persisted.forEach(this.updateLocalStorage.bind(this))
-
                 this.disableReactivity = false
             }
         })
@@ -50,9 +48,11 @@ const Spruce = {
 
     store(name, state, persist = false) {
         if (persist) {
-            this.stores[name] = this.retrieveFromLocalStorage(name)
+            this.stores[name] = this.retrieveFromLocalStorage(name, getMethods(state))
 
-            this.persisted.push(name)
+            if (! this.persisted.includes(name)) {
+                this.persisted.push(name)
+            }
         }
 
         if (! this.stores[name]) {
@@ -80,33 +80,14 @@ const Spruce = {
         })
     },
 
-    retrieveFromLocalStorage(name) {
+    retrieveFromLocalStorage(name, methods = {}) {
         const storage = JSON.parse(window.localStorage.getItem(`__spruce:${name}`))
 
-        if (! storage) {
-            return null
-        }
-
-        return storage
+        return storage ? Object.assign(methods, storage) : null
     },
 
     updateLocalStorage(name) {
-        window.localStorage.setItem(
-            `__spruce:${name}`,
-            JSON.stringify(this.store(name))
-        )
-    },
-
-    persist(name) {
-        if (! this.persisted.includes(name)) {
-            this.persisted.push(name)
-        }
-
-        this.updateLocalStorage(name)
-    },
-
-    dontPersist(name) {
-        this.persisted = this.persisted.filter(_ => name !== _)
+        window.localStorage.setItem(`__spruce:${name}`, JSON.stringify(this.store(name)))
     }
 }
 

+ 13 - 1
src/utils.js

@@ -16,4 +16,16 @@ export const isObject = _ => {
     return Object.getPrototypeOf(_) === Object.prototype
 }
 
-export const isArray = _ => Array.isArray(_)
+export const isArray = _ => Array.isArray(_)
+
+export const getMethods = obj => {
+    let methods = {}
+
+    Object.entries(obj).forEach(([key, value]) => {
+        if (typeof value === 'function') {
+            methods[key] = value
+        }
+    })
+
+    return methods
+}

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно