secretlm
secretlm

Reputation: 2361

About template in emberjs

 $.each(data, function () {
                var d = new Date(item.created_at.replace('+', 'UTC+'));
                var dd = d.format('mediumDate');
                var text = '';
                if (item.text != null)
                    text = item.text.linkify();
                html += text + '<br/>' + dd + '</div>';

            });

I want to make template (emberjs) for it. But I don't know how to handle property date (dd variable) in the template. How I can do that? Thanks.

Upvotes: 1

Views: 496

Answers (1)

Mike Aski
Mike Aski

Reputation: 9236

Assuming I correctly guessed missing parts, you've got a working sample here: http://jsfiddle.net/MikeAski/Pueqb/, which illustrates the case for a single item display.

Handlebars:

<script type="text/x-handlebars" >
    {{linkify text}}
    <br/>
    {{formatDate created_at}}
</script>

JavaScript:

Ember.Handlebars.registerHelper('linkify', function(path, options) {
    var text = this.get(path);
    return Ember.String.htmlSafe(linkify(text));
});

Ember.Handlebars.registerHelper('formatDate', function(path, options) {
    var rawDate = this.get(path);
    return new Date(rawDate).toLocaleString(); // or whatever format you need...
});

For an array of items, see: http://jsfiddle.net/MikeAski/Pueqb/11/ or http://jsfiddle.net/MikeAski/Pueqb/12/

Upvotes: 7

Related Questions