/** * JS Url Maps * Maps Django url to callable JS objects, contauns a method for replaceing placegolder args * with real args * Auth: Chris Reeves, POKE, 2010-03-29 */ /** USAGE ===== BASIC ---------------- Each url is degined as a method, for example TheFeed.url_maps.example() would return a url to www.example.com/example. ADVANCED ---------------- Some urls may require placeholder arguments for django to perform the reverse url method, so we might define this like: {[%] url example1 arg1,arg2 [%]} This script has a method fo swapping placeholder args with the desired correct args, for example: TheFeed.url_maps.example2 = function(arg1, arg2) { dict = { arg1: { placeholder: 'arg1', arg: arg1 }, arg2: { placeholfer: 'arg2', arg: arg2 } return this.replace_holders('{[%] url exaple2 arg1,arg2 [%]}', dict); } } this will return back a url with the corret arguments, you can then call this: TheFeed.url_maps.example2(21312, 4823); which will return something like: www.example2.com/example2/21312/4823/ Which you can then use in an ajax call There is probably a more eligant way of achieving this, if you think of a way let me know :) Note: django template ({%) tags in these comments are noted as {[%] url [%]} to prevent django from interpreting these as real requests. **/ // URL maps namespace TheFeed.url_maps = {} // Reaceplace placeholders method // Takes url as 1st arg and dictionary object as second arg TheFeed.url_maps.replace_holders = function(url, dict, type) { if(!type) { type = 'url' } for(arg in dict) { var obj = dict[arg]; if(type == 'url') { url = url.replace('/' + obj.placeholder + '/', '/' + obj.arg + '/'); } if(type == 'filename') { url = url.replace('/' + obj.placeholder, '/' + obj.arg); } } return url; } // Static URL TheFeed.url_maps.static_url = function() { return 'http://d21uhduswkkbj9.cloudfront.net/cdn/1367228652/static/'; } // This or that microapp TheFeed.url_maps.thisorthat_microapp = function(view, post_id) { return '/microapp/thisorthat/overlay/?post_id=' + post_id + '&overlay=' + view } // Conversationator microapp TheFeed.url_maps.conversationator_microapp = function(view, post_id) { return '/microapp/conversationator/overlay/?post_id=' + post_id + '&overlay=' + view } // Rather microapp TheFeed.url_maps.rather_microapp = function(view, post_id) { return '/microapp/rather/overlay/?post_id=' + post_id + '&overlay=' + view } // Comment pagination TheFeed.url_maps.comment_pagination = function(post_id) { dict = { arg1: { placeholder: '999', arg: post_id } } return this.replace_holders('/comments/load/999/', dict); } // Bafta Get Film TheFeed.url_maps.bafta_film_json = function(slug) { dict = { arg1: { placeholder: 'film-slug', arg: slug } } return this.replace_holders('/microapp/bafta/get_film/film-slug.json', dict, 'filename'); } // Bafta Submit Guess TheFeed.url_maps.bafta_submit_guess = function() { return '/microapp/bafta/submit_guess/'; } // Bafta get session guesses TheFeed.url_maps.bafta_session_guesses = function() { return '/microapp/bafta/session_gueses/'; } TheFeed.url_maps.bafta_verfied_guesses = function(slug) { dict = { arg1: { placeholder: 'film-slug', arg: slug } } return this.replace_holders('/microapp/bafta/verified_gueses/film-slug/', dict); } TheFeed.url_maps.bafta_verfied_guess = function(pk) { dict = { arg1: { placeholder: '1', arg: pk } } return this.replace_holders('/microapp/bafta/verified_guess/1/', dict); } TheFeed.url_maps.bafta_scene_overlay = function(pk) { dict = { arg1: { placeholder: '1', arg: pk } } return this.replace_holders('/microapp/bafta/scene_overlay/1/', dict); } // LuckyLikes urls TheFeed.url_maps.ll_fb_login_callback = function() { return '/microapp/lucky-likes/fb_login_callback/'; } TheFeed.url_maps.ll_fb_logout_callback = function() { return '/microapp/lucky-likes/fb_logout_callback/'; } TheFeed.url_maps.ll_index = function() { return '/microapp/lucky-likes/'; } TheFeed.url_maps.ll_play = function() { return '/microapp/lucky-likes/play/'; } TheFeed.url_maps.ll_lost = function() { return '/microapp/lucky-likes/lost/'; } TheFeed.url_maps.ll_won = function(param) { return '/microapp/lucky-likes/won/' + param; } TheFeed.url_maps.ll_likesite = function(param) { return '/microapp/lucky-likes/like/' + param; } TheFeed.url_maps.ll_prize = function(param) { return '/microapp/lucky-likes/prize/' + param; } // Birthday portrait submission TheFeed.url_maps.birthday_submit = function() { return '/birthday-tab/submit/'; }