Files
IlLievitario-App/js/router.js
T

220 lines
11 KiB
JavaScript

define(['jquery', 'underscore', 'backbone',
'modules/home/home', 'modules/faq/faq', 'modules/index/index', 'modules/category/category', 'modules/item/item',
'modules/about/about', 'modules/search/search', 'modules/search/found', 'modules/more/more', 'modules/note/note',
'modules/sync/sync', 'modules/converter/converter', 'modules/donation/donation', 'modules/settings/settings',
'modules/debug/debug',
'model/note/noteCollection', 'model/category/categoryCollection', 'model/categoryItem/categoryItemCollection',
'model/item/itemCollection', 'model/item/ingredientiCollection', 'model/categoryItem/foundItemCollection',
'classes/cache', 'libs/fastclick',
'jqm', 'jQueryPlugins'],
function ($, _, Backbone,
HomeView, FaqView, IndexView, CategoryView, ItemView,
AboutView, SearchView, FoundView, MoreView, NoteView,
SyncView, ConverterView, DonationView, SettingsView,
DebugView,
NoteCollection, CategoryCollection, CategoryItemCollection,
ItemCollection, IngredientiCollection, FoundItemCollection,
Cache, FastClick) {
'use strict';
var Router = Backbone.Router.extend({
initialize: function (options) {
var self = this;
$(document).on("pagecontainershow", function (event, ui) {
try
{
ga('send', 'pageview',
{
'page': "/" + this.location.hash,
'title': document.title
});
}
catch (err)
{
}
if (self.activeView.onShowed !== null)
self.activeView.onShowed();
// if (self.fastClickInstance !== null)
// self.fastClickInstance.destroy();
// self.fastClickInstance = FastClick.attach(document.body);
});
},
//define routes and mapping route to the function
routes: {
'': 'showHome', //home view
'homeView': 'showHome', //home view as well
'faqView': 'showFaq', //home view as well
'indexView': 'showIndex', //home view as well
'category/:categoryName/:categoryId': 'showCategory',
'ricetta/:itemId': 'showItem',
'aboutView': 'showAbout',
'syncView(/)*founded': 'showSync',
'noteView': 'showNote',
'searchView': 'showSearch',
'debugView': 'showDebug',
'settingsView': 'showSettings',
'donationView': 'showDonation',
'search/:nresult/:categoria/:difficolta/*titolo': 'searchFn',
'converterView': 'showConverter',
'moreView': 'showMore',
'*actions': 'defaultAction' //default action
},
defaultAction: function (actions) {
this.showHome(actions);
},
showHome: function (actions) {
// will render home view and navigate to homeView
var homeView = new HomeView();
homeView.render();
this.changePage(homeView);
},
showSettings: function (actions) {
var settingsView = new SettingsView();
settingsView.render();
this.changePage(settingsView);
},
showDonation: function (actions) {
var donationView = new DonationView();
donationView.render();
this.changePage(donationView);
},
showAbout: function (actions) {
// will render home view and navigate to homeView
var aboutView = new AboutView();
aboutView.render();
this.changePage(aboutView);
},
showSync: function (actions) {
var option = {};
if (actions)
{
option = {tokenFound: 1};
}
// will render home view and navigate to homeView
var syncView = new SyncView(option);
syncView.render();
this.changePage(syncView);
},
showNote: function (actions) {
var notes = new NoteCollection();
// will render home view and navigate to homeView
var noteView = new NoteView({collection: notes});
noteView.render();
this.changePage(noteView);
},
showFaq: function (actions) {
// will render home view and navigate to homeView
var faqView = new FaqView();
faqView.render();
this.changePage(faqView);
},
showIndex: function (actions) {
Cache.checkCacheFromServer();
//var categories = new CategoryCollection();
// will render home view and navigate to homeView
var indexView = new IndexView();//{collection: categories});
indexView.render();
this.changePage(indexView);
//indexView.bind('renderCompleted:Categories', this.changePage, this);
},
showCategory: function (categoryName, categoryId) {
var categories = new CategoryItemCollection({categoryId: categoryId});
// will render home view and navigate to homeView
var categoryView = new CategoryView({categoryName: categoryName, categoryId: categoryId, collection: categories});
categoryView.render();
this.changePage(categoryView);
if(!categories.isCached() || categoryView.created){
$.mobile.loading('show', {
text: 'Caricamento categoria ' + categoryName + '...',
textVisible: true,
//theme: 'z',
html: ""
});
}
//categoryView.bind('renderCompleted:Category',this.changePage,this);
},
showItem: function (itemId) {
var item = new ItemCollection({ricettaId: itemId});
var ingr = new IngredientiCollection({ricettaId: itemId});
// will render home view and navigate to homeView
var itemView = new ItemView({
collRicetta: item,
collIngredienti: ingr
});
//indexView.render();
itemView.bind('renderCompleted:Item', this.changePage, this);
},
showConverter: function () {
var converterView = new ConverterView();
converterView.render();
this.changePage(converterView);
},
showMore: function () {
var moreView = new MoreView();
moreView.render();
this.changePage(moreView);
},
showDebug: function () {
var debugView = new DebugView();
debugView.render();
this.changePage(debugView);
},
showSearch: function () {
var categories = new CategoryCollection();
// will render home view and navigate to homeView
var searchView = new SearchView({collection: categories});
//indexView.render();
searchView.bind('renderCompleted:Search', this.changePage, this);
},
searchFn: function (nresult, categoria, difficolta, titolo) {
var foundedItems = new FoundItemCollection({categoryId: categoria, difficolta: difficolta, titolo: titolo, nResult: nresult, indexItems: 0});
var foundView = new FoundView({collection: foundedItems});
foundView.render();
this.changePage(foundView);
$.mobile.loading('show', {
text: 'Caricamento ricerca ricette...',
textVisible: true,
//theme: 'z',
html: ""
});
},
fastClickInstance: null,
activeView: null,
//1. changePage will insert view into DOM and then call changePage to enhance and transition
//2. for the first page, jQuery mobile will present and enhance automatically
//3. for the other page, we will call $.mobile.changePage() to enhance page and make transition
//4. argument 'view' is passed from event trigger
changePage: function (view) {
var self = this;
//append to dom
if (view.created === true)
{
$('body').append(view.$el);
}
if (this.activeView !== null &&
this.activeView.onClosing !== null)
this.activeView.onClosing();
$.mobile.pageContainer.pagecontainer("change", $(view.el), {changeHash: false});
var lastView = this.activeView;
this.activeView = view;
if (lastView !== null &&
lastView.onClosed !== null)
lastView.onClosed();
if (this.activeView.onShowing !== null)
this.activeView.onShowing();
}
});
return Router;
});