Alex Tonkonozhenko
Alex Tonkonozhenko

Reputation: 1574

Ember conditions in views

I have a simple blog in which I learn Ember.
Now I have a controller

App.IndexController = Ember.ArrayController.extend({
  sortProperties: ['originalId:desc']
  sortedPosts: Ember.computed.sort('model', 'sortProperties'),
})

So in template I show all posts. For this I want to show edit link near all of my posts.
So question is how to do this in handlebars.

I want to do something like this:

{{#each post in sortedPosts}}
  <h1>{{link-to post.title 'post' post }}</h1>

  {{#if session.isAuthenticated and post.ownedBy(session.user)}} <!--This place doesn't work-->
    {{link-to 'Edit' 'post.edit' post }}
  {{/if}}

  {{{post.text}}}
  <hr/>
{{/each}}

I found this question Logical operator in a handlebars.js {{#if}} conditional but I hope that there is better solution.

P.S.: There is one more question about sorting with SortableMixin. It doesn't reload templates when sortProperties is changed. So I have to create one more property sortedPosts. Maybe someone knows why it doesn't work?

Upvotes: 1

Views: 54

Answers (1)

Kalman
Kalman

Reputation: 8131

You will need to create a computed property in the controller and then use it in the view. If you have 2 computed properties and want to combine them into a 3rd - you can use computed.and (see here)

Upvotes: 1

Related Questions