我正在使用 Hot Towel SPA 项目 我正在尝试在激活 View 时调用一个简单的 js 函数。我看到的是调用激活函数时似乎没有加载该项目。
我还尝试按照其他 SO 帖子的建议将代码放入由 activate 调用的初始化函数中。这似乎没有帮助。
那么在 Durandal/HotTowel 中推荐的在 View 加载时调用函数的方法是什么?
home.js(查看模型)
define(['services/logger'], function (logger) {
var vm = {
activate: activate,
title: 'Home'
};
return vm;
function activate() {
logger.log('Home View Activated', null, 'home', true);
return init();
}
function init() {
$("#backBtn").hide();
console.log($("#myBtn").html()); // returns undefined
}
});
home.html( View )
<section>
<div id="myBtn">test</div>
</section>
最佳答案
根据最新的 Durandal 文档,位于 Interacting with the DOM , viewAttached 重命名为 attached,因此您使用 Durandal 2 的代码将如下所示:
define(['services/logger'], function (logger) {
var vm = {
activate: activate,
attached: attached
title: 'Home'
};
return vm;
function activate() {
logger.log('Home View Activated', null, 'home', true);
}
function attached(view, parent) {
$(view).find("#backBtn").hide();
console.log($(view).find("#myBtn").html());
}
});
关于javascript - 在 Hot Towel/Durandal 单页应用程序中加载 View (激活)时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16444307/