user2168081
user2168081

Reputation:

How to get the current object in for loop in JsRender

I want the current object which is set as value to checkbox in for loop of JsRender. Below is my code.

{{for data.UserConnection}}
  <li>
    <input name="checkboxlist" class="profilechkbox" type="checkbox" value={{:#view}} /><br />
    <a onclick="stateManager.renderSelectedProfileForInviteGroup({{:#index}})" title="{{:Fullname}}" class="active" href="#" >
      <img  src="{{:userDetails.ImagePath}}" width="40" height="40" /><br />
      {{:Fullname}}
    </a>
  </li>
{{/for}}

I set the value as {{:#view}} to get the current object. But I am getting [object object]. Please correct me if I am doing wrong here.

Upvotes: 1

Views: 3018

Answers (3)

Anirudh
Anirudh

Reputation: 125

#view in JsRender is the current data object.

By writing {{:#view}} you are trying to display the object, which would appear as [object Object] in the browser.

Try {{:#view.data.yourattribute}}.

Upvotes: 3

Kishore
Kishore

Reputation: 11

I had a requirement to render dynamically named properties of the current object in a loop. For this you can create a function to set current instance in your view model(object) and fetch a value of dynamically named property of the current instance by calling another function in your model. Please see my fiddle JsRender Dynamically Named Props. Inspiration: John Papa's fiddle.

<tbody>
    {{for markets}}  
    {{:~SetInstanceFunc()}}
    <tr>
        <td>{{:id}}</td> <td>{{:market}}</td>
        {{for #parent.parent.data.years}}      
        <td>{{:~GetYearPropFunc('min', #data)}} </td>
        <td>{{:~GetYearPropFunc('max', #data)}} </td>
        {{/for}}
    </tr>
    {{/for}}
</tbody>

Here is java script:

var vm = {
  years: years, 
  markets:markets,
  instance:{},
  setInstance: function(){
    instance = this.data;
  },
  getYearProp: function(text, year){
    if(!instance) return '';
    return instance[(text + year)] || '';
    }
};
$.views.helpers({
  GetYearPropFunc: vm.getYearProp,
  SetInstanceFunc: vm.setInstance
});

Upvotes: 1

oll
oll

Reputation: 251

To render the current object:

{{for #data }} 
    {{:attribute1}}
    {{:attribute2}}
    ...
{{/for}}

Upvotes: 0

Related Questions