Ben*_*ley 5 javascript html5 single-page-application durandal hottowel
我正在使用Hot Towel SPA项目,我试图在激活视图时调用简单的js函数.我所看到的是,当调用activate函数时,似乎没有加载该项.
我也尝试将代码放在由其他SO帖子建议的activate调用的初始化函数中.这似乎没有帮助.
那么在Durandal/HotTowel中调用视图加载函数的推荐方法是什么?
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
}
});
Run Code Online (Sandbox Code Playgroud)
home.html(查看)
<section>
<div id="myBtn">test</div>
</section>
Run Code Online (Sandbox Code Playgroud)
Yev*_*nov 12
根据与DOM交互的最新Durandal文档,viewAttached重命名为附加,因此使用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());
}
});
Run Code Online (Sandbox Code Playgroud)
当durandal附加一个视图模型时,它会查看该viewAttached
方法的模型.我在下面修改了你的代码.它应该找到你正在寻找的jQuery元素.
define(['services/logger'], function (logger) {
var vm = {
activate: activate,
viewAttached: viewAttached
title: 'Home'
};
return vm;
function activate() {
logger.log('Home View Activated', null, 'home', true);
}
function viewAttached(view) {
$(view).find("#backBtn").hide();
console.log($(view).find("#myBtn").html());
}
});
Run Code Online (Sandbox Code Playgroud)
请查看底部的"合成"页面以获取更多信息viewAttached
.
http://durandaljs.com/documentation/Using-Composition.html