Polymer Access Element Inside of Nested Template by Id - nested

Polymer provides access to elements by id via this.$['foo']. However, I find that I am unable to access elements by id that are in nested templates.
<template>
<div id="foo"></div>
<template>
<div id="bar"></div>
</template>
</template>
In this situation this.$.foo works but this.$.bar does not. Are you able to access elements inside of a nested template by id and if so how?
In my code I'm using a conditional template to include some html if a attribute is true. I was providing this functionality in javascript by editing the html but think that conditional templates more clearly show what is going on and I would prefer to use this method.

Using this.$.<id> syntax does work with inner templates. The problem in your example is that the inner <template> is inert, and it isn't rendered. I've rewritten your example so that the inner <template> is rendered, and you can see that the this.$.<id> syntax works for both the outer and inner <template>s:
<polymer-element name="my-element">
<template>
<div id="foo">
<p>Old foo</p>
<template if="{{showBar}}">
<div id="bar">
<p>Old bar</p>
</div>
</template>
</div>
<button on-tap="{{changeContent}}">Change Content</button>
</template>
<script>
Polymer({
showBar: true,
changeContent: function() {
this.$.foo.querySelector('p').textContent = 'New foo';
this.$.bar.querySelector('p').textContent = 'New bar';
}
});
</script>
</polymer-element>
Pressing the "Change Content" button finds the #foo and #bar <div>s and updates the text of the contained <p>s.
You can try out the answer at http://jsbin.com/roceta/edit.

Related

Vuetify Grid system adding a horizontal scroll bar in my dialog

Good day. I have the following dialog that whenever i add anything inside it's component an horizontal scrollbar shows up and the right of my dialog suddenly has a space on it's right side. like in reference pic
This right here all the code i have:
My view:
<template>
<v-main>
<v-dialog
v-model="dialog"
>
<v-card background-color="white">
<div>
<myComponent/>
</div>
</v-card>
</v-dialog>
<v-container>
// rest of my code
</v-container>
</main>
</template>
My Component:
<template>
<v-main>
<v-row align='center' justify="center">
<v-col class="d-flex justify-center" cols="5">
hello
</v-col>
</v-row>
</v-main>
</template>
Why does this happen exactly. Every page i has scooped styles and most of them don't even have any css applied to it. When i remove the "hello" together with the v-col tags the dialog goes back to normal like so:
Add style="overflow-x: hidden;" to your div that is wrapped around <myComponent/>

change in polymer paper-menu

I've recently updated polymer and components, and a weird thing appears.
I have paper-dialog element, and inside, on the left a paper-menu item with multiple paper-item. On the right, a simple div with some content.
When clicking on a paper-item, the content of the div will change.
Since the update, when clicking on a paper-item element, the dialog will automatically close.
After searching, it appears that if I remove the paper-menu element and only left the multiple paper-item, the problem will no longer occurs.
After looking inside the iron-menu-behavior, I found a new function (an override of _activateHandler), which, when commented, will kept previous functionning without closing the dialog.
I keep searching to find any solution, but anyone encountered the same problem?
For information :
<paper-dialog id="dialog" with-backdrop>
<div id="content"></div>
</paper-dialog>
And inside my div is added this :
<div class="content">
<div class="list">
<paper-menu>
<template is="dom-repeat" items="{{menu}}">
<paper-item on-click="_onCategorySelection">
<iron-icon icon="{{item.icon}}" class="icon"></iron-icon>
<span class="text">{{item.text}}</span>
</paper-item>
</template>
</paper-menu>
</div>
<div id="listContent">
<div class="noContent" hidden$="{{content}}">
<div class="noContentText">Pas de catégorie séléctionnée</div>
</div>
<template is="dom-if" if="{{content}}">
<div class="withContent">
<template is="dom-repeat" items="{{content}}" as="widget">
<badge data="{{widget}}" on-click="_onWidgetSelect"></badge>
</template>
</div>
</template>
</div>
</div>
Thanks a lot
Okay, looking at the problem, is seems to be a known issue: https://github.com/PolymerElements/iron-menu-behavior/issues/40
I'll point your JSBIN too there!
EDIT: it is yours! :)

marionette keep class on layout element

