Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Last active August 29, 2015 14:27
Show Gist options
  • Save mattmccray/7fbfcdeeca4468982081 to your computer and use it in GitHub Desktop.
Save mattmccray/7fbfcdeeca4468982081 to your computer and use it in GitHub Desktop.
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'
}
])
// 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