Change google-map center using Polymer - polymer

I'm working on a custom element using Polymer. I want to change its center after it was rendered. I can't found it on Polymer docs. Anyone could help me?
<dom-module id="my-map">
<template>
<google-map latitude="{{latitude}}" longitude="{{longitude}}" zoom="15">
</google-map>
</template>
<script>
Polymer({
is: "my-map"
// I want to change map center here
});
// or here.. I don't know
</script>

In your <script> tag you can create a ready call back function. See lifecycle callbacks
Polymer adds an extra callback, ready, which is invoked when Polymer has finished creating and initializing the element’s local DOM.
In here you can change the longitude and latitude properties of your my-map element which you are passing into the google-map element:
<dom-module id="my-map">
<style>
google-map {
height: 600px;
width: 600px;
}
</style>
<template>
<google-map latitude="{{latitude}}" longitude="{{longitude}}" zoom="15"></google-map>
</template>
</dom-module>
<script>
Polymer({
is: "my-map",
ready: function () {
this.latitude = 37.77493;
this.longitude = -122.41942;
}
});
</script>
Alternatively, if you can give the longitude and lattitude properties default values (see here):
<script>
Polymer({
is: "my-map",
properties: {
longitude: {
type: Number,
value: -122.41942
},
latitude: {
type: Number,
value: 37.77493
}
}
});
</script>

Related

Listener method not defined

I'm a little new to Polymer and don't quite get what's happening here. I'm trying to create a simple form page. This is the code:
<dom-module id="sams-add-student">
<template >
<div class="vertical-section">
<paper-button on-click="addstudent">SUBMIT</paper-button>
</div>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'sams-add-student',
properties: {
item: {
type: Object
},
addstudent: function (event) {
console.log('addstudent');
}
}
});
})();
</script>
</dom-module>
However, I get an error that the listener method is not defined. Am I missing something?
You've incorrectly declared the addstudent method inside properties when it should actually be outside properties at the top-level of the object.
Polymer({
is: 'sams-add-student',
properties: {
// addstudent: function() {...} // DON'T DO THIS HERE
},
addstudent: function() {...} // DO THIS HERE
}
codepen
If it is a paper input, you could use something like :
this.$.IDofyourelement.value;

Polymer dom-repeat not displaying the dynamically added values

Polymer dom-repeat is not working as expected, when I add a new element to the Array dynamically, dom-repeat not displaying the value.
I am using the Polymer's array mutation methods when pushing items into the array, but still not working.
Please check the codepen link;
<dom-module id="wind-studio-widget">
<template>
<div on-tap='_addNewPad' style="cursor:pointer"><strong>Click Me</strong></div>
<template id="padListContainerId" is="dom-repeat" items="[[padList]]">
<p style="border:1px solid red"> <span>[[item.id]]</span></p>
</template>
</template>
</dom-module>
<script type="text/javascript">
Polymer({
is: 'wind-studio-widget',
properties: {
baseUrl: {type: String},
padList: {
type: Array,
notify: true
},
padListAverage: {type: Object}
},
ready: function () {
//this.baseUrl = localStorage.baseUrl;
//this.loadPadData();
this.padList = [{'id':'1'},{'id':'2'}];
},
_addNewPad: function () {
var newPadList = this.padList;
newPadList.push({'id':'3'});
this.set('padList', []);
this.set('padList', newPadList);
console.log(this.padList);
//this.$.padListContainerId.render();
}
});
</script>
http://codepen.io/nareshchennuri/pen/vxjvdO

Polymer. Update view after model changed

This is a common question in Polymer about updating view when model was updated (answer is to use this.set or this.push Polymer methods).
But when I have two elements:
First element:
properties:{
items: {
type: Array
}
},
someFunction: {
this.push('items', {'Name': 'something'});
}
Second Element has property which is bound to 'items' from first element
ready: function(){
this.items = firstElement.items;
}
I would like second element 'items' to be updated when 'items' updated on firstElement. I can't manually notify secondElement, because of app restrictions.
So for now I see (in devTools) that model of second element is correct ('items' contains objects), but view isn't updated.
How to update it?
You need to set notity on the property of the first element to receive changes outside of the element itself
properties:{
items: {
type: Array,
notify: true
}
},
then, in secondElement you can
<firstElement items="{{items}}" />
Let Polymer's two-way binding handle the notifications for you.
Consider two elements, x-foo and x-bar, in a container element, x-app.
In x-foo, declare items with notify:true so that its changes would be propagated upward.
Polymer({
is: 'x-foo',
properties: {
items: {
type: Array,
value: function() { return []; },
notify: true
}
}
});
In x-app, bind x-foo's items to x-bar's.
<dom-module id="x-app">
<template>
<x-foo items="{{items}}"></x-foo>
<x-bar items="[[items]]"></x-bar>
</template>
<script>
Polymer({ is: 'x-app' });
</script>
</dom-module>
Now, x-bar would be notified of all changes to the items array (when an item is added/removed).
HTMLImports.whenReady(() => {
"use strict";
Polymer({
is: 'x-app'
});
Polymer({
is: 'x-foo',
properties: {
items: {
type: Array,
value: function() { return []; },
notify: true
}
},
_addItem: function() {
this.push('items', {name: 'item' + this.items.length});
}
});
Polymer({
is: 'x-bar',
properties: {
items: {
type: Array
}
}
});
});
<head>
<base href="https://polygit.org/polymer+1.6.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<x-app></x-app>
<dom-module id="x-app">
<template>
<x-foo items="{{items}}"></x-foo>
<x-bar items="[[items]]"></x-bar>
</template>
</dom-module>
<dom-module id="x-foo">
<template>
<h2><code>x-foo</code></h2>
<button on-tap="_addItem">Add item</button>
</template>
</dom-module>
<dom-module id="x-bar">
<template>
<h2><code>x-bar</code></h2>
<ul>
<template is="dom-repeat" items="[[items]]">
<li>[[item.name]]</li>
</template>
</ul>
</template>
</dom-module>
</body>
codepen

Polymer data from router to dom-element

I started with polymer 1.0 and created a new route endpoint as mentioned below.
page('/users/:name', scrollToTop, function(data) {
app.route = 'user-info';
app.params = data.params;
});
so if I go to users/bob it will go to route "user-info"
in my index.html the route is defined as below
<section data-route="user-info">
<web-homepage></web-homepage>
</section>
where web-homepage is a custom element.
the custom element is as defined below
<dom-module id="web-homepage">
<template>
This is homepage
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'web-homepage',
ready:function() {
//want to get the route parameters (eg: bob) here
}
});
})();
</script>
</dom-module>
Is there a way to get the value route parameter ":name" i.e bob inside the ready function?
ready function is executed when the element is imported and created. If you access route params there, it will most likely be null/empty/undefined.
However you can pass route params to the element as below.
The params in app.params is the property of app. You can simply pass it to its child elements.
index.html should look like this
<section data-route="user-info">
<web-homepage route-params=[[params]]></web-homepage>
</section>
Define the route-params in web-homepage
<dom-module id="web-homepage">
<template>
This is homepage
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'web-homepage',
properties: {
routeParams: {
type: Object,
observer: '_routeParamsChanged'
}
},
ready:function() {
//ready function is run during element creation. route-params will most likely be empty/null/undefined
//Just random
//want to get the route parameters (eg: bob) here
},
_routeParamsChanged: function(newValue) {
console.log('route params: ', newValue);
}
});
})();
</script>
</dom-module>

