Skip to content

Commit

Permalink
Use Marionette.Application
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytroyarmak committed Feb 5, 2014
1 parent f6e432d commit 4ab93c8
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions app/js/app.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
});

0 comments on commit 4ab93c8

Please sign in to comment.