Using a spacebar variable as an object key in Blaze?

I’m not sure how to search for this, but I’ve tried a few ways and not found what I’m looking for, so if it’s been answered, I apologize, and just ask for a link to the answer.

I have a template where I’m iterating through a data object returned from my database.

myTypes: function() {
    return MyTypes.find({});
}

{{#each myTypes}}
    
{{/each}}

I then have a helper where I build an object to return (different from above, I swear).

objINeed: function() {
    for (i=0; i < countTypes; i++) {
          objVal = false;
          for (j=0; j < countOfKeys; j++) {
              console.log("Compare " + myTypes[i].myType + " to " + objType.subDocVals[j]);
              if (myTypes[i].myType == objType.subDocVals[j]) {
                  myObj[myTypes[i].myType] = true;
                  break;
              } else {
                  myObj[myTypes[i].myType] = false;
              }
          }
      }
      console.log(myObj);
      return myObj;
}

From this lovely little function, and the other, I’m dynamically building a list of checkboxes for the user based on other input they’ve provided.

What I’m doing is checking to see if the checkbox should be checked in ‘Edit’ mode in the application.

So, I compare the main list of values to those in my sub-document, and if they match I mark them ‘true’, add them to my object, and break out of the inner loop so as not to overwrite it with a ‘false’. The outer loop continues to the next value and we go through it again. Once the object is complete, it is returned for use in the UI.

So, I have the part I showed above, with this in between. What I’m trying to do is use raix-handlebar helpers to see if the value for objINeed.myType is true or not. See below.

{{#each myTypes}}
    <input type="checkbox" class="myCheckboxes" id="{{myType}}" checked="{{#if $eq objINeed.myType true"}}checked{{/if}}" />
</label for="{{myType}}">{{myType}}</label>
{{/each}}

The idea being that if I check the value associated with objINeed.myType (where myType is actually a variable for a list of types from another collection - maybe ‘horse’, ‘dog’, ‘cow’) and it’s true, then the checkbox will be checked.

What I realize now, however, is that the object objINeed.myType is being taken literally as objINeed.myType instead of as the intended way I’m creating it as objINeed.horse, objINeed.dog, and objINeed.cow.

Is there a way in Blaze to tell it that the key portion of an object variable object.<key> is actually a variable itself, and the variable value should be substituted in each iteration?

As always, any help is greatly appreciated.

I was wrong - that Idea wasn’t great. Still need a way, if one exists to use Blaze as described above.

If you surround the input with a {{#with objINeed}} statement then the input context switches to the object and you can access it’s properties using this. and the property name:

{{#each myTypes}}
  {{#with objINeed}}
  <input type="checkbox" class="myCheckboxes" id="{{../myType}}" checked="{{#if $eq this.myType true}}checked{{/if}}" />
  {{/with}}
  </label for="{{myType}}">{{myType}}</label>
{{/each}}
1 Like

I appreciate it. I solved it a different way, but this will be helpful in the future! Thanks so much.