-
-
Save wallymathieu/1471042 to your computer and use it in GitHub Desktop.
Routing in KO with Backbone.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Router = Backbone.Router.extend({ | |
routes : { | |
"" : "home" | |
,"/" : "home" | |
,"!" : "home" | |
,"!/" : "home" | |
,"!/:username" : "show_user" | |
,":username" : "show_user" | |
,":username/" : "show_user" | |
,"!/:username/" : "show_user" | |
,"!/:username/:course_slug": "show_course" | |
,"/:username/:course_slug": "show_course" | |
,":username/:course_slug": "show_course" | |
,":username/:course_slug": "show_course" | |
,"!/:username/:course_slug/settings": "show_course_settings" | |
,"/:username/:course_slug/settings": "show_course_settings" | |
,":username/:course_slug/settings": "show_course_settings" | |
,":username/:course_slug/settings": "show_course_settings" | |
,"!/:username/:course_slug/:unit_id": "show_unit" | |
} | |
,home : function(){ | |
var cu = _cu | |
if (cu.username === "guest"){ | |
App.VMs.global.mainTemplate('show_home') | |
App.VMs.show_user_bar(false) | |
} else { | |
Backbone.history.navigate("#!/" + cu.username, true) | |
} | |
} | |
,show_user : function(username){ | |
App.VMs.show_user_bar(true) | |
$('#loading').fadeIn(200) | |
if (App.VMs.current_user() == undefined){ | |
App.VMs.current_user(new User(cu)); | |
} | |
if (App.VMs.current_user().username() === username){ | |
App.VMs.user(new User(cu)) | |
App.VMs.global.mainTemplate('show_user') | |
$(document).attr('title', username + "'s Curriculum -- Common Curriculum") | |
reset_new_course() | |
$("body").scrollTop(0) | |
$("#loading").fadeOut(200) | |
} else { | |
App.fetchers.user(username, function(user){ | |
App.VMs.user(user) | |
App.VMs.new_course = ko.observable(new new_course()) | |
App.VMs.global.mainTemplate('show_user') | |
$(document).attr('title', username + "'s Curriculum -- Common Curriculum") | |
$("body").scrollTop(0) | |
$("#loading").fadeOut(200) | |
}) | |
} | |
} | |
,show_course : function(username, course_slug){ | |
$("#loading").fadeIn(200) | |
if (App.VMs.current_user() == undefined){ | |
var user = new User(cu) | |
App.VMs.current_user(user) | |
} | |
App.fetchers.course(username, course_slug, function(course){ | |
App.VMs.course(course) | |
var unit_data = { | |
username : course.username() | |
,id : course.root_unit_id() | |
,course_slug : course.slug() | |
} | |
load_unit(unit_data) | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.fetchers = { | |
course : function(username, course_slug, cb){ | |
amplify.request( | |
'course#read' | |
,{ username: username, course_slug : course_slug} | |
,function(data){ | |
if( typeof cb !== 'undefined') {cb(new Course(data))} | |
} | |
) | |
} | |
,user : function(username, cb){ | |
amplify.request( | |
'user#read' | |
,{username : username} | |
,function(data){ | |
if( typeof cb !== 'undefined') {cb(new User(data))} | |
} | |
) | |
} | |
,unit : function(data, cb){ | |
var timestamp = new Date().getTime() | |
amplify.request( | |
'unit#read' | |
,{ | |
username : data.username | |
,course_slug : data.course_slug | |
,id : data.id | |
} | |
,function(data){ | |
if( typeof cb !== 'undefined') {cb(new Unit(data))} | |
} | |
) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
amplify.request.define('user#read', "ajax", { | |
url : "api/v1/users/{username}" | |
,dataType: "json" | |
,type: "GET" | |
}) | |
amplify.request.define('user#update', "ajax", { | |
url : "api/v1/users/{username}" | |
,dataType: "json" | |
,type: "PUT" | |
}) | |
//UNIT AJAX METHODS | |
amplify.request.define('unit#create', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/" | |
,dataType: "json" | |
,type: "POST" | |
}) | |
amplify.request.define('unit#read', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{id}" | |
,dataType: "json" | |
,type: "GET" | |
}) | |
amplify.request.define('unit#update', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{id}" | |
,dataType: "json" | |
,type: "PUT" | |
}) | |
amplify.request.define('unit#delete', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{id}" | |
,dataType: "json" | |
,type: "DELETE" | |
}) | |
//CONTENT AJAX METHODS | |
amplify.request.define('content#create', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{unit_id}/contents" | |
,dataType: "json" | |
,type: "POST" | |
}) | |
amplify.request.define('content#read', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{unit_id}/contents/{id}" | |
,dataType: "json" | |
,type: "GET" | |
}) | |
amplify.request.define('content#update', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{unit_id}/contents/{id}" | |
,dataType: "json" | |
,type: "PUT" | |
}) | |
amplify.request.define('content#delete', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/units/{unit_id}/contents/{id}" | |
,dataType: "json" | |
,type: "DELETE" | |
}) | |
//COURSE AJAX METHODS | |
amplify.request.define('course#create', "ajax", { | |
url : "api/v1/courses/" | |
,dataType: "json" | |
,type: "POST" | |
}) | |
amplify.request.define('course#read', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}" | |
,dataType: "json" | |
,type: "GET" | |
}) | |
amplify.request.define('course#update', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}" | |
,dataType: "json" | |
,type: "PUT" | |
}) | |
amplify.request.define('course#delete', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}" | |
,dataType: "json" | |
,type: "DELETE" | |
}) | |
//DOCUMENT AJAX METHODS | |
amplify.request.define('document#update', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/contents/{content_id}/documents/{id}" | |
,dataType: "json" | |
,type: "PUT" | |
}) | |
amplify.request.define('document#delete', "ajax", { | |
url : "api/v1/users/{username}/courses/{course_slug}/contents/{content_id}/documents/{id}" | |
,dataType: "json" | |
,type: "DELETE" | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment