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
{{#each}} now supports objects. #272
Conversation
Adding the On another note, I had originally tried storing the key reference as |
Currently with Handlebars, if an array is empty it will trigger the inverse of the block. This commit will break that, but otherwise it looks good. #238 (but this version doesn't provide the key) |
Good call, @brianreavis. Our pull requests started out pretty dissimilar, but now they are close. Should we talk about merging them into one, or let @wycats pick? |
Nah, I like yours! No reason to not go with it :) |
I just saw a recent commit that adds support for the |
This seems like a good idea. Lets get confirmation from @wycats if we can. |
@rosshadden you should use |
actually here is my proposition : if (Ember.isArray(context) && !Ember.isEmpty(context)) {
for (var j = context.length; i<j; i++) {
if (data) { data.index = i; }
ret = ret + fn(context[i], { data: data });
}
} else if (Ember.typeOf(context) === 'object') {
for (var key in context) {
if (context.hasOwnProperty(key)) {
if (data) { data.key = key; }
ret = ret + fn(context[key], {data: data});
i++;
}
}
} |
I agree with what you are saying, @tchak, however Handlebars is entirely isolated from Ember. Thus we do not have those functions at our disposal. Therefore there is a trade-off to make between either adding those functions in for this single use case or not being as thorough as possible in the type checking. |
Wow, sorry I thought it was an ember issue... My bad! (we have issues notifications in campfire for both, and some times I miss the origin :) ) |
For simple key-value object pairs, will For example, given this context:
Would this be the correct template syntax?
|
@mpetrovich: If memory serves, However, given a slightly modified example: var items = {
34594: {
letters: "abc",
numbers: 123
},
43982: {
letters: "def",
numbers: 456
},
20985: {
letters: "ghi",
numbers: 789
}
}; The following works as expected: {{#each items}}
{{@key}} = {{letters}} and {{numbers}}
{{@key}} = {{this.letters}} and {{this.numbers}}
{{/each}} where |
Can you please add a test? |
@rosshadden Any progress here? |
In the interest of getting this done and merged, it may be faster if someone else who knew what they were doing could add a test to it. Otherwise, I will begin doing so on Tuesday. |
I'll add tests tomorrow morning. |
In testing this, I realized that object properties don't have an implicit order in Javascript. Most browsers iterate over object properties in the order in which they were defined, whereas some (eg. Chrome and Opera) iterate over numeric (and numeric-like) properties (eg. 1, "2") before non-numeric properties (eg. "first", "foo"). More details here: http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop Given that object iteration order is undefined and inconsistent across browsers, should we move forward? If so, I'll complete the unit tests and we'll document that #each with objects shouldn't be used if order matters. @wycats, what do you think? |
Yeah this was the reason I have a different kind of |
Added unit tests for #each with objects
@wycats This PR has been updated with those tests. |
Don't worry, it still is just @rosshadden and @mpetrovich's commits.... I just rebased it so @wycats can pull it in. |
Merged @mikesherov's rebase |
Is there any particular stuff to do? coz it still doesn't work here when I use this code: https://gist.github.com/4130484 contrary to this one: https://gist.github.com/4130488 My code is pretty simple:
Do you have any idea? Thank you very much. |
@kud, have you tried doing it with the |
@wagenet It's ok, sorry to have disturbed you. In fact, I use bower, and as there's no precompiled version of handlebars in the official handlebars repo, I used this one: It was called as same version "1.0-rc1" but it seems to have still some bugs. So I've download yours Now it works like a charm. Thank you! |
Any pointers to an authoritative writeup of the issue with handlebars and {{each}} of objects? I see the problem, but only in some circumstances that seem totally unrelated (like the HTML outside the {{each}} helper......). |
As far as I know there isn't a problem with {{each}} on objects. I have been using it in production for months. If you are having issues try compiling handlebars.js from source. Or let me know / email me and I will send you the version I use so you do not have to compile (white elephant in the room: it's a bitch for a non-rails person to compile a project like this!). |
Sure I'll take the email : bretweinraub AT yahoo DOT com For instance this works:
But insert of a "td" tag before {{title}}, it stops! For example:
Literally that piece of before {{title}} shuts the whole thing down. Obviously I would've taken this to stackoverflow but actually this thread seems to be the best thing out there..... Merci beacoup from Switzerland! |
@bretweinraub Try making |
Hi thanks for the responses. I fixed the problem. The template was sitting in a "div"; and the browser ate my homework. Errr I mean it had randomly reorganized the text and was moved things around. It only does this with elements ... everything else works fine.Like this
There was a jQuery call $('#templ').html() that was extracting the template. MORAL of the story; don't do that. We have now stuck the template into a URI encoded string; which since a PHP server generated it, of cours we can't use decodeURI(). But it all works, I can send example of how to do it to anyone who cares. |
You could also put it in a |
genius! Where were you about 1000 keystrokes ago? |
The value of {{this}} appears as an empty string when iterating over an object in Safari 6 (6.0.2 - 8536.26.17 on OS X 10.8.2). It appears that "this" can still be passed to scripts and evaluated perfectly fine. To make the bug more bizarre, it behaves as expected when the Web Inspector is open. It also works as expected in Google Chrome. I haven't tested other browsers. |
@dak1 I've noticed a related issue that has the same symptoms (Safari 6 Mac, but only if dev tools are not open), where a template with 3 placeholders renders fine, but add a fourth and nothing renders. I'll try to create a reduced test case. |
At the jQuery conference a few days ago, @wycats and I discussed adding a for-in loop to base.js to support loops over objects. After giving it some thought, I decided it would be a better idea to just extend the existing {{#each}} helper, so as not to saturate the project with too many existing helpers.
Given the template:
and the data object:
The following will be rendered:
*Note: We had decided to use@key
for the magic key reference, and I still want this to be the case. However, for now it is '_key' because I could not figure out the former.Please let me know what you think.