I started to refactor some "legacy" code, where a lot of js files are duplicated due to one or two specific parameters for a script. My idea is to merge all files into one, remove code duplication and distinguish those parameters based on routing. I would like to use Spine.js
Example code:
$(function () {
var SApp = Spine.Controller.create({
init:function () {
this.routes({
"product/direct":function () {
pId = $('#product-id').val();
magicFunction(pId);
},
"product/suggested":function () {
products = $('#parent-products').find('li');
$.each(products, function (index, product) {
pId = $(product).attr('data-id');
magicFunction(pId);
});
}
})
}
});
Spine.Route.setup();
new SApp();
});
every routes is a separate page
<a href="/page1.html#product/direct">1</a>
<a href="/page2.html#product/suggested">2</a>
It works fine when I first visit a page /page2.html then paste #product/direct part and click enter, but the problem occurs when user is redirected to a page with #... part and Spinejs is not working until first event invocation.
Is is possible to tell Spine that current url should be used now, so spine will not wait for any event? Something like autostart or onload trigger?
----------------------------- edit ------------------------------
I found dirty solution:
Unfortunately it costs one more page request in case of redirection.
if (app.config.startAction) {
location = window.location.href;
if (location.indexOf('#') > 0) { //if user reload a page
location = location.split('#')[0];
window.location.href = location;
}
window.location.href = location + '#' + app.config.startAction;
}