当我的 SPA 初始化时,它必须进行一些 ajax 调用,这些调用需要在加载任何 View 之前运行。我已将它们放在 shell.js 的 activate 函数中。在其他 View 中,我从激活函数返回这些调用,一切正常。但在 shell 中我应该从 router.activate 调用返回。如何返回正确的 View 并确保进行这些 ajax 调用?我看到的是数据并不总是加载。
activate: function () {
var query =
$.when(
datacontext.getCurrentUser(),
datacontext.getGroupsAuthorized(),
datacontext.getTeams(),
datacontext.getRoles(),
datacontext.checkPermissions('views.shell')
)
.then(function (s0, s1, s2,s3, s4) {
/* code excluded */
},
function (f0, f1, f2) {
/* code excluded */
}).always( function() {
/* code excluded */
});
return router.activate('welcome');
},
所有数据上下文调用看起来都像这样:
getCurrentUser = function () {
return $.ajax({
url: 'api/currentuser/',
type: 'GET'
});
},
最佳答案
确保返回一个 promise ,以便 Durandal 知道何时允许继续创作。 沿线的一些东西应该可以解决问题。
activate: function () {
return $.when(
datacontext.getCurrentUser(),
datacontext.getGroupsAuthorized(),
datacontext.getTeams(),
datacontext.getRoles(),
datacontext.checkPermissions('views.shell')
)
.then(function (s0, s1, s2,s3, s4) {
/* code excluded */
return router.activate('welcome');
}
},
function
和 always
需要相应调整。
关于jquery - Durandal:如何在 shell 激活功能中正确发出 ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17623311/