Meteor: Preselected options lost after rerendering multiple select - meteor

I have a multiple select with a Handlebar template in Meteor.js. On first rendering, everything is fine ("Politics" and "People" are preselected as expected):
As soon as the template has to be rerendered (because a Session variable changes, e.g. Session.set("foo", "Hello World!")), the third option is not preselected anymore:
My setup:
<template name="select">
<select name="foo" multiple>
<option value="1">Tech</option>
<option value="2" selected>Politics</option>
<option value="3" selected>People</option>
</select>
</template>
<template name="test">
{{foo}}
{{> select}}
</template>
{{> test}}
Template.test.helpers(
foo: ->
Session.get("foo")
)
Do you have any idea why the options are preselected anymore after rerendering?

Solution is:
<template name="test">
{{#isolate}}
{{foo}}
{{/isolate}}
{{> select}}
</template>
Typically, the exact extent of re-rendering is not crucial, but if you
want more control, such as for performance reasons, you can use the
{{#isolate}}...{{/isolate}} helper. Data dependencies established
inside an #isolate block are localized to the block and will not in
themselves cause the parent template to be re-rendered. This block
helper essentially conveys the reactivity benefits you would get by
pulling the content out into a new sub-template.
If you put {{foo}} inside {{#isolate}} ... {{/isolate}} then parent template won't be rerendered, so also {{> select}} won't be affected.

So I am not sure why you are loosing the multiple select but I can recommend you put an {{#isolate}} tag around your{{> select}} template. That should keep the template from re-rendered. Though it will not help if your select template re-renders because its data changes. Hope that helps...

Related

Passing reusable blaze dropdown to sweet alert 2 html

I have a simple meteor template that creates a dropdown:
<template name="Select">
<div class="input-fields">
<select>
{{#each options}}
<option value="{{_id}}" selected={{optionSelected}}>{{name}}</option>
{{/each}}
</select>
</div>
</template>
I normally use it by adding it as a blaze spacebar:
{{> Select
options=options
}}
Now I'm trying to pass this dropdown as the HTML argument inside a Sweet Alert 2 dialog, but blaze doesn't render the component, since swal is probably triggered after the blaze rendering is already finished. Is there another way to reuse the template inside the swal call?
swal.fire({
title: "MyDropdown",
HTML: "{{> Select
options="+options+"}}",
})
I know you can natively add a dropdown in Sweet Alert 2, but I'm specifically interested in how to add my own templates to it.
Did you try
Blaze.toHTML('Select')
or
Blaze.toHTMLWithData('Select',{options})

how to stop conditional rendering from flashing?

I'm not sure if this is a Meteor issue or a generic one but I have the following code in my app.
<template name='admin'>
<div class='admin container-fluid noPadding'>
{{#if isInRole 'Admin'}}
<h3 class="homepageText">Server Statistics</h3>
{{> serverFacts}}
{{else}}
You don't belong here!
{{/if}}
</div>
When the page renders I see "You don't belong here!", then "Server Statistics" replaces it a second or so later. I have the same problem other places in my app always with a Blaze {{#if ...}} involved. Is there a way to stop the page from displaying in the browser until the rendering has completed and settled down?
This is a generic issue with reactive applications - rendering often happens while the data is still being pushed to the client. The normal solution is to use a spinner (ex: sacha:spin) until the underlying subscription(s) are ready. Then in Blaze you end up with:
<template name='admin'>
{{#if loading}}
{{> spin}}
{{else}}
<div class='admin container-fluid noPadding'>
{{#if isInRole 'Admin'}}
<h3 class="homepageText">Server Statistics</h3>
{{> serverFacts}}
{{else}}
You don't belong here!
{{/if}}
{{/if}}
</div>
You'll need a helper to compute loading based on your subscriptions. In a more complicated layout backed by several subscriptions you might end up with more than one spinner spinning at a time.

dynamicComponent Stencil

In my Stencil theme, I am including a few different size charts for products which I intend to include by just changing the path to the size chart document. I found dynamicComponent in the Stencil docs and I thought I understood the way it worked. In my higher level partial, I am binding the string to component in this way - (product.html)
<div class="container" itemscope itemtype="http://schema.org/Product">
{{> components/products/product-view schema=true sizeChart='components/products/size_charts/tshirt.html'}}
{{#if product.videos.list.length}}
{{> components/products/videos product.videos}}
{{/if}}
{{#if settings.show_product_reviews}}
{{> components/products/reviews reviews=product.reviews product=product urls=urls}}
{{/if}}
</div>
(product-view.html)
{{#if sizeChart}}
<div class="tab-content" id="tab-sizeChart">
{{dynamicComponent sizeChart}}
</div>
{{/if}}
Where all I wish to change is the variable sizeChart in future theme maintenance. When the page renders, the place where I wrote the dynamicComponent is blank.
I used if conditionals instead of dynamicComponent. It wasn't what I thought it was.

Javascript input masking plugins that work with Meteor Autoform + SimpleSchema?

Has anyone here gotten a jQuery masking library to work with Autoform? I'm trying BootStrap Form Helpers
<div class="col-md-12">
<div class="form-group">
{{> afFieldLabel name="customerInfo.agentPhone"}}
<input name="customerInfo.agentPhone" type="text" class="form-control track-order-change bfh-phone" required data-schema-key="customerInfo.agentPhone" data-format="ddd ddd-dddd">
</div>
</div>
I have customerInfo.agentPhone set to required in my schema.
When I do this code I lose the ability to validate the form against the schema. The input field can be blank and the error message that says this field can't be blank never shows up.
I figured it out. It turns out that I was using afFieldInput incorrectly - every time you use afFieldInput you have to have an #if statement to change the CSS of the wrapping div to show the validation message.
<div class="col-md-12 {{#if afFieldIsInvalid name="customerInfo.agentPhone"}}has-error{{/if}}">
<div class="form-group">
{{> afFieldLabel name="customerInfo.agentPhone"}}
{{> afFieldInput name="customerInfo.agentPhone" class="form-control track-order-change bfh-phone" data-format=" (ddd) ddd-dddd"}}
</div>
</div>
Note two things:
You have to put bfh-phone for the class.
You have to put a space in front of the parenthesis " (ddd) ddd-dddd". If you don't and just do "(ddd) ddd-dddd" the ( gets inserted into the text field on load. And obviously if you're checking to make sure that the text field is not blank, it won't fire because the ( is loaded in there already.
I've never used autoform, but looking over the documentation I'm wondering have you tried this:
{{> afFieldInput name="customerInfo.agentPhone" data-format="ddd ddd-dddd" }}
(obviously adding any additional attributes you may have set to afFieldInput before)

R brew select statement

Question - Re: R brew package with HTML (using Rook)
When using forms to retrieve queries, usually when the screen refreshes, the user's preselected option remains selected in the form. For eg., if there was a name field and I enter John,... enter other form data, hit Submit, when the page refreshes, the form will still show "John" in the name field instead of an empty box.
I can pre-fill an entry in a text type input field using something like --
...
<input class="someclass" type="text" name="Name" value="<%=Name%>" />
... (other form data)
When the user hits submit and the page refreshes, R prefills the name field with the value from <%=Name%>
I'm having some difficult in replicating the same when using drop-down menus. The idea is that when the page refreshes, the option that the user had selected in the previous query will be active.
<select class="someclass" name="group3" style="width:200px;">
<option value="," selected>None</option>
<option value="Name,">Name)</option>
<option value="Phone">Phone</option>
</select>
I think one way could be to enter something like say,
<%if (group3=="Name"){ print("selected") } ... etc for each of those options in the respective lines, but that is a bit cumbersome. Could you please share your thoughts on the same,
Got this working using jQuery finally. The inline <%= if(..) print ("selected") ... within the tags wasn't working.
Add the id=something tag to select --
<select class="someclass" name="group3" id="myselect" style="width:200px;">
<option value=",">None</option>
<option value="Name">Name)</option>
<option value="Phone">Phone</option>
</select>
Add jquery.js to the html file in the header--
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
In my Rook brew file, I have something like,
forminput <- req$GET()
userselection <- forminput$group3 #group3 is the class name for the select statement
and in the HTML document, before the closing form tag, enter the following --
<script>
$("#myselect").val("<%=userselection%>").attr("selected", "selected");
</script>

Resources