-
Notifications
You must be signed in to change notification settings - Fork 17
Should be able to <template repeat> on object values, Maps just like we do on array values. #11
Comments
Ping! @timoxley |
+1 |
…rties, due to PolymerExpression bugs: googlearchive/polymer-expressions#28 googlearchive/polymer-expressions#11
+1 |
2 similar comments
+1 |
+1 |
Update: I just found: http://www.polymer-project.org/docs/polymer/binding-types.html When using named scopes with the repeat attribute, the index value for each item in the array is also available by using the following syntax:
Polymer.dart has an |
+1 |
edited the subject to include ES6 Map as well, as noted in googlearchive/TemplateBinding#188 ... thanks for suggesting that @jyasskin |
+1 |
2 similar comments
+1 |
+1 |
+1 |
They ability to give the repeater a hint about what is new like in react.js would be nice as well. For example when a list of search results gets reordered it would not need to destroy a bunch of DOM but could just swap them around by a unique key Here is the example from react. Could this be translated to Polymer? render: function() {
var results = this.props.results;
return (
<ol>
{results.map(function(result) {
return <li key={result.id}>{result.text}</li>;
})}
</ol>
);
} When React reconciles the keyed children, it will ensure that any child with key will be reordered (instead of clobbered) or destroyed (instead of reused). |
@timcash TemplateBinding already does this, except that its notion of identity is ===. IOW, if you Array.sort() an array it will retain the DOM which corresponds to each array element. |
+1 |
1 similar comment
+1 |
fwiw, I'm not actively working on this, so unassigned. Someone eager should feel free to tackle. Happy to review a pull request for this feature. |
also happy to give pointers. I'm not super familiar with polymer-expressions but I can try and help interpret the code :) My guess is this feature is a lot like Overall it's probably very few lines of code, but figuring out where to put those lines will be the trick |
+1 |
@jmesserly I tried looking into polymer-expressions but I'm afraid I'm completely unfamiliar with Polymer source and I couldn't make any progress with it. However, I managed to make a custom filter (hack) that did the trick. PolymerExpressions.prototype.observedObjectToArray = function(object) {
var array = [];
if (_.isObject(object)) {
array.push.apply(array, _.map(object, function(value, key) {
return {'value': value, 'key': key};
}));
var observer = new ObjectObserver(object);
observer.open(function(added, removed, changed, getOldValueFn) {
_.each(added, function(value, key) {
array.push({'value': value, 'key': key});
});
_.each(removed, function(value, key) {
_.remove(array, {'key': key});
});
// TODO: observe change
}, this);
}
return array;
}; With this filter I was then able to: <template repeat="{{item, i in someObject | observedObjectToArray}}">
{{i}}, {{item.key}}, {{item.value}}
</template> Once I had this working, I was able to remove a lot of code from our code base. It made me realize how much complexity we generated due to inability to iterate on object properties in Polymer. If anyone can find time to implement object iteration in template, it would make a lot of people very happy. |
+1 please god |
Does adding +1 's really make a difference? But anyway, +1 from me. |
+1 vote |
👍 |
1 similar comment
👍 |
I solved the problem by using a small custom filter: filterKeys: function(object)
{
return Object.keys(object);
} Then use it in the template like this: <template repeat="{{key, index in someObject | filterKeys}}">
{{ index }}. {{ key }} = {{ someObject[key] }}
</template> |
+1 |
@manustays Does it still work after you deleted or modified a property of |
+1 |
We have +21 so far. @jmesserly @pflannery what's the status of PR #211? Looks like it's ready to merge. No? |
Regarding googlearchive/TemplateBinding#211. If the fix was to polymer-expressions, I'd feel okay merging it. I don't personally feel comfortable merging a new feature into TemplateBinding. This bug was moved to polymer-expressions, by the author of TemplateBinding. It's a lower level part of the system, and @rafaelw took care to keep it very minimal. polymer-expressions is where the higher level APIs live. Maybe someone on the Polymer team could comment if they think adding new features to TemplateBinding is okay (@kevinpschaaf maybe? I know you've done data binding stuff). If it is, then we're perhaps close to merge. The code changes looked okay (other than the |
This ensures the value is an Object (dictionary) and not a Date, String, Number or an Array. If someone wants to provide a better way of determining this then I can rectify but this works well otherwise. I'm not convinced polymer-expressions is the place to do this as we need to be able to determine if the value is to be repeated and if it's an Array or an Object (dictionary). When it's an Object (dictionary) it's converted to an array and observed for changes and when a change occurs it updates the array that the repeat is bound to. @kevinpschaaf do you think this syntax is to be carried over to 0.8? from what I see it looks like its being dropped and replaced with x-repeat? |
We don't want to introduce additional risk in to the 0.5 codebase, as it will be transitioning to maintenance mode soon. This is good feedback though, and the intent is to support a flavor of this in the 0.8+ codebase. Issue for that opened here: |
Moved from googlearchive/TemplateBinding#116
It would also be nice for both arrays and objects to be able to have an index value available for binding. For instance:
Demonstration of object repeat template not working:
The text was updated successfully, but these errors were encountered: