0

I am using handlebars.runtime-v1.3.0 and I am not sure how to access parent object value in #each. Here is my code:

data object:

var data = {
    data : {
        question : "work",
        sub : "pref",
        value : [0,1,2]
    }
};

Template:

{{#each data.data.value}}
    <div class="option" data-value="{{this}}" data-question="{{../question}}" data-subcat="{{../sub}}">
        <img src="/images/image.png" alt="" class="image" />
        <span class="style-name"></span>
    </div>
{{/each}}

I am not able to get the values at {{../question}} and {{../sub}}.

I have been searching for the answer for 1 day and I found these links: link1, link2. None of them is working for me, so please don't mark the question duplicate on the basis of this.

Community
  • 1
  • 1
Bharat Soni
  • 2,824
  • 6
  • 23
  • 48
  • Are you precompiling your templates, such as with grunt? – gfullam Nov 09 '15 at 16:51
  • @gfullam yes, I am using gulp for that with gulp-handlebars plugin v2.0.0 – Bharat Soni Nov 09 '15 at 18:12
  • So your runtime and compiler versions appear different. This may be the cause of your problem. – gfullam Nov 09 '15 at 18:19
  • Well, I suppose the gulp plugin version may not correlate. What version of Handlebars do you have [defined in your packages.json](https://github.com/lazd/gulp-handlebars#compiling-using-a-specific-handlebars-version)? – gfullam Nov 09 '15 at 18:23
  • @gfullam everything is working... I have made full webapp and it is working pretty fine. Just this accessing parent object isn't working.. I have made one more app with same configs and things are working pretty fine over there.. entry in package.json is "gulp-handlebars": "2.0.0" – Bharat Soni Nov 09 '15 at 19:22
  • @gfullam I have tried to change the version, still not working. currently "gulp-handlebars": "^1.0.0" in my package.json. – Bharat Soni Nov 09 '15 at 19:46
  • You should find something like this in packages.json: `"handlebars": "^1.3.0"` What version number is here? If none is specified, [try specifying the same version](https://github.com/lazd/gulp-handlebars#compiling-using-a-specific-handlebars-version) as your runtime compiler. Handlebars has changed the meaning of `../` since v1.3.0, (see [Compatibility notes](https://github.com/wycats/handlebars.js/blob/master/release-notes.md#v400---september-1st-2015)), so having matching versions for precompilation and runtime is important. – gfullam Nov 09 '15 at 20:03

2 Answers2

0

The foreach loop is for data.data.value not data.data

try

{{data.data.question}}

or

{{data.question}}

additionally is you loop working fine? i cant see the rest of your code but i would guess that the foreach should be on data.value rather than data.data.value

Shujaat
  • 691
  • 4
  • 18
  • `data.data.question` or `data.question` won't work because currently control is at `data.data.value`. Look is working fine at `data.data.value`, it should be `data.data.value`. – Bharat Soni Nov 16 '15 at 06:00
0

I used {{../data.data.question}} and it works fine now :)

Bharat Soni
  • 2,824
  • 6
  • 23
  • 48