Last active
August 29, 2015 14:27
-
-
Save mattmccray/7fbfcdeeca4468982081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid from 'node-uuid' | |
import {makeReactive} from 'mobservable' | |
export class NoteStore { | |
constructor(initialNotes=[]) { | |
this.notes = makeReactive(initialNotes) | |
} | |
addNote({task}) { | |
this.notes.push({id: uuid.v4(), task}) | |
} | |
editNote(id, task) { | |
const notes = this.notes | |
const noteIndex = this.findNote(id) | |
if(noteIndex < 0) { | |
return | |
} | |
this.notes[noteIndex].task = task | |
} | |
deleteNote(id) { | |
const notes = this.notes | |
const noteIndex = this.findNote(id) | |
if(noteIndex < 0) { | |
return | |
} | |
this.notes.splice(noteIndex, 1) | |
} | |
findNote(id) { | |
const notes = this.notes | |
const noteIndex = notes.findIndex((note) => note.id === id) | |
if(noteIndex < 0) { | |
console.warn('Failed to find note', notes, id) | |
} | |
return noteIndex | |
} | |
} | |
export default new NoteStore([ | |
{ | |
id: uuid.v4(), | |
task: 'Learn webpack' | |
}, | |
{ | |
id: uuid.v4(), | |
task: 'Learn React' | |
}, | |
{ | |
id: uuid.v4(), | |
task: 'Do laundry' | |
} | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use the module as the 'Container' | |
import uuid from 'node-uuid'; | |
import {makeReactive} from 'mobservable'; | |
export let notes = makeReactive([ | |
{ | |
id: uuid.v4(), | |
task: 'Learn webpack' | |
}, | |
{ | |
id: uuid.v4(), | |
task: 'Learn React' | |
}, | |
{ | |
id: uuid.v4(), | |
task: 'Do laundry' | |
} | |
]) | |
export function addNote({task}) { | |
notes.push({id: uuid.v4(), task}) | |
} | |
export function editNote(id, task) { | |
const noteIndex = findNote(id) | |
if(noteIndex < 0) { | |
return | |
} | |
notes[noteIndex].task = task | |
} | |
export function deleteNote(id) { | |
const noteIndex = findNote(id) | |
if(noteIndex < 0) { | |
return | |
} | |
notes.splice(noteIndex, 1) | |
} | |
export function findNote(id) { | |
const noteIndex = notes.findIndex( note => note.id === id ) | |
if(noteIndex < 0) { | |
console.warn('Failed to find note', notes, id) | |
} | |
return noteIndex | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment