timpone
timpone

Reputation: 19979

trying to figure out how to set up this link in Ember

I'm looking at Ember to see whether it is suitable. One issue that came up is that we have many 'narrow' api calls - these calls return a list with the minimal data to create a list and then the user clicks on a link which goes to the detail view. Due to how link-to helper works, this will bypass the model method in the route. This question has the same issue: Transition from one route to another with a different model in Emberjs But I honestly don't understand the answer he provided. Specifically, he provides this code:

<a {{bindAttr href="somePropertyInYourModel"}}>{{someTextProperty}}</a>

and says:

The property somePropertyInYourModel is a property containing the url to the new page. If the url is in the ember routes it will be as if you where typing that address in the address bar and pressing enter, but without the full reload of the page.

I don't really understand what he's saying (my fault on this). I tried putting in <a {{bindAttr href="{{post}}"}}>{{someTextProperty}}</a> and <a {{bindAttr href="{{post}}"}}>{{someTextProperty}}</a> but to no avail. Say I have this model:

Hex.Post = Ember.Object.extend({
    id: null,
    body: null,
    isEnabled: null,
    createdAt: null
});

How could I get this to work? What is he telling us to do?

thx for help, ember looks really cool but has a lot to know

edit #1

Here's the whole Router list. I want to have a posts view and when the user clicks, it goes to the post view which will be populated to the right. The problem is that the link-to bypasses the model so we really need to reload the model at that point. This would allow us to repurpose much of our existing api. Thx for help

Hex.Router.map(function() {
  // put your routes here
    this.resource('index', { path: '/' });
    this.resource('users', { path: 'users' });
    this.resource('loginslogouts', { path: 'loginslogouts' });
    this.resource('locations', { path: 'locations' });
    this.resource('flaggedcontent', { path: 'flaggedcontent' });
    this.resource('posts', function(){
        this.resource('post', { path: ':post_id' });
    });
    this.resource('comments', { path: 'comments' });

});

Upvotes: 1

Views: 53

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

ahhh, send the id instead of the model, that will retrigger the model hook. Sending a model to the hook makes ember think you have the model, sending an id tells ember to hit the model hook with that id.

{{#link-to 'post' post.id}}{{post.name}}{{/link-to}}

Upvotes: 1

Related Questions