javascript - Backbone js router - better way to orginize views -
here way orginize 4 views , 4 links backbone router
var linksrouter = backbone.router.extend({ routes:{ "classes" : "classes", "profile" : "profile", "reportcard" : "reportcard", "newclasses" : "newclasses", }, classes : function(){ $(".viewable").hide(); $(".clickable").removeclass("active"); $("#classes-view").show(); $("#classes-link").addclass("active"); }, profile : function(){ $(".viewable").hide(); $(".clickable").removeclass("active"); $("#profile-view").show(); $("#profile-link").addclass("active"); }, reportcard : function(){ $(".viewable").hide(); $(".clickable").removeclass("active"); $("#reportcard-view").show(); $("#reportcard-link").addclass("active"); }, newclasses : function(){ $(".viewable").hide(); $(".clickable").removeclass("active"); $("#newclasses-view").show(); $("#newclasseslink").addclass("active"); } }); var links = new linksrouter(); backbone.history.start();
it looks tedious , not efficient. there better way backbone js?
i'm sure there many ways.. how about:
var linksrouter = backbone.router.extend({ routes:{ ":subview" : "viewset", // or "/:subview" ? }, viewset : function(subview){ $(".viewable").hide(); $(".clickable").removeclass("active"); $("#"+subview+"-view").show(); $("#"+subview+"-link").addclass("active"); } }); var links = new linksrouter(); backbone.history.start();
Comments
Post a Comment