I’m having some difficulties when using the data binding system to keep two arrays with a one-to-many relationship in sync.
The basic app idea is that I need to edit photos. Each photo may be present inside many slots.
Objective
Given two array of models: [Photo 1, Photo 2, ...] and [Slot 1, Slot 2, ...].
With the relationship: A Photo belongs to many Slots.
photos = [{id: 1}, {id: 2}, ...]
slots = [{photoId: 1}, {photoId: 1}, {photoId: 2}, ...]
I want propagate changes from the Photo rendered inside a Slot.
Question
How do i propagate changes from a model (that will be rendered inside a slot and selected by the slot.photoId) to the photo inside the original photos array?
Relevant Code
<template items="{{spread.slots}}" as="slot" is="dom-repeat">
<paper-input value="binding for photo.width selected from [[slot.photoId]]"> </paper-input>
</template>
Possibilities Considered
- Change the data model (to which one?)
- Use array-selector (how?)
- Manually store and compute association outside binding system (using this solution for now)
The same problem is described in the snippet:
Polymer({
is: 'demo-element',
properties: {
project: {
type: Object,
value: () => {
return {
photos: [
{id: 1, src: 'http://lorempixel.com/200/100/cats/1', width: 200 },
{id: 2, src: 'http://lorempixel.com/200/100/cats/2', width: 100}
],
spreads: [{
slots: [{
number: 1,
photoId: 1
},{
number: 2,
photoId: 1
}, {
number: 3,
photoId: 2
}]
}]
};
}
}
}
});
<!DOCTYPE html><html><head>
<script src="http://static.jsbin.com/js/vendor/traceur.js"></script>
<meta charset="utf-8">
<meta description="Polymer 1.0 Template">
<base href="http://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html" />
<link rel="import" href="paper-input/paper-input.html" />
<link rel="import" href="paper-material/paper-material.html" />
<style> paper-material {padding: 1em;}</style>
</head>
<body>
<demo-element></demo-element>
<dom-module id="demo-element">
<template>
<paper-material>
<h2>2 Photos</h2>
<template is="dom-repeat" items="{{project.photos}}" as="photo">
<span>
Photo [[photo.id]]
<img src$="{{photo.src}}"
style="width: [[photo.width]]px;" />
</span>
</template></paper-material>
<br />
<paper-material>
<template items="{{project.spreads}}" as="spread" is="dom-repeat">
<h2>1 Spread </h2>
<h4>3 Slots. Photo 1 used twice, Photo 2 used once. Need to edit photo.width!</h4>
<template items="{{spread.slots}}" as="slot" is="dom-repeat">
<div>
<p>
The slot {{slot.number}}
has photoId: {{slot.photoId}} <br /><br />
How to data bind photo.width for the photo of this slot?
<paper-input label="binding for width property of photo of slot [[slot.number]] with id [[slot.photoId]]"></paper-input>
</p>
</div>
</template>
</template></paper-material>
</template>
</dom-module>
</body>
</html>