I am using CartoCSS to style features in a run-time CartoDB layer (as opposed to a pre-made visualization) but it gets messy doing this within JavaScript:
sublayers: [{
cartocss: '#initvisitedfws {bottom/marker-width: 24; bottom/marker-ignore-placement:false; bottom/marker-fill: gold; bottom/marker-allow-overlap:true; top/marker-width: 14; top/marker-fill: #111; top/marker-file: url(clock.svg); top/marker-allow-overlap:true;',
interactivity: 'cartodb_id, parent',
sql: "SELECT * FROM subprojects"
}]
I could keep it somewhat neat by using multi-line strings but that becomes unsightly and tedious in its own way, so I would prefer to edit the CartoCSS in its own native .mss file (especially in tandem with this Sublime Text 2 .mss syntax/autocomplete package).
The ghetto-ish way I am approaching this is with an XMLHttpRequest:
function getMss() {
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open('GET', "somepath/somefile.mss", false);
xmlhttp.send();
var mapStyle = xmlhttp.responseText;
return mapStyle;
}
...and then calling it from the CartoDB sublayer parameters:
var cartodbParams = {
user_name: 'user_name',
type: 'cartodb',
interaction: true,
sublayers: [{
cartocss: getMss(),
interactivity: 'cartodb_id, parent',
sql: "SELECT * FROM subprojects"
}]
}
This works fine but leads to another HTTP request, and I'm also not optimistic about its maintainability once I have dozens or hundreds of rules and multiple map layers.
So, what is a better solution for using CartoCSS within JavaScript?