你可以在這裏使用一些助手和模板變量。
可以說你有這個模板。
<template name="example">
{{#if clicked}}
{{> firstPage}} <!-- the firstPage template will be rendered if the var is == false -->
{{else}}
{{> secondPage}} <!-- this will be render when the session is equals to true -->
{{/if}}
</template>
現在Javascript。
首先在onCreate函數聲明默認值(類似這樣Session.setDefault)
Template.example.onCreated(function(){
var self = this;
self.vars = new ReactiveDict();
self.vars.setDefault('clicked' , true); //default is true
});
現在的事件來改變點擊的狀態爲假(以會話類似的get/set)。
Template.example.events({
'click .waves-effect':function(event,template){
var instance = Template.instance();
instance.vars.set('clicked', false) //set to true.
}
})
現在我們的東西聽我們的變量的變化,讓我們使用一個輔助
Template.example.helpers({
clicked:function(){
var instance = Template.instance(); //http://docs.meteor.com/#/full/template_instance
return instance.vars.get('clicked') //this will return false(default) | true
}
})
注意這裏我們使用無功字典至極共享會話的相同構造結。
meteor add reactive-dict
這裏是MeteorPad例