Quick information on how this code should work: Ajax takes an html, compiles with Handlebars, the same ajax takes a json from an api, packages everything and renders an index with an append.
- var orderTemplate takes a template from index.hbs
- var temp compiles the orderTemplate to be rendered in index.hbs
- function addOrder (order) makes a append template within index.hbs
Below the code inside main.js:
var orderTemplate = $('#template-html').html();
var temp = Handlebars.compile(orderTemplate);
function addOrder(order) {
$orders.append(temp(order));
}
$.ajax({
type: 'GET',
url: '/api',
success: function(orders){
$.each(orders, function(i, order){
addOrder(order);
});
},
error: function(){
alert('Erro ao listar :(');
}
});
Below is the template, that the var orderTemplate = $ ('# template-html'). html (); paste via id. I use Handlebars to render.
<script type="text/x-handlebars-template" id="template-html">
<div id='conteudo' class='row' data-id='{{_id}}'>
<h4><span class="noedit nome">{{nome}}</span></h4>
<p><span class="noedit nome">{{bebida}}</span></p>
</div>
</script>
Below the Append photo. Note: Ajax can get the content searched by the api and play in index.hbs, but can not print on the screen.
- Belowisapictureofhowthefinalresultshouldbe!
Thank you very much for who you read and tried to help. I've done my best to enter as much detail as possible.