admin管理员组文章数量:1289858
I recently started using knockout.js and sammy.js to modernize my app. However I got stuck with some problems.
I have some valid links on the page - users should actually navigate to that location, instead of imitating navigation behaviors using sammy.js
. I want only hash-based links to be routed by sammy.js, but it also intercepts links that does not contain any hashes.
for example, it intercepts <a href="/logout">logout</a>
.
the js part that does routing is :
Sammy(function () {
this.get('#/', function () {
...
});
this.get('#:id', function () {
...
});
this.get('', function () { this.app.runRoute('get', '#/') });
}).run();
I think this.get('' .. )
part is the culprit that invokes this behavior - I got it from knockout.js tutorial, which says that the line is necessary to allow users from other origins to properly browse my web page. the page that is ran by knockout.js code is /w/
. I want sammy.js to work only in /w/
or, at least allow users to navigate to /logout
. How can I acplish this?
I recently started using knockout.js and sammy.js to modernize my app. However I got stuck with some problems.
I have some valid links on the page - users should actually navigate to that location, instead of imitating navigation behaviors using sammy.js
. I want only hash-based links to be routed by sammy.js, but it also intercepts links that does not contain any hashes.
for example, it intercepts <a href="/logout">logout</a>
.
the js part that does routing is :
Sammy(function () {
this.get('#/', function () {
...
});
this.get('#:id', function () {
...
});
this.get('', function () { this.app.runRoute('get', '#/') });
}).run();
I think this.get('' .. )
part is the culprit that invokes this behavior - I got it from knockout.js tutorial, which says that the line is necessary to allow users from other origins to properly browse my web page. the page that is ran by knockout.js code is /w/
. I want sammy.js to work only in /w/
or, at least allow users to navigate to /logout
. How can I acplish this?
- Did you ever solve this? If not I would definitely remend reproducing this on jsfiddle. – edhedges Commented Oct 30, 2013 at 20:15
-
@edhedges I thinks this is about defining entry points of your web app. my app is traditional multi-page app that has HTMLs generated from a python web server(and leverages sammy.js and hash+ajax to some extent). so, my solution to this was remove
this.get('', ...)
and putlocation.href = < some hash I want for this page, that is generated from the server >
. now non-hash urls aren't intercepted. – thkang Commented Nov 4, 2013 at 6:28 - You should answer the question yourself for others sake if you have resolved it. – edhedges Commented Nov 4, 2013 at 14:58
3 Answers
Reset to default 10Been a while since I used Sammy, but I think you can turn off this behaviour with the disable_push_state
setting:
Sammy(function() {
this.disable_push_state = true;
...
});
You can use "around" function. There is lot of options. I suggesting to do as follows,
Let as assume there is some urls. On hitting that url you want to municate server to the page. For example, logout url like "/logout".
Now make that url as follows
"/logout?reload=true"
So that, you can control by following Sammy's code
Sammy(function(){
this.around(function(callback) {
if(this.params.reload === 'true')
location.replace(this.path);
else
callback();
});
// Your routers are going to be here
}).run()
I found that if I pare the window.location to the a copy of the window location stored on start up, I can safely escape the Sammy trap. If the base urls match, I run the base route. If they don't match, I reload the window.location and unload Sammy.js.
function myUrl() {
var url= window.location.href;
if (window.location.hash) {
url= url.replace(window.location.hash, "");
}
if (window.location.search) {
url= url.replace(window.location.search, "");
}
return url;
}
...
Sammy(function () {
var myLocation = myUrl();
...
this.get('', function () {
if (myLocation === myUrl()) {
this.app.runRoute('get', '#/');
} else {
window.location.reload(true);
this.unload();
}
});
}).run();
本文标签: javascriptblock sammyjs from stealing 39real links39Stack Overflow
版权声明:本文标题:javascript - block sammy.js from stealing 'real links' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741457857a2379859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论