我正在運行Meteor 0.5.7,並嘗試在用戶單擊提交時顯示引導警報消息。如何使會話變量具有反應性?
client.js
Handlebars.registerHelper('isSuccessful',function(input){
return Session.get("success");
});
Template.form.events({
'click .submit' : function (event, template) {
if (condition) {
Session.set("success", true);
// hide warning, show success
$('#valid_form').show();
$('#invalid_form').hide();
} else {
Session.set("success", false);
// hide success, show warning
$('#valid_form').hide();
$('#invalid_form').show();
}
}
});
Template.form.rendered = function() { $('.alert').hide();};
page.html中
<body>
{{> page}}
</body>
<template name='page'>
{{> form}}
</template>
<template name='form'>
<!-- Show Alerts Above Form -->
<div class="alert alert-success" id="valid_form">..</div>
<div class="alert" id="invalid_form">..</div>
{{#if isSuccessful}}
<div>SHOW CONFIRMATION PAGE</div>
{{else}}
<div>SHOW INPUT FIELDS</div>
{{/if}}
<div>
</template>
當用戶最初點擊提交和條件沒有被滿足,他們必須點擊才能再次提交得到警告信息。所有其他時間的邏輯工作。
我已經看了這個 - How does Meteor's reactivity work behind the scenes?,並重新閱讀關於反應式編程部分的流星文檔,但仍有一些問題。
客戶端的else語句不應該默認爲會話變量success == false,並且句柄模板立即在{{else}}塊中選取它?直接困惑。謝謝,
當我只希望在需要時顯示警報時,您的建議將一直顯示警報之一。爲了澄清,只有當表單字段有效時,纔會彈出警報,並且表單字段在示例中未顯示,只是爲了保持簡短。希望這是有道理的? – gamengineers 2013-03-11 20:11:17
'$('。alert')。alert('hide')'在模板呈現時隱藏所有提醒 – Prashant 2013-03-11 20:25:28
Prashant,感謝您的努力,我更新了模板以反映我在本地的工作。正如你所看到的,我希望警報能夠顯示在可能有或沒有警報狀態的其他html元素之外。 – gamengineers 2013-03-11 21:02:04