瀏覽代碼

Merge pull request #89 from ryangjchandler/fix/array-watchers

fix: array watchers
Ryan Chandler 4 年之前
父節點
當前提交
3959a02cab

文件差異過大導致無法顯示
+ 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


+ 25 - 0
examples/index.html

@@ -26,6 +26,19 @@
             <p x-text="$store.colorScheme"></p>
         </div>
 
+        <div x-data>
+            <input type="text" x-model="$store.todo.new">
+            <button @click="$store.todo.add($store.todo.new)">
+                Add Todo
+            </button>
+
+            <ul>
+                <template x-for="todo in $store.todo.todos">
+                    <li x-text="todo"></li>
+                </template>
+            </ul>
+        </div>
+
         <script src="../dist/spruce.umd.js"></script>
 
         <script>
@@ -45,6 +58,14 @@
 
             Spruce.store('colorScheme', 'light', true)
 
+            Spruce.store('todo', {
+                new: '',
+                todos: [],
+                add(todo) {
+                    this.todos.push(todo)
+                }
+            })
+
             Spruce.watch('application.name', () => {
                 Spruce.store('persisted').example = 'World';
             });
@@ -60,6 +81,10 @@
             Spruce.started(function () {
                 console.log('started...')
             })
+
+            Spruce.watch('todo.todos', function () {
+                console.log('Added todo')
+            })
         </script>
     </body>
 </html>

+ 2 - 0
src/index.js

@@ -141,6 +141,8 @@ const Spruce = {
     },
 
     runWatchers(stores, target, key, value) {
+        key = target['__key_name'] || key
+
         const self = this
 
         if (self.watchers[key]) {

+ 4 - 0
src/observable.js

@@ -3,6 +3,10 @@ import { isNullOrUndefined, isObject, isArray } from './utils'
 export const createObservable = (target, callbacks) => {
     Object.entries(target).forEach(([key, value]) => {
         if (! isNullOrUndefined(value) && (isObject(value) || isArray(value))) {
+            if (isArray(value)) {
+                value['__key_name'] = key
+            }
+            
             target[key] = createObservable(value, callbacks)
         }
     })

+ 27 - 2
tests/cypress/fixtures/watchers/init.html

@@ -4,17 +4,42 @@
     </head>
     <body>
         <p x-data x-text="$store.persisted.foo"></p>
-        <button x-data @click="$store.persisted.foo = 'car'"></button>
-        <span x-data x-text="$store.persisted.bar"></span>
+        <button data-change-foo x-data @click="$store.persisted.foo = 'car'"></button>
+        <span data-bar x-data x-text="$store.persisted.bar"></span>
+
+        <div x-data>
+            <ul>
+                <template x-for="todo in $store.todo.todos">
+                    <li data-todo-item x-text="todo"></li>
+                </template>
+            </ul>
+            <button data-add-todo @click="$store.todo.add('Example')">Add Todo</button>
+            <span data-todo-label x-text="$store.todoLabel.test"></span>
+        </div>
 
         <script src="/dist/spruce.umd.js"></script>
 
         <script>
+            Spruce.store('todo', {
+                todos: [],
+                add(todo) {
+                    this.todos.push(todo)
+                }
+            })
+
+            Spruce.store('todoLabel', {
+                test: 'Yes!'
+            })
+
             Spruce.store('persisted', {
                 foo: 'bar',
                 bar: 'boo'
             }, true)
 
+            Spruce.watch('todo.todos', () => {
+                Spruce.store('todoLabel').test = 'Yay!'
+            })
+
             Spruce.watch('persisted.foo', () => {
                 Spruce.store('persisted').bar = 'bop'
             })

+ 11 - 3
tests/cypress/integration/watchers.spec.js

@@ -5,11 +5,19 @@ describe('watchers', () => {
         cy.visit('/tests/cypress/fixtures/watchers/init.html')
 
         cy.get('p').should('have.text', 'bar')
-        cy.get('span').should('have.text', 'boo')
+        cy.get('[data-bar]').should('have.text', 'boo')
 
-        cy.get('button').click()
+        cy.get('[data-change-foo]').click()
 
         cy.get('p').should('have.text', 'car')
-        cy.get('span').should('have.text', 'bop')
+        cy.get('[data-bar]').should('have.text', 'bop')
+
+        cy.get('[data-todo-item]').should('have.length', 0)
+        cy.get('[data-todo-label]').should('have.text', 'Yes!')
+
+        cy.get('[data-add-todo]').click()
+
+        cy.get('[data-todo-item]').should('have.length', 1)
+        cy.get('[data-todo-label]').should('have.text', 'Yay!')
     })
 })

部分文件因文件數量過多而無法顯示