JavaScript Backbone Hash App #1 – the router

// this helps the app remain configurable, moveable, thus this ‘state’ of the app is extracted
var mostPopularAnswersHashText = “MostPopular”

//Router
var AppRouter = Backbone.Router.extend({

routes: {
”: ‘home’,
‘index.php’: ‘home’,
‘Search::searchQuery’ : ‘search’, // Matches http://support.htm/#Search:query
‘Search/:searchQuery’ : ‘search’, // Matches http://support.htm/#Search/query
‘:globalState’ : ‘globalStates’, // Matches http://example.com/#six
‘a_id/:answerIdInput’: ‘answerDetails’, // Matches http://example.com/#a_id/34
‘*actions’: ‘defaultAction’ // Matches http://example.com/#anything-here
},

defaultAction : function(){
this.navigate(mostPopularAnswersHashText, {trigger: true});
},
home : function(){
takeDown();
tracer(‘index’);
appProcessing = false;
this.navigate(mostPopularAnswersHashText, {trigger: true});
},

search : function(searchQuery){
searchResults = ‘Search/’ + searchQuery;

var portal = new AnswerModel({
//passing these values to the Model, which simply hits an AJAX server service
methodNeeded: ‘searchAjax’, answerId: prodID + ‘/’ + searchQuery, callback: ‘searchResults’
});
portal.fetch({
success: function(response,xhr) {
// Pass returned populated model to a View & render
var output = new AllAnswersView({ model : ‘AnswerModel’ });
output.render(response);
},
error: function (errorResponse) {
}
});
},

globalStates : function(globalState){

switch(globalState){

case mostPopularAnswersHashText:
var portal = new AnswerModel({
methodNeeded: ‘displayAnswersAjax’, answerId: prodID, callback: ‘callResponse’
});
portal.fetch({
success: function(response,xhr) {
var output = new MostPopularView({ model : ‘AnswerModel’ });
output.render(response);
},
error: function (errorResponse) {
}
});
break;

case allAnswersHashText:
case allTravelToolsHashText:
case allTravelEssentialsHashText:
case allTravelAdvisoriesHashText:

if(globalState == allAnswersHashText){ searchQuery = ”; }
if(globalState == allTravelToolsHashText){ searchQuery = ‘/c_travel-tools’; }
if(globalState == allTravelEssentialsHashText){ searchQuery = ‘/c_travel-essentials’; }
if(globalState == allTravelAdvisoriesHashText){ searchQuery = ‘/c_travel-advisories’; }

var portal = new AnswerModel({methodNeeded: ‘searchAjax’, answerId: prodID + ‘/’ + searchQuery, callback: ‘searchResults’});
portal.fetch({
success: function(response,xhr) {
var output = new AllAnswersView({ model : ‘AnswerModel’ });
output.render(response);
},
error: function (errorResponse) {
}
});
break;
}
},

answerDetails : function(answerIdInput){
// Initiate an Answer Model, pass it the desired ID
var portal = new AnswerModel({methodNeeded: ‘getFullAnswersAjax’, answerId: answerIdInput, callback: ‘answerContentResponse’});
portal.fetch({
success: function(response,xhr) {
// Pass returned populated model to a View, render
var article = new AnswerIdView({ model : ‘AnswerModel’ });
article.render(response);
},
error: function (errorResponse) {
tracer(errorResponse);
}
});
}
});

Leave a Reply

You must be logged in to post a comment.