5
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 picks up an index.Hbs tab
- var temp compiles the Compute order to be rendered in index.Hbs
- Function addOrder(order) makes a append of the template within the index Hbs.
In theory, the Ajax requisition should take the Function addOrder(order) and insert into the index Hbs. along with the json that the Ajax url (url: '/api') also picks up. But on the screen only the Append of the template appears, without the content inside.
Below the code inside the 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, which the var orderTemplate = $('#template-html'). html(); handle 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: The ajax can take the content searched by the api and play at index.Hbs, but can not print on the screen.
Thank you so much for those who read and tried to help. I did my best to enter as much detail as possible.
Tried to do that :
<h4><span class="noedit nome">{{order.nome}}</span></h4>
?– Atila Silva
$orders.append(temp(order));
this variable$orders
here is not wrong?– Wees Smith
I have tried to do {{order.name}} friend
– Victor
The $Orders variable is this: var $Orders = $('#Orders'); It is responsible for picking a div within the index. And in addition the append will be placed exactly inside this variable. What could be wrong? In the rest of the code it works. To Delete, Update, etc.
– Victor