chCmsExposeRoutingPluginWhat is this ?
chCmsExposeRoutingPlugin is a
symfony 1.(3|4) plugin used to expose routing definition
to the client side.
if you use symfony2, have a look to Bazinga/ExposeRoutingBundle
This file is the client side api reference.
Basic usage
in your script just access a server side route this way:
Routing.get('your route name');
You can generate a url this way:
Routing.generate('your_route_name', {'foo': 'bar', 'baz': 1});
| |
| web/js/routing.js |
define Routing class
|
$.ExposeRouting = $.ExposeRouting || {};
(function(Routing, $, undefined) {
$.extend(Routing, (function() {
var _routes = {},
_defaults = {},
rquery = /\?/,
rabsurl = /^\//,
rescregexp = /[-[\]{}()*+?.,\\^$|#\s]/g,
rdblslash = /\/\
self = Routing,
ServerMethods = {};
|
prepare a regexp part with several caracters/parts
having to be escaped.
regexify('a'); // returns 'a'
regexify(['a', '.']); // returns 'a|.'
regexify(['a', '.'], '$'); // returns 'a|.|$'
param: Array | string separators a list of separators. param: String unescaped a meta character to use in regexp. return: String the regexp part, ready to use.
|
function regexify(separators, unescaped) {
var _i, _separators = [];
if (!$.isArray(separators)) {
separators = [separators];
}
for (_i in separators) {
_separators[_i] = separators[_i].replace(rescregexp, '\\$&');
}
if (unescaped) { _separators.push(unescaped); }
if (_separators.length > 1) {return _separators.join('|')}
else if (_separators.length) {return _separators[0];}
return '';
};
|
replace params in given url.
WARNING: used params are removed.
param: String url the raw url. param: Object params the params to replace. return: String the treated url. api: private
|
function replace_params(url, params) {
var _i,
_url = url,
_separators = self.segmentSeparators,
_prefixes = regexify(self.variablePrefix),
_suffixes = regexify(self.variableSuffix),
_prefix = '(' + regexify(_separators, '^') + ')' + _prefixes,
_suffix = _suffixes + '(' + regexify(_separators, '$') + ')';
for (_i in params) {
var _r = new RegExp(_prefix + _i + _suffix, '');
if (_r.test(_url)) {
_url = _url.replace(_r, '$1' + params[_i] + '$2');
delete(params[_i]);
}
}
return _url;
};
|
get, put, post, del:
send server request at route url from route id and params.
param: String route_id the id of route to generate url for. param: Object [params] the parameters to append to the route. param: Function [callback=success(data, textStatus, jqXHR)] function for success. param: String [type] the return data type. api: public
|
$(["get", "post"]).each(function(i, method) {
ServerMethods[method] = function(route_id, data, callback, type) {
if ($.isFunction(data)) {
type = type || callback;
callback = data;
data = undefined;
}
return $[method](self.generate(route_id, data, false), data, callback, type);
}
});
$(["put", "delete"]).each(function(i, method) {
ServerMethods[method.substr(0,3)] = function(route_id, data, callback, type) {
var _data = {};
_data[self.methodParameterName] = method;
if ($.isFunction(_data)) {
type = type || callback;
callback = _data;
data = undefined;
}
return $.post(self.generate(route_id, _data, false), $.extend(_data, data || {}), callback, type);
}
});
return $.extend(ServerMethods, {
|
default routing parameters for every routes.
|
defaults: {},
|
route parameter suffix.
|
variableSuffix: '',
|
route parameter prefix.
|
variablePrefix: ':',
|
route url separator list.
type: String | Array api: public
|
segmentSeparators: ['/', '.'],
|
route url prefix to use.
|
prefix: '',
|
the cross site request forgery defaults
|
csrf: {},
|
name for the parameter used to specify non HTTP 1.0 method name
|
methodParameterName: 'sf_method',
|
connect a route.
|
connect: function(id, pattern, defaults) {
_routes[id] = pattern;
_defaults[id] = defaults || {};
return self;
},
|
retrieve a route by it's id.
|
getRoute: function(route_id) {
return _routes[route_id] || undefined;
},
|
determines wether a route is registered or not.
|
has: function(route_id) {
return (_routes[route_id] ? true : false);
},
|
clears all routes
return: Object Routing. api: public
|
flush: function() {
_routes = {};
_defaults = {};
return self;
},
|
generate a route url from route id and params.
Warnig: given params are modified !
used parameters are
param: String route_id the id of route to generate url for. param: Object params the parameters to append to the route. param: Boolean withExtraParameters include extra parameters to url ? (default = true). return: String generated url. api: public
|
generate: function(route_id, params, withExtraParameters) {
var _route = self.getRoute(route_id),
_queryString,
_url = _route;
if (!_url) {
throw 'No matching route for ' + route_id;
}
_url = replace_params(_url, params);
_url = replace_params(_url, $.extend({},
self.defaults || {},
_defaults[route_id] || {}));
if (withExtraParameters || undefined === withExtraParameters) {
_queryString = $.param(params || {});
if (_queryString.length) {
_url += (rquery.test(_url) ? '&' : '?') + _queryString;
}
}
_url = (rabsurl.test(_url) ? '' : '/') + _url;
_url = self.prefix + _url;
_url = (rabsurl.test(_url) ? '' : '/') + _url;
return _url.replace(rdblslash, '/');
}
});
})());
})($.ExposeRouting, jQuery);
|