From 56e0ee073ac037064d924e2d16d05d7390e22d74 Mon Sep 17 00:00:00 2001 From: yang-wei Date: Sun, 12 Jul 2015 14:19:51 +0900 Subject: [PATCH] Store as data close #1 --- src/store/ItemStore.js | 25 ++++++++++++++++--------- src/view/items-view.js | 5 +---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/store/ItemStore.js b/src/store/ItemStore.js index b87d42b..4dcdbfb 100644 --- a/src/store/ItemStore.js +++ b/src/store/ItemStore.js @@ -1,27 +1,34 @@ import MicroEvents from 'microevent-github' import AppDispatcher from '../AppDispatcher' -const _items = [{title: 'Item #1'}, {title: 'Item #2'}, {title: 'Item #3'}] +const _items = [{title: 'Item #1'}, {title: 'Item #2'}, {title: 'Item #3'}] + +class itemStore { + constructor(items = []) { + this.items = items + this.newItem = { title: ''} + } -const ItemStore = { addItem(item) { - _items.push( item ) - }, + this.items.push( item ) + } removeItem(item) { - let index = _items.indexOf(item); - _items.splice(index, 1); - }, + const index = this.items.indexOf(item); + this.items.splice(index, 1); + } resetItem() { return { title: ''} - }, + } getAll() { - return _items + return this.items } } +const ItemStore = new itemStore(_items) + MicroEvents.mixin(ItemStore) AppDispatcher.register( payload => { diff --git a/src/view/items-view.js b/src/view/items-view.js index 236af94..0be9688 100644 --- a/src/view/items-view.js +++ b/src/view/items-view.js @@ -14,10 +14,7 @@ const itemView = { `, data() { - return { - items: [], - newItem: '' - } + return ItemStore }, compiled() { this.update()