2016-03-25 27 views
2

node.js'yi kullanarak bir uygulama yapıyorum ve sunucu ve istemci tarafı için şablonlar için gidonu kullanıyorum. Ancak, bir nesne üzerinde yinelenen sorun yaşıyorum.Nesneler üzerinde yineleme yapmayan jp'ler

Benim .js dosyası::

$(function() { 
    var theTemplateScript = $("#update_address_distributor_template").html(); 
    context.city = 'london'; 
    var theTemplate = Handlebars.compile(theTemplateScript); 
    var theCompiledHtml = theTemplate(context); 
    $('.update_address_distributor_template_placeholder').html(theCompiledHtml); 
}); 

Benim html dosyası:

<script id="update_address_distributor_template" type="text/x-handlebars-template"> 
    <form class="form-horizontal" role="form" id="update_distributor_form"> 
     \{{city}} //correctly prints out london 
     \{{address_distributors.length}} //returns 2, so i know the object exists 
     {{#each address_distributors}} 
     \{{name}} //does not print anything 
     {{/each}} 
    </form> 
</script> 

Benim bağlam nesnesi:

{ 
    address_distributors: { 
     [ 

      { 
       name: 'nike', 
       id: '1' 
      }, 

      { 
       name: 'yokohama', 
       id: '4' 
      } 

     ] 
    }, 

    city: 'london' 

} 

şey yazmaz {{#each ../address_distributors}} ... {{/each}} Benim İşte benim kod. Html sayfasında \\{{address_distributors.length}} baskıları 2 olduğundan, addres_distributors nesnesinin çıkışını biliyorum. Ayrıca, gidon şablonumun düzgün bir şekilde kurulduğunu test etmek için context.city = 'london';'u yazdırıp basmadığını görmek için ayarladım. Bu yüzden, sorunun nesne ile bir ilgisi olduğunu biliyorum ...

Birisi yardımcı olabilir mi?

Şimdiden teşekkürler!

+0

''/'tuhaf bir şey değil mi? – Cohars

+0

onu çıkardım; Hala çalışmıyor .. –

+0

İsim/Nerede tanımlanmış? –

cevap

1

deneyebilirsiniz, ayrıca

{{this.name}} 

deneyin, bir aşağıda verilerle deneyin yaşadım aşağıdaki sonuç:

london //correctly prints out london 
2 //returns 2, so i know the object exists 
nike //does not print anything 
yokohama //does not print anything 

İşte bir keman o f testim: https://jsfiddle.net/ChristopheThiry/30xh7epu/

0

name değişken mevcut değil. Ben olsun

{ "address_distributors": [ { name: 'nike', id: '1' }, { name: 'yokohama', id: '4' } ], city: 'london' } 

: basitçe veri ile ilgili bir sorun var

{{#each address_distributors}} 
    Anything // Just to see if your loop works. 
    {{/each}} 
+0

Merhaba - 'hiçbir şey' işe yaramadı. Sorumlu bir örnek bağlam nesnesini –

+0

ile güncelledim. Bu nedenle, döngüsünüz bile çalışmıyor. Neden ilk başta '../' yaptın? – Cohars

İlgili konular