【发布时间】:2014-03-24 01:25:15
【问题描述】:
SO 的长期用户,但第一次发帖。
我有一个 Meteor Web 应用程序,在该应用程序中,用户会看到一个带有单个输入的页面。一旦他们采取行动,下面的部分就会出现。这个过程会重复。
Journey.html
<head>
<title>Page Journey</title>
<script type="text/javascript">
function showValue(newValue)
{
document.getElementById("range").innerHTML=newValue;
}
</script>
</head>
<body>
{{> Page0}}
{{> Page1}}
{{> Page2}}
{{> Page3}}
</body>
<template name="Page0">
<h1>This is Page 0!</h1>
<p>Welcome to Journey App.</p>
<input type="range" min="1" max="10" value="1" step="1" onchange="showValue(this.value)" />
<span id="range">1</span>
</template>
<template name="Page1">
{{#if showpage1}}
<h1>This is Page 1!</h1>
<p>{{page1copy}}</p>
<input type="button" value="Show Page 2" />
{{/if}}
</template>
<template name="Page2">
{{#if showpage2}}
<h1>This is Page 2!</h1>
<p>{{page2copy}}</p>
<input type="button" value="Show Page 3" />
{{/if}}
</template>
<template name="Page3">
{{#if showpage3}}
<h1>This is Page 3!</h1>
<p>{{page3copy}}</p>
{{/if}}
</template>
Journey.js
//On initialization, set position to 0.
if (Meteor.isClient) {
Meteor.startup(function () {
Session.set("position",0);
});
}
if (Meteor.isClient) {
Template.Page0.events({
'click input': function () {
var value = getValue();
if (value>5) { Session.set("position",1); }
else { Session.set("position",0); }
}
});
Template.Page1.events({
'click input': function () {
//Increment position
Session.set("position",2);
}
});
Template.Page2.events({
'click input': function () {
//Increment position
Session.set("position",3);
}
});
}
if (Meteor.isClient) {
Template.Page1.showpage1 = function () {
return Session.get("position") > 0;
}
Template.Page2.showpage2 = function () {
return Session.get("position") > 1;
}
Template.Page3.showpage3 = function () {
return Session.get("position") > 2;
}
}
if (Meteor.isClient) {
Template.Page1.rendered = function(){
console.log ('Page 1 rendered');
};
Template.Page2.rendered = function(){
console.log ('Page 2 rendered');
};
Template.Page3.rendered = function(){
console.log ('Page 3 rendered');
};
}
function getValue()
{
var value = parseInt(document.getElementById("range").innerHTML);
return value;
}
这一切都错了吗?
我想在每次加载新页面时触发一个功能(例如,谷歌分析跟踪代码)。 .rendered() 函数在页面第一次加载时被触发,所以这并不理想。
【问题讨论】:
标签: javascript meteor handlebars.js