After checking core-ajax usage in the polymer website I decided to add ajax functionality using core-ajax inside my element/widget.
test-view.html
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/core-ajax/core-ajax.html">
<polymer-element name="test-view" attributes="url">
<core-ajax id="elemAjax" url="{{url}}" handleAs="json"></core-ajax>
<template>
<div id="body"></div>
</template>
<script>
Polymer('test-view', {
ready: function() {
var ajax = this.$.elemAjax; // this line
ajax.addEventListener('core-response', function() {
console.log(this.response);
});
}
});
</script>
</polymer-element>
Unfortunately, the "ajax" variable in my script returns "undefined". How can I load ajax inside an element using core-ajax?
Side Question: Is the "id" attribute inside a polymer element only accessible inside the polymer element?
After checking core-ajax usage in the polymer website I decided to add ajax functionality using core-ajax inside my element/widget.
test-view.html
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/core-ajax/core-ajax.html">
<polymer-element name="test-view" attributes="url">
<core-ajax id="elemAjax" url="{{url}}" handleAs="json"></core-ajax>
<template>
<div id="body"></div>
</template>
<script>
Polymer('test-view', {
ready: function() {
var ajax = this.$.elemAjax; // this line
ajax.addEventListener('core-response', function() {
console.log(this.response);
});
}
});
</script>
</polymer-element>
Unfortunately, the "ajax" variable in my script returns "undefined". How can I load ajax inside an element using core-ajax?
Side Question: Is the "id" attribute inside a polymer element only accessible inside the polymer element?
Share Improve this question asked Jun 23, 2014 at 6:11 GasimGasim 7,96114 gold badges70 silver badges144 bronze badges 1- 1 Re: the side question, essentially yes. Nodes created via a polymer template are created in ShadowDOM (by default), so they are not accessible to queries from outside that ShadowDOM. – Scott Miles Commented Jun 23, 2014 at 6:43
1 Answer
Reset to default 23Three main problems:
- The
core-ajax
must go inside the template, so it's part of each instance's DOM (this is whyajax
was undefined). - You need the
auto
attribute on thecore-ajax
(otherwise, you must call thego()
method on thecore-ajax
to send the request). - Your event handler uses
this
but is not bound to the element scope. Suggest you use event delegation instead ofaddEventListener
.
See the new example below. The other (less important) tweaks I made:
- Remove id from
core-ajax
, we don't need to reference it anymore. - Remove the
polymer.html
import,core-ajax
already imports it. - Remove the
test-view
parameter toPolymer()
, it's not necessary when defining an element inside an import.
Modified example:
<link rel="import" href="bower_components/core-ajax/core-ajax.html">
<polymer-element name="test-view" attributes="url">
<template>
<core-ajax auto url="{{url}}" handleAs="json" on-core-response="{{ajaxResponse}}"></core-ajax>
<div id="body"></div>
</template>
<script>
Polymer({
ajaxResponse: function(event, response) {
console.log(response);
}
});
</script>
</polymer-element>
Even better is to avoid events altogether and data-bind directly to the response data. Example:
<link rel="import" href="bower_components/core-ajax/core-ajax.html">
<polymer-element name="test-view" attributes="url">
<template>
<core-ajax auto url="{{url}}" handleAs="json" response="{{response}}"></core-ajax>
<div id="body"></div>
</template>
<script>
Polymer({
responseChanged: function(oldValue) {
console.log(this.response);
}
});
</script>
</polymer-element>