override API location with data-prefix="..."

This commit is contained in:
Martin Zimmermann 2013-11-02 18:26:33 +01:00
parent 0d07515c18
commit 8da9d0b13b
2 changed files with 48 additions and 8 deletions

View File

@ -122,6 +122,15 @@ You can configure the client (the JS part) via `data-` attributes:
Make sure to escape the attribute value. Make sure to escape the attribute value.
* data-prefix
Isso usually detects the REST API automatically, but when you serve the JS
script on a different location, this may fail. Use `data-prefix` to
override the API location:
```html
<script data-prefix="/isso" src="/path/to/embed.min.js"></script>
```
### Webserver configuration ### Webserver configuration

View File

@ -22,20 +22,51 @@ define(["q"], function(Q) {
"/count": [200] "/count": [200]
}; };
// guess Isso API location /*
* Detect Isso API endpoint. There are typically two use cases:
*
* 1. use minified, single-file JavaScript. The browser interprets
* scripts sequentially, thus we can safely use the last script
* tag. Then, we chop off some characters -- /js/embed.min.s --
* and we're done.
*
* If the script is not served by Isso directly, a custom data
* attribute can be used to override the default detection
* mechanism:
*
* .. code-block:: html
*
* <script data-prefix="/path" src="/.../embed.min.js"></script>
*
* 2. use require.js (during development). When using require.js, we
* assume that the path to the scripts ends with `/js/`.
*
*/
var script, uri;
var port = window.location.port ? ":" + window.location.port : "",
host = window.location.protocol + "//" + window.location.hostname + port;
var js = document.getElementsByTagName("script"); var js = document.getElementsByTagName("script");
for (var i = 0; i < js.length; i++) { for (var i = 0; i < js.length; i++) {
if (js[i].src.match("/js/components/requirejs/require\\.js$")) { if (js[i].src.match("require\\.js$") && js[i].dataset.main.match("/js/embed$")) {
endpoint = js[i].src.substring(0, js[i].src.length - 35); uri = js[i].dataset.main;
break; endpoint = uri.substr(0, uri.length - "/js/main".length);
} else if (js[i].src.match("/js/(embed|count)\\.(min|dev)\\.js$")) {
endpoint = js[i].src.substring(0, js[i].src.length - 16);
break;
} }
} }
if (! endpoint) { if (! endpoint) {
throw "no Isso API location found"; script = js[js.length - 1];
if (script.dataset.prefix) {
endpoint = script.dataset.prefix;
if (endpoint[endpoint.length - 1] === "/") {
endpoint = endpoint.substring(0, endpoint.length - 1);
}
} else {
uri = script.src.substring(host.length);
endpoint = uri.substring(0, uri.length - "/js/embed.min.js".length);
}
} }
var curl = function(method, url, data) { var curl = function(method, url, data) {