+ There are {todos.length} total todos
+
+
+ )
+}
+Graph.propTypes = {
+ todos: React.PropTypes.array,
+}
diff --git a/src/store.js b/src/store.js
index 0278006..c3b97c2 100644
--- a/src/store.js
+++ b/src/store.js
@@ -1,3 +1,4 @@
+import {remove} from './helpers'
export default Store
/**
@@ -23,6 +24,16 @@ function Store(name, callback) {
}
callback.call(this, JSON.parse(localStorage[name]))
+ this.subscribers = []
+}
+
+Store.prototype.subscribe = function(subscriber) {
+ this.subscribers.push(subscriber)
+ return () => remove(this.subscribers, subscriber)
+}
+
+Store.prototype._notify = function() {
+ this.subscribers.forEach(s => s())
}
/**
@@ -102,6 +113,7 @@ Store.prototype.save = function(updateData, callback, id) {
localStorage[this._dbName] = JSON.stringify(data)
callback.call(this, [updateData])
}
+ this._notify()
}
/**
@@ -123,6 +135,7 @@ Store.prototype.remove = function(id, callback) {
localStorage[this._dbName] = JSON.stringify(data)
callback.call(this, JSON.parse(localStorage[this._dbName]).todos)
+ this._notify()
}
/**
@@ -133,4 +146,5 @@ Store.prototype.remove = function(id, callback) {
Store.prototype.drop = function(callback) {
localStorage[this._dbName] = JSON.stringify({todos: []})
callback.call(this, JSON.parse(localStorage[this._dbName]).todos)
+ this._notify()
}
diff --git a/src/todo.js b/src/todo.js
new file mode 100644
index 0000000..e51cf36
--- /dev/null
+++ b/src/todo.js
@@ -0,0 +1,41 @@
+import View from './view'
+import Controller from './controller'
+import Model from './model'
+import Store from './store'
+import Template from './template'
+import {remove} from './helpers'
+
+export {updateTodo, getTodo, subscribe}
+
+let todo
+const subscribers = []
+
+/**
+ * Sets up a brand new Todo list.
+ *
+ * @param {string} name The name of your new to do list.
+ */
+function Todo(name) {
+ this.storage = new Store(name)
+ this.model = new Model(this.storage)
+ this.template = new Template()
+ this.view = new View(this.template)
+ this.controller = new Controller(this.model, this.view)
+}
+
+function updateTodo() {
+ todo = new Todo('todos-vanillajs')
+ todo.controller.setView(document.location.hash)
+ subscribers.forEach(s => s())
+}
+
+function getTodo() {
+ return todo
+}
+
+function subscribe(cb) {
+ subscribers.push(cb)
+ return function unsubscribe() {
+ remove(subscribers, cb)
+ }
+}
diff --git a/webpack.config.babel.js b/webpack.config.babel.js
index 6818b09..af5e69c 100644
--- a/webpack.config.babel.js
+++ b/webpack.config.babel.js
@@ -1,5 +1,6 @@
/* eslint no-console:"off" */
const {resolve} = require('path')
+const ProgressBarPlugin = require('progress-bar-webpack-plugin')
const webpackValidator = require('webpack-validator')
const {getIfUtils} = require('webpack-config-utils')
@@ -21,6 +22,9 @@ module.exports = env => {
{test: /\.css$/, loaders: ['style', 'css']},
],
},
+ plugins: [
+ new ProgressBarPlugin()
+ ],
})
if (env.debug) {
console.log(config)