2013-06-20 56 views
2

我對jsrender非常陌生。我想把當前對象設置爲jsrender for循環中的checkbox複選框。下面是我的代碼。如何在jsrender中獲取for循環中的當前對象

{{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}} 

我將值設置爲{{:#view}}來獲取當前對象。但是我得到[object object]。如果我在這裏做錯了,請糾正我。

回答

4

jsrender中的'#view'是當前數據對象。

通過編寫{{:#view}}您要顯示的對象,這將在瀏覽器中顯示爲[對象對象]

嘗試{{:#view.data.yourattribute} }

+1

Anirudh你好,我想全沒有對象的屬性。通過上面的代碼我完全想要用戶連接對象。 – Simha

+1

你想如何顯示一個完整的對象作爲複選框的值?您可以將對象轉換爲字符串或布爾值,或者獲取對象的布爾值或字符串值屬性。否則,結果將是字符串轉換,如[object Object]。 {{:myAttribute}}或{{:〜myconverterhelper(#data)}}或{{myconverter:#data}}都是可能的。 #數據等同於#view.data。 – BorisMoore

0

渲染當前對象:

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

我有一個要求,呈現在一個循環中當前對象的動態命名屬性。爲此,您可以創建一個函數,在您的視圖模型(對象)中設置當前實例,並通過調用模型中的另一個函數來獲取當前實例的動態命名屬性的值。請看我的小提琴JsRender Dynamically Named Props。 靈感: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> 

這裏是Java腳本:

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 
}); 
相關問題