samedi 3 octobre 2015

Conditionals in templates with data fetched with getJSON

I'm trying to figure out how to do something incredibly simple: Use conditionals in templates with data fetched with getJSON. I need to show info based on the type key on the JSON hash. As an example, I've included a HBS file of what'd I'd like to do. What is the Ember Way™ to accomplish this?

Company route:

// routes/company.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(params) {
        return this.store.findRecord('company', params.company_slug);
    },
    setupController: function(controller, model) {
        this._super(controller, model);

        Ember.$.getJSON('/companies/%@/events.json'.fmt(model.get('slug'))).then(data => {
            controller.set('events', data);
        });
    }
});

Company show template with example of what I'd like to do:

<ul class="test">
    <li>lol</li>
    {{#each events as |event|}}
        <li class="{{event.type}}">
            {{event.title}}

            {{!-- This code will obviously not work: --}}
            {{#if event.type == 'company_event'}}
               show company event stuff
            {{/if}}
            {{#if event.type == 'user_event'}}
               show user event stuff
            {{/if}}

        </li>
    {{/each}}
</ul>

Example of JSON data:

[
    {
        title: "test test test",
        type: "company_event",
        date: "2015-10-14T00:00:00.000+02:00"
    }
]

Aucun commentaire:

Enregistrer un commentaire