handlebars.js - Meteor: Preserving ClassName Flags -
i'm having trouble using preserve method in meteor. doesn't seem want do.
basically, have 3 nested templates which, when rendered, in closed state:
<div class="dropdown"><!--from template1--> <div class="group"><!--from template2--> <div class="subgroup"><!--from template3--> i'm subgroup's content! </div> </div> </div>
to view content in .subgroup node, template wrappers need bear .open class name. done on click event. here's looks in open state:
<div class="dropdown open"><!--from template1--> <div class="group open"><!--from template2--> <div class="subgroup open"><!--from template3--> i'm subgroup's content! </div> </div> </div>
the issue is, when events fire , alter database within .subgroup's content, looks template2 , template3 both re-rendered , lose programmatically applied .open class.
i've tried using template.template2.preserve(['.group']);
on each template every selector think affect it. i've tried {{#constant}} , {{#isolated}} helpers, have yet expected results these.
what's right way keep meteor wiping class names out?
you should use session variables coupled handlebars helpers.
<template name="dropdown"> opened helper resolves current dropdown state <div class="dropdown {{opened}}"> iterate on each group using cursor {{#each groups}} call subtemplate fed current group document fetched cursor {{> group}} {{/each}} </div> </template> <template name="group"> assign unique id group div, using document._id <div id="group-{{_id}}" class="group {{opened}}"> ... , on </div> </template> template.dropdown.helpers({ opened:function(){ // session variable undefined on first page view (closed state) // have value set in click handler return session.get("dropdown-opened")?"open":""; }, groups:function(){ return groups.find(); } }); template.dropdown.events({ "click .dropdown":function(){ var opened=session.get("dropdown-opened"); // toggle open state session.set("dropdown-opened",!opened); } }); // executed once on each template instance creation template.group.created=function(){ // this.data._id fetched document._id // comment line if don't want stuff collapsed // when template re-created (ie when change page) session.set("group-"+this.data._id+"-opened",false); }; template.group.helpers({ opened:function(){ // this._id fetched document._id return session.get("group-"+this._id+"-opened")?"open":""; }, subgroups:function(){...} }); template.group.events({ "click .group":function(event,template){ // once again, template.data._id corresponds id of // document used feed group template var sessionkey="group-"+template.data._id+"-opened"; var opened=session.get(sessionkey); session.set(sessionkey,!opened); } });
this untested code similar stuff on app , works charm. think meteor way (session+helpers) of achieving kind of things (as opposed using jquery manipulate dom , class names). unfortunately, kind of pattern quite verbose , can bit obscure developers coming non-meteor classic front-end js web-app development, i'm sure gonna improved.
Comments
Post a Comment