Skip to content

Instantly share code, notes, and snippets.

@scott-w
Last active April 26, 2017 17:54
Show Gist options
  • Save scott-w/3a7d3411f0f732de1500aabdf94dc7ee to your computer and use it in GitHub Desktop.
Save scott-w/3a7d3411f0f732de1500aabdf94dc7ee to your computer and use it in GitHub Desktop.
Root View for a simple note-taking app
import {View} from 'backbone.marionette';
import {template} from 'underscore';
import {NoteCollection} from './notes';
import {NoteModel} from './note';
import {NoteListView} from './list';
import {NoteView} from './detail';
export const RootView = View.extend({
template: template('<div class="primary-hook"></div>'),
regions: {
primary: '.primary-hook',
},
/** This listens to events on its child views to figure out what
* we should be showing to the user.
*/
childViewEvents: {
'detail:back': 'showNoteList',
'note:create': 'showCreateNote',
'note:list': 'showNoteList',
'note:view': 'showNote'
},
/** Shows the list on our first render */
onRender() {
this.showNoteList();
},
/** Shows the full note list */
showNoteList() {
const notes = new NoteCollection();
notes.fetch();
this.showChildView('primary', new NoteListView({
collection: notes
}));
},
/** Show a single note - we need to pass a note model in every
* time for this to work
*/
showNote(note) {
this.showChildView('primary', new NoteView({
model: note
}));
},
/** Create a new note with the note view */
showCreateNote() {
const note = new NoteModel();
this.showNote(note);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment