From 4ab93c8e44a48cd254c166fe1bffd93fd050df5c Mon Sep 17 00:00:00 2001 From: Dmytro Yarmak Date: Wed, 5 Feb 2014 11:37:47 +0200 Subject: [PATCH] Use Marionette.Application --- app/js/app.js | 93 +++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/app/js/app.js b/app/js/app.js index 5a2d124..e353f59 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -1,62 +1,69 @@ -window.ContactManager = { +var ContactManager = new Marionette.Application({ Models: {}, Collections: {}, - Views: {}, + Views: {} +}); - start: function(data) { - var contacts = new ContactManager.Collections.Contacts(data.contacts), - router = new ContactManager.Router(), - mainRegion = new Marionette.Region({el: '.main-container'}); +ContactManager.addRegions({ + mainRegion: '.main-container' +}); - router.on('route:home', function() { - router.navigate('contacts', { - trigger: true, - replace: true - }); - }); +ContactManager.addInitializer(function(data) { + var contacts = new ContactManager.Collections.Contacts(data.contacts), + router = new ContactManager.Router(); - router.on('route:showContacts', function() { - var contactsView = new ContactManager.Views.Contacts({ - collection: contacts - }); + router.on('route:home', function() { + router.navigate('contacts', { + trigger: true, + replace: true + }); + }); - mainRegion.show(contactsView); + router.on('route:showContacts', function() { + var contactsView = new ContactManager.Views.Contacts({ + collection: contacts }); - router.on('route:newContact', function() { - var newContactForm = new ContactManager.Views.ContactForm({ - model: new ContactManager.Models.Contact() - }); + ContactManager.mainRegion.show(contactsView); + }); - newContactForm.on('form:submitted', function(attrs) { - attrs.id = contacts.isEmpty() ? 1 : (_.max(contacts.pluck('id')) + 1); - contacts.add(attrs); - router.navigate('contacts', true); - }); + router.on('route:newContact', function() { + var newContactForm = new ContactManager.Views.ContactForm({ + model: new ContactManager.Models.Contact() + }); - mainRegion.show(newContactForm); + newContactForm.on('form:submitted', function(attrs) { + attrs.id = contacts.isEmpty() ? 1 : (_.max(contacts.pluck('id')) + 1); + contacts.add(attrs); + router.navigate('contacts', true); }); - router.on('route:editContact', function(id) { - var contact = contacts.get(id), - editContactForm; + ContactManager.mainRegion.show(newContactForm); + }); - if (contact) { - editContactForm = new ContactManager.Views.ContactForm({ - model: contact - }); + router.on('route:editContact', function(id) { + var contact = contacts.get(id), + editContactForm; - editContactForm.on('form:submitted', function(attrs) { - contact.set(attrs); - router.navigate('contacts', true); - }); + if (contact) { + editContactForm = new ContactManager.Views.ContactForm({ + model: contact + }); - mainRegion.show(editContactForm); - } else { + editContactForm.on('form:submitted', function(attrs) { + contact.set(attrs); router.navigate('contacts', true); - } - }); + }); + + ContactManager.mainRegion.show(editContactForm); + } else { + router.navigate('contacts', true); + } + }); +}); +ContactManager.on('initialize:after', function(options){ + if (Backbone.history){ Backbone.history.start(); } -}; +});