Durandal:如何在该视图的activate()函数中远离当前视图? [英] Durandal: How to route away from current view within that view's activate() function?
问题描述
我有以下内容:
function activate(routeData) {
// make dataservice call, evaluate results here. If condition is met, reroute:
if (true){
router.navigateTo("#/someRoute");
}
alert ("should not be shown");
}
但是警报被触发了,然后然后视图改变了。
The alert is getting hit however, and then the view changes.
如何完全远离当前项目并防止该虚拟机中的其他任何代码被击中?
How do I fully navigate away from the current item and prevent any further code in that vm from being hit?
更新:
我尝试使用 guardroute
,但是我必须激活viewModel调用返回数据的dataservice,该数据确定是否应该重新路由。完全使用guardroute可以防止调用dataservice(因为activate函数中的任何内容都不会被命中)。
I tried using guardroute
but I have to activate the viewModel to call the dataservice that returns the data that determines whether or not I should re-route. Using guardroute totally prevents the dataservice from getting called (since nothing in the activate function will get hit).
我也尝试从 if
块返回,但这仍然加载view / viewAttached / etc,因此UX是
I also tried returning from the if
block but this still loads the view / viewAttached / etc so the UX is glitchy.
推荐答案
这里是@EisenbergEffect答案,它回答了 Google网上论坛。
Here's @EisenbergEffect answer to a quite similar discussion in google groups.
实施canActivate on您的视图模型。返回一个错误的承诺,
然后使用重定向链接。
Implement canActivate on your view model. Return a promise of false, then chain with a redirect.
您可能想举@JosepfGabriel的例子(讨论)在Durandal 1.2中进行尝试。为您的Durandal版本检查正确的 router
语法,您可能必须用 router.navigateTo(#/ YourHash,'replace ')
。
You might want to give @JosepfGabriel's example (discussion) a try in Durandal 1.2. Check the correct router
syntax for your Durandal version, you might have to substitute it with something like router.navigateTo("#/YourHash", 'replace')
.
canActivate: function () {
return system.defer(function (dfd) {
//if step 2 has a problem
dfd.resolve(false);
})
.promise()
.then(function () { router.navigate("wizard/step1", { trigger: true, replace: true }); });
}
但是,这在Durandal 2.0中不起作用,并且有功能要求 https://github.com/BlueSpire/Durandal/issues/203 。
However this is NOT working in Durandal 2.0 and there's a feature request https://github.com/BlueSpire/Durandal/issues/203 for it.
这篇关于Durandal:如何在该视图的activate()函数中远离当前视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!