-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow object iteration #115
Comments
Apparently, Handlebars has support for this already, via an 'each' tag (http://stackoverflow.com/questions/9058774/handlebars-mustache-is-there-a-built-in-way-to-loop-through-the-properties-of):
But I'm not a fan of that syntax. |
This one has been on my radar for a while, I've had the same need from time to time - just struggled to figure out the best way to approach it. It's actually come up before - #24 - and I suspect it will come up again, particularly since it's quite common to get data from a back end structured in this form rather than as an array. So it's probably time to revisit it. It gets tricky because there's no canonical way to sort object keys - different browsers do it differently (see this Stack Overflow answer and weep). And there's the question of what to do when new keys are added - do they get added to the bottom, or do they get inserted in the middle according to some sorting logic? And does there need to be a mechanism for specifying custom sort logic, rather than (for example) just sorting keys alphabetically? There are some cases (particularly with SVG dataviz, for example) where sort order doesn't really matter. So maybe the right approach is to implement it without worrying too much about sorting, and see from there what the limitations are. We agree on the syntax - I think that's the right way to approach it. |
I think the easiest way is not to worry about sorting. If you want to sort restructure your object to an array with properties key & value |
Okay, that's in. You can now do this sort of thing: <ul>
{{#countries:code}}
<li>{{code}}: {{name}}</li>
{{/countries}}
</ul> ractive = new Ractive({
el: container,
template: template,
data: {
countries: {
GBR: { name: 'United Kingdom' },
USA: { name: 'United States' },
FRA: { name: 'France' }
}
}
});
// would render
// <ul>
// <li>GBR: United Kingdom</li>
// <li>USA: United States</li>
// <li>FRA: France</li>
// </ul> Then of course you can do It makes no attempt to maintain any sort order - items will initially be added in whatever order Two ways to remove items: delete ractive.get( 'countries' ).GBR;
ractive.update();
ractive.set( 'countries.GBR', undefined ); I did wonder about (where necessary) adding countries.add( 'CHN', { name: 'China' });
countries.add({
SWE: { name: 'Sweden' },
GTM: { name: 'Guatemala' }
});
countries.remove( 'FRA' ); ...in lieu of One final note: there is a theoretical performance penalty to doing list sections based on objects - every time the object (or one of its properties, or sub-properties) changes, Ractive needs to do a |
I'd avoid adding The behaviors of
|
Closing this issue. Quite like your proposed sorting solution @gliese1337 but I haven't found it necessary to implement yet, and no-one else has asked for it, so will put it in the YAGNI pile for now |
Object iteration seems to have stopped working: http://jsbin.com/uZavecob/3/edit?html,output (example from the docs http://docs.ractivejs.org/latest/mustaches) :( |
It doesn't work because of |
Ah, whoops. Thanks for spotting it @der-david and identifying the problem @MartinKolarik. I'll leave this issue closed in favour of #396. |
@Rich-Harris Please add methods |
👍 Found myself need to delete a key and resort to this:
|
@Rich-Harris Any plans for adding a method to |
(correct me if I should make another issue) I see elsewhere (#1474) that "objects are first class citizens" and we can iterate objects in a couple different ways:
But what if you want two-way binding on the key? ex) turn a dictionary/map into a table so you can edit both key/value. |
@zaus' comment led me back to this (long-since-abandoned!) issue - @svapreddy the |
I said (https://github.com/Rich-Harris/Ractive/issues/110#issuecomment-22333513) that I couldn't think of a reason to need to add properties to a Ractive model that aren't referenced in the template, but then just today I ran into one: dealing with a model that contains a key-value map rather than a regular array.
This is kind of a pie-in-the-sky feature request (it's fairly simply worked around, and it would make magic setter methods per #110 harder to implement in the absence of Object.observe or Proxy objects), but it would be kind of neat to have something like
that would automatically iterate over object properties.
The text was updated successfully, but these errors were encountered: