Eine Route mit Flags einschränken
Flags beginnen mit @ und stehen nach der URL. Eine Route passt nur, wenn die Anfrage alle ihre Flags erfüllt; andernfalls probiert Total.js die nächste Route für dieselbe URL und antwortet mit 404, wenn keine passt. Nützliche Flags:
@jsonund@xml: Der Body muss JSON oder XML sein@xhr: Die Anfrage muss eine AJAX-Anfrage sein (Headerx-requested-with)@upload: Der Body mussmultipart/form-datasein@debugund@release: Die Route existiert nur in diesem Modus@mobileund@robot: Der Client ist ein mobiler Browser oder ein Crawler
exports.install = function() {
ROUTE('POST /data/ @json', json_only);
ROUTE('POST /data/ @xml', xml_only);
// Careful: a route with flags has a lower priority than the same URL without flags
ROUTE('POST /mixed/ @json', json_only);
ROUTE('POST /mixed/', anything);
};
function json_only($) {
$.json({ handler: 'json' });
}
function xml_only($) {
$.json({ handler: 'xml' });
}
function anything($) {
$.json({ handler: 'anything' });
}Mische nie eine Route mit Flags und eine ohne Flags auf derselben URL: Die ohne Flags wird zuerst geprüft und übernimmt jede Anfrage, die Route mit Flags wird also nie erreicht.