I'm trying to preserve the full markup of a template, including the class on the root node when using a marionette region. I'm also trying to avoid creating an extra wrapping div. I've solved the problem, but in a way which I don't think is satisfactory.
I am creating and rendering a layout like this:
MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
mainRegion: "#main"
});
AppLayout = Backbone.Marionette.Layout.extend({
template: '
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4"></div>
</div>
'
});
var layout = new AppLayout();
MyApp.mainRegion.show(layout);
layout.show(new MenuView());
And the result is that my template is rendered like this:
<div id="main">
<div>
<div class="col-md-8"></div>
<div class="col-md-4"></div>
</div>
</div>
Notice, the class="row" is missing from the root node of the template. It appears that marionette is removing the root div from my template, and then wrapping the contents in a new div.
I have managed to hack a solution to this like this
AppLayout = Backbone.Marionette.Layout.extend({
template: '
<div><!-- sacrificial div -->
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4"></div>
</div>
</div>
',
onRender: function () {
// get rid of that pesky wrapping-div
// assumes 1 child element.
this.$el = this.$el.children();
this.setElement(this.$el);
}
});
I'm adding an extra root div (my sacrificial div) to my template which marionette removes, and then I'm telling marionette to use the first child as the layouts 'el' (as per Turning off div wrap for Backbone.Marionette.ItemView).
This seems crazy!
Can somebody suggest a better way?
EDIT: n.b. I'd like to keep all the template logic in the template, so don't want to have to use code in my view to specify the class on the root node - if I do this, I end up with a maintenance headache.
Try with
AppLayout = Backbone.Marionette.Layout.extend({
template: '
<div class="col-md-8"></div>
<div class="col-md-4"></div>
',
className: "row"
});
AppLayout is missing its region(s).
AppLayout = Backbone.Marionette.Layout.extend({
template: '
<div class="row">
<div id="region1" class="col-md-8"></div>
<div id="region2" class="col-md-4"></div>
</div>
',
regions: {
region1: '#region1',
region2: '#region2'
}
});
Then at your instantiated layout:
layout.region1.show(new MenuView());
layout.region2.show(new MenuView());
Instead of assigning the template to a string, compile the HTML into a template function with underscore. Something like this:
template: _.template(
'<div class="row">' +
'<div class="col-md-8"></div>' +
'<div class="col-md-4"></div>' +
'</div>'
)

Use layouts with meteor-router

From this discussion here,
Allow a passed in variable to {{renderPages}} helper
I am trying to display a template within an existing {{renderPage}};
My Use case: I have a very simple high level body template
<template name="body">
{{> header}}
{{> notifications}}
<div class="content" id="content">
<div id="main-content">
{{{renderPage}}}
</div>
</div>
{{> footer}}
</template>
And as you can see, my main-content has the {{renderPage}}. This works great, when i set up a route:
'/home': 'home'
The route finds the template 'home' and replaces renderPage with that template. I want to expand that now and see if route templates can be placed within specific divs.
For example, in my home.html template:
<template name="home">
{{#if isAdmin}}
<h1>student</h1>
{{/if}}
<div id="inner_home">
{{renderPage innerHome}}
</div>
</template>
Is it possible to have a different route /home/tools : 'home_tools render it's template not in the highest main content div but within my child div inner_home?
EDIT:
I am evaluating an approach that just uses JQuery and the callback function of the meteor-router method to grab the template i am looking for and add it directly to the concerning div tag.
Something like:
var foundTemplate = _.template($('#item-template').html())
$('#div_example').html(foundTemplate); //
If anyone has a better approach, please let me know.
Sure.
client.js
Meteor.Router.add({
'/home/tools': function() {
Session.set("homeTemplate", "tools");
return 'home';
},
'/home/admin': function() {
Session.set("homeTemplate", "admin");
return 'home';
}
});
home.js
Template.home.homeTemplateTools = function() {
return Session.equal("homeTemplate", "tools");
};
Template.home.homeTemplateAdmin = function() {
return Session.equal("homeTemplate", "admin");
};
home.html
<template name="home">
{{#if isAdmin}}
<h1>student</h1>
{{/if}}
<div id="inner_home">
{{#if homeTemplateTools}}
{{> homeTools}}
{{/if}}
{{#if homeTemplateAdmin}}
{{> homeAdmin}}
{{/if}}
</div>
</template>

nested template rendering in backbone.js

I have a template like
script type: "text/template", id: "list-template", '''
<div class="display">
<div id="list-name"><%= name %></div>
<span class="list-destroy"></span>
</div>
<div>
<ul id="ul-cards">
</ul>
</div>
<div class="edit">
<input class="list-input" type="text" value="<%= name %>" />
<input id="btnEdit" type="button" value="Save" class="primary wide js-add-list" />
<input id="hdnListId" type="hidden" value="<%= listId%>" />
</div>
<form class="add-list-card js-add-list-card clearfix">
<textarea placeholder="Add card" class="new-card"></textarea>
<input type="button" value="Add" class="primary js-add-card">
<a class="app-icon close-icon dark-hover cancel js-cancel-add-card" href="#" id="closeCard"></a>
</form>
'''
in this template i have <ul id="ul-cards"> element in which i want to render another template which display list inside this ul.
this template is :
script type: "text/template", id: "card-template", '''
<div>
<span class="card-name"><%= name %></span>
</div>
'''
is it possible or i have to do it in another way?
please help me if anyone have idea.
thanks in advace.
it is worked but still i have one problem in data display in
<ul id="ul-cards"> there sholud be 2 as per records in my database but it will display only 1 . data fetch properly but display only last data.
There are two ways to do this: the DOM way and the template way.
The DOM way involves adding your views using DOM methods: you have your ListView and your CardView; the ListView invokes individual CardViews that fill in the ListView's element.
The template way requires that you remember this: backbone's views are policy frameworks, not policy templates. Render doesn't have to render into the DOM. You can use render() to return a string, for example. If your event manager is on the ListView object only (possible; I've done this), then you can have ListView invoke "the resulting array of an array of CardView renders" and insert that directly into ListView's template. This is actually faster, as you only require the browser to analyze the entire ListView HTML blob once, when it's inserted into the innerHTML of the parent DOM object.

Resources