Discussion:
[polymer-dev] Polymer 2.0- Error while using Array of objects with dom-repeat
Naga Sai A
2017-07-17 03:08:51 UTC
Permalink
Hi

I am trying to loop through the array of objects using dom-repeat and
getting below error

Polymer::Attributes: couldn't decode Array as JSON: {{test}}

I tried changing attr type as Array and Object but no luck .

I found solution for similar error message here
- https://github.com/Polymer/polymer/issues/1693

But I am using the same solution suggested but getting same error again

Codepen- https://codepen.io/nagasai/pen/bRJvbK

HTML:

<head>
<base href=
"https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/"
<link rel="import" href="polymer/polymer.html">
<div class="toolbar">
</head>


<body>

<dom-module id="custom-form">
<template>
<style>
:host {
display: block;
}




</style>
<div>


<div>
<pre>[[attr]]</pre>
<template is="dom-repeat" items="{{attr}}">
<label>{{item.title}}</label>
<input type="{{item.type}}"
required="{{item.required}}"
value="{{item.value}}"
disabled="{{item.disabled}}"
size="{{item.size}}"
oninput="{{item.oninput}}"
on-click="{{item.onclick}}"
onchange="{{item.onchange}}"
onfocus="{{item.onfocus}}"
onkeydown="{{item.onkeydown}}"
onkeypress="{{item.onkeypress}}"
onkeyup="{{item.onkeyup}}()"
</template>
</div>


</div>




</template>




</dom-module>
<custom-form attr="{{test}}"></custom-form>
</body>

JS:

/**
* @customElement
* @polymer
*/


class CustomForm extends Polymer.Element {
static get is() {
return "custom-form";
}
static get properties() {
return {
attr: {
type: Array
}
};
}
}
customElements.define(CustomForm.is, CustomForm);


let test = [
{
element: "input",
type: "text",
title: "Name",
name: "name",
value: "",
size: "30",
class: "",
required: "required",
disabled: "",
onblur: "",
onchange: "",
onclick: "display()",
onfocus: "",
oninput: "",
onkeydown: "",
onkeypress: "",
onkeyup: "",
id: 0
},
{
element: "checkbox",
type: "checkbox",
title: "Checkbox",
name: "name",
value: "",
size: "30",
class: "",
required: "required",
disabled: "",
onchange: "",
onclick: "",
onselect: "",
id: 1
}
];


function display(){
alert("hi");
}

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/83dc1d6e-0692-40fd-8393-4e340969dc5c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
abhishek gupta
2017-07-17 06:46:43 UTC
Permalink
You are using polymer binding outside of dom-module. That will not work.
In order to test your element you can pass it an array like
<custom-form attr="[1,2,3,4,5]"></custom-form>



Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/30770f77-1579-4710-90db-5cb29869613b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Naga Sai A
2017-07-17 14:13:43 UTC
Permalink
Thanks Abhishek, my question is how to dom repeat array of objects?
You are using polymer binding outside of dom-module. That will not work.
In order to test your element you can pass it an array like
<custom-form attr="[1,2,3,4,5]"></custom-form>
Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/9e7560ae-59b6-4989-8f92-5c905a59fc6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
abhishek gupta
2017-07-18 15:24:35 UTC
Permalink
The error you are getting is not because you are not using dom-repeat
properly but because you are not passing an Array to it.

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/a3a56ab3-cfaa-49a5-bdab-b49e00f5a968%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Naga Sai A
2017-07-18 15:30:56 UTC
Permalink
Abhishek, I am using below array of objects , ideally it should work even
with array of object , every indexed position have an object

[
{
element: "input",
type: "text",
title: "Name",
name: "name",
value: "",
size: "30",
class: "",
required: "required",
disabled: "",
onblur: "",
onchange: "",
onclick: "display()",
onfocus: "",
oninput: "",
onkeydown: "",
onkeypress: "",
onkeyup: "",
id: 0
},
{
element: "checkbox",
type: "checkbox",
title: "Checkbox",
name: "name",
value: "",
size: "30",
class: "",
required: "required",
disabled: "",
onchange: "",
onclick: "",
onselect: "",
id: 1
}
];
Post by abhishek gupta
The error you are getting is not because you are not using dom-repeat
properly but because you are not passing an Array to it.
Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/95bc8856-cb8e-4401-9655-72793046f84c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
abhishek gupta
2017-07-19 06:59:30 UTC
Permalink
What value of attr is getting printed in pre tag?

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/29f0b7df-3a78-411e-81ce-733f458d94c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Naga Sai A
2017-07-19 14:57:09 UTC
Permalink
empty
Post by abhishek gupta
What value of attr is getting printed in pre tag?
Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/78169655-c71c-4ce4-8f13-b9a529d4465d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
abhishek gupta
2017-07-21 11:55:41 UTC
Permalink
try this

<custom-form attr='[{element: "input", type: "text", title: "Name", name:
"name", value: "", size: "30", class: "", required: "required", disabled:
"", onblur: "", onchange: "", onclick: "display()", onfocus: "", oninput:
"", onkeydown: "", onkeypress: "", onkeyup: "", id: 0 }, {element:
"checkbox", type: "checkbox", title: "Checkbox", name: "name", value: "",
size: "30", class: "", required: "required", disabled: "", onchange: "",
onclick: "", onselect: "", id: 1 }]' ></custom-form>


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/e454a644-b061-4734-b02e-750069df68b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Loading...