johnnymire
johnnymire

Reputation: 1103

Ember JS - Reference view inside linkTo

I have an Ember JS confusion. First of all I have to admit I'm a bit of an Ember newbie so apologies if this is obvious.

I have the following template:

<script type="text/x-handlebars" data-template-name="threadListItem">
    {{#linkTo show thread.receiverLogin}}
        <h4>{{thread.receiver.login}}</h4>
        <span>{{view.prettyTime}}</span>
    {{/linkTo}}
</script>

There's basically a linkTo helper with some elements inside of it.

The weird thing is the {{view.prettyTime}} doesn't display anything when it's inside the linkTo.

However, if I move the span outside the linkTo it finds the view variable ok and it works.

Is there anyway to get access to the view object inside the linkTo?

Upvotes: 1

Views: 170

Answers (2)

Mike Grassotti
Mike Grassotti

Reputation: 19050

Christopher is correct that you can access the prettyTime property with:

{{view.parentView.prettyTime}}

Since you're new to ember, it's maybe worth mentioning that in most cases it is best to avoid accessing properties of the view. Instead, consider adding a prettyTime property to your controller. The controller will be the default context for sub-views like the one created by the linkTo helper, so you'll be able to access it with just

{{prettyTime}}

Upvotes: 1

Christopher Swasey
Christopher Swasey

Reputation: 10552

linkTo creates its own view, so the view you want to get access to is the parentView of view.

{{view.parentView.prettyTime}}

Upvotes: 0

Related Questions