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/donation/donation','modules/settings/settings', 'model/note/noteCollection', 'model/category/categoryCollection','model/categoryItem/categoryItemCollection', 'model/item/itemCollection','model/item/ingredientiCollection','model/categoryItem/foundItemCollection', 'classes/utility', 'jqm', 'jQueryPlugins'], function($, _, Backbone, HomeView, FaqView, IndexView, CategoryView, ItemView, AboutView, SearchView, FoundView, MoreView, NoteView, SyncView, DonationView, SettingsView, NoteCollection, CategoryCollection, CategoryItemCollection, ItemCollection, IngredientiCollection, FoundItemCollection, Utility) { '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){ var categories = new CategoryCollection(); // will render home view and navigate to homeView var indexView=new IndexView({ collection: categories}); //indexView.render(); 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); }, 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){ //console.log("Search by Titolo: " + titolo + ", Categoria: " + categoria + ", Difficoltà: " + difficolta); var foundedItems = new FoundItemCollection({ categoryId: categoria, difficolta: difficolta, titolo: titolo, nResult:nresult }); 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) { Utility.changePage(view); if(!this.init){ }else{ this.init = false; } } }); return Router; });