vendredi 2 janvier 2015

Ember - Render products through different templates based on a value in the array

I have simplified my app for the purposes of this question. I am just starting to get my head around Ember, and I am not yet doing things as efficiently as it can be done, I am sure.


I am looking for a way to use a different template for entries with certain characteristics. Let me explain:


I have a nested route as follows:



App.Router.map(function(){
this.resource('products', function() {
this.resource('product', { path: '/:product_id' });
});
});


and a JSON file like this:



{"product": [
{
"id": 1,
"title": "Bicycle",
"description": "A nice red bicycle that you can ride on.",
"display": "textonly"
},
{
"id": 2,
"title": "Bus",
"description": "A big bus that many people can ride on."
"display": "bigblocks"
},
{
"id": 3,
"title": "Airplane",
"description": "You can fly in this to get to your destination faster",
"display": "textonly"
}
]
}


As you can see, I have added a property to my JSON that determines how the product entries should be displayed.


Now, my html templates looks like this:



<script type='text/x-handlebars' data-template-name='products'>
<div class='row'>
<div class='col-md-3'>
<div class='list-group'>
{{#each}}
{{#link-to 'product' this classNames='list-group-item'}}
{{title}}
{{/link-to}}
{{/each}}
</div>
</div>
<div class='col-sm-9'>
{{outlet}}
</div>
</div>
</script>

<script type='text/x-handlebars' data-template-name='product'>
<div class ='row'>
<div class ='col-md-9'>
<h4>{{title}}</h4>
<p>{{description}}</p>
</div>
</div>
</script>


My app currently renders like this:


enter image description here


enter image description here


Now, what I want to do, is render some products through a different template (or view or component or whatever is the best for the job), so that when "display": "bigblocks" the product is rendered slightly different than for when "display": "textonly" like so:


enter image description here


So in effect, I want to have many JSON entries, and render them differently based on their 'display' value in the JSON file.


I am just looking for a very simple way to do this for now, maybe an IF statement or something similar, just so I can get my head around how this could be done.


Thanks alot, and please ask for more info if required :)





Aucun commentaire:

Enregistrer un commentaire