link to another controller with hot towel and durandal

*爱你&永不变心* 提交于 2019-12-07 12:10:52

问题


I am developing an mvc4 app with multiple spa's.

I am using the hot towel template for my spa.

What I want to do is have a anchor link within my views for a spa go to the index action for another controller, so my users can exit one spa and open another. Not all actions will start a spa some are regular mvc style pages.

I have put code like like the following in my spa html pages:

<a href="/controller/action">Navigate to another controller</a>

This will change the url in the browser but always reloads the default html page for the spa. If I hit the refresh button in the browser then if will go to the proper page.

I have been able to put a target on the anchor tag of _parent or _top like:

 <a href="/controller/action" target="_parent">Navigate to another controller</a>

and it will navigate to the new controller page.

I believe it is something in the durandal framework that is preventing the spa from navigating to the second controller, but since I am just starting to work with this, I am stumped as what I need to change. I think there should be a better way than using the target in the anchor tag, or is that the best option.


回答1:


I hope that I have understood you correctly. I believe this behaviour is caused my Sammy.js (which the durandal router is currently based on). The default behaviour of Sammy is to hijack all links and process them as regards to the SPA itself.

I didn't like this behaviour for the website I am working on and needed links to other MVC web pages to be possible from within the SPA. What I really wanted is for regular links to just work and hash links to be interpreted as a link to another view within the SPA. So '/controller/action' would go to another web page and '#moduleId' would go to another durandal V/VM.

I found this Sammy option that works for me and set it in my main.js

    // This stops Sammy from hijacking regular links
    Sammy.Application.prototype.disable_push_state = true;


来源:https://stackoverflow.com/questions/15731534/link-to-another-controller-with-hot-towel-and-durandal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!