Files
IlLievitario-App/js/router.js
T

204 lines
9.9 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',
'model/note/noteCollection', 'model/category/categoryCollection', 'model/categoryItem/categoryItemCollection',
'model/item/itemCollection', 'model/item/ingredientiCollection', 'model/categoryItem/foundItemCollection',
'libs/fastclick', 'classes/cache', 'jqm', 'jQueryPlugins'],
function ($, _, Backbone,
HomeView, FaqView, IndexView, CategoryView, ItemView,
AboutView, SearchView, FoundView, MoreView, NoteView,
SyncView, ConverterView, DonationView, SettingsView,
NoteCollection, CategoryCollection, CategoryItemCollection,
ItemCollection, IngredientiCollection, FoundItemCollection,
FastClick, Cache) {
'use strict';
var Router = Backbone.Router.extend({
//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',
'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, collection: categories});
categoryView.render();
this.changePage(categoryView);
$.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);
},
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: ""
});
},
init: true,
//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) {
//append to dom
$('body').append(view.$el);
$.mobile.pageContainer.pagecontainer("change", $(view.el), {changeHash: false});
FastClick.attach(document.body);
$(":jqmData(role='page'):last").on("pageshow", function (event) {
if (view.onShowed !== null)
view.onShowed();
});
$('input[type=radio].star').rating({
callback: function (value, link) {
var tip = $('#hover-test');
if (value === undefined)
{
$('#hover-test').html("Nessuna" || 'value: ');
}
else
$('#hover-test').html(link.title || 'value: ' + value);
},
focus: function (value, link) {
var tip = $('#hover-test');
tip[0].data = tip[0].data || tip.html();
tip.html(link.title || 'value: ' + value);
}
// ,
// blur: function(value, link){
// var tip = $('#hover-test');
// $('#hover-test').html(tip[0].data || '');
// }
});
if (!this.init) {
} else {
this.init = false;
}
}
});
return Router;
});