因爲這是持久性的,你就需要設置一個集合中,以切換狀態
你的JS在你的點擊處理程序:
Template.yourtemplate.events({
'click #yourbutton':function(event,template) {
var state = MyCollection.findOne({}).state
MyCollection.update({}, {$set:{state:!state}});
}
});
Template.yourtemplate.helpers({
item:function() {
return MyCollection.findOne({});
}
});
那麼你的HTML:
<template name="yourtemplate">
{{#if yourtemplate.state}}
<div id="yourbutton">STATE 1</div>
{{else}}
<div id="yourbutton">STATE 0</div>
{{/if}}
</template>
當然,以上只是一個示例,您可以使用每個塊幫助程序或不同的模板幫助程序來返回您的數據。但希望你明白這個主意。
我會推薦使用兩個不同div的if語句(你甚至可以只使用css類),但我不會推薦使用if語句或html屬性中的句柄,因爲spark標註(流星的模板系統)放入它們通常是html的註釋,並且它們在html屬性中的表現並不好。
的作品就像一個夢 – funkyeah 2013-05-09 05:44:36
能否請您解釋一下({} ).state'' .state'在做什麼? 'findOne()'搜索匹配,但是'.state'在這裏意味着什麼? – Edgar 2016-02-25 23:38:59