问题
I have an SPA app using DurandalJS that works great. Links are routed, view models are activated, etc.
The problem is, I have an anchor that points to a page that actually lives on the server. I need the browser to actually query this page (it's an asp.net MVC page).
It seems the router in Durandal hooks into all links that are relative (or on the same domain, really) and if it finds no route, calls the handleInvalidRoute method of router.
I cannot for the life of me figure out how to tell Durandal to not route this URL. I need to keep middle / ctrl+click functionality as well.
Documentation is no help and google has turned up nothing. I also tried to look into the router / SammyJs code, but I couldn't see anything obvious.
This question seems to be the same as mine, but the answer does not work for me. My URLs are all actual URLs (not hash-prefixed) so I can't turn off PushState.
回答1:
The easiest way I found so far was to create an absolute url (e.g. http://myapp.com/whatever-that-is-not-a-sammy-route) and add
target="_top"
to the anchor. This allows new tab behavior (ctrl+click, middle click, open new tab context menu) and when the link is left clicked, it opens in the current window, bypassing Sammy routing!
回答2:
Not sure if this will help, but I created a click binding which called location.assign. I used PHP, but it should still work for you. Here is an example:
In View
<a data-bind="click: goToMyLink">Go</a>
In ViewModel
goToMyLink: function() {
location.assign('myLink.php'):
}
来源:https://stackoverflow.com/questions/16577423/ignore-an-anchor-in-durandaljs-app