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

淺唱寂寞╮ 提交于 2019-12-24 17:14:21

问题


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' });

});

回答1:


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}}


来源:https://stackoverflow.com/questions/20590691/trying-to-figure-out-how-to-set-up-this-link-in-ember

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!