Polymer 1.0: how to access a property value inside a function

How can I access the value of a property inside a function?
Here is the property
properties:{
name: {type: String}
}
<my-element name="Subrata"></my-element>
Inside <my-element> I have a function like this:
Approach #1
<dom-module id="my-element">
<template>
...
...
</template>
<script>
(function () {
is: 'my-element',
properties:{
name: {type: String}
},
getMyName: function(){
return this.name;
}
})();
</script>
</dom-module>
My another approach was to put the value inside an element but that did not work either.
Approach #2
<dom-module id="my-element">
<template>
<!-- value of name is rendered OK on the page -->
<p id="maxp">{{name}}</p>
</template>
<script>
(function() {
is: 'my-element',
properties: {
name: {type: String}
},
getMyName: function(){
var maxpValue = this.$$("#maxp").innerHTML;
return maxpValue;
}
})();
</script>
</dom-module>
How can I accomplish this? Please help.
Thanks in advance
Instead of using a self-invoking anonymous function, you should be using the Polymer function. Change (function() { ... })(); in your code to read Polymer({ ... });.
Here's an example:
<dom-module id="my-element">
<template>
...
</template>
</dom-module>
<script>
Polymer({
is: 'my-element',
properties: {
name: {
type: String
}
},
getMyName: function() {
return this.name;
}
});
</script>
I suggest that you follow the Getting Started guide from the Polymer documentation as it goes over all of this and more. It is a good starting point when you are looking to begin working with Polymer.
You could simple do
this.name
Anywhere in any function to access the variable