在 Emberjs 中使用不同的模型从一条路线过渡到另一条路线 [英] Transition from one route to another with a different model in Emberjs

查看:22
本文介绍了在 Emberjs 中使用不同的模型从一条路线过渡到另一条路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索页面,我们可以在其中获得不同类型的搜索结果.在我想使用的搜索结果列表中

I have a search page where we are getting different types of search results. In the list of search results I would like to use

{{#linkTo 'someResources.someResource' result}}{{result.Name}}{{/linkTo}}

在路线 someResources.someResource 上,我想使用与搜索页面完全不同的模型.我怎么做?当我单击 linkTo 的链接时,它不会再次加载模型,而是尝试在此处使用名为 result 的模型.

And on the route someResources.someResource I want to use a totally different model than on the search page. How do I do that? When I click on the link for the linkTo it doesn't load the model again, instead it tries to use the model named result here.

所以我想做的是当我根据 result 中的值导航到 someResources.someResource 时重新加载模型.

So what I would like to do is to reload the model when I navigate to someResources.someResource based on the values in result.

我确实有一个名为 App.SomeResource 的模型和一个 find 方法,如果我直接转到该页面,该方法就可以工作.

The I do have a model named App.SomeResource and a find method for it that works if I go directly to that page.

推荐答案

因为问题是我想要在使用 linkTo 进行转换时完全重新加载模型将不起作用,因为那是使用给它的模型.这个问题的解决方法其实很简单,只需要使用一个普通的html a-tag 来代替.我最终做的是这样的:

Since the problem is that I want a full reload of the model when doing the transition using linkTo won't work since that is using the model given to it. The solution to the problem is actually quite simple, just use a regular html a-tag instead. What I ended up doing was this:

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

属性 somePropertyInYourModel 是一个包含新页面 url 的属性.如果 url 在 ember 路由中,就像您在地址栏中键入该地址并按 Enter 键一样,但不会完全重新加载页面.

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.

我认为这是可以在 ember 中改进的东西,如果我能写这样的东西会更好:

I think this is something that could be improved in ember, it would be much nicer if I could write something like:

{{#linkToRoute "resourceA.routeB" params="val1,val2,val3"}}Go here{{/linkToRoute}}

鉴于我设置了此路线:

App.Router.map(function() {
    this.resource("resourceA", {{path: "/resourceA"}}, function() {
        this.route("routeB", {{path: "/:prop1/:prop2/:prop3");
    }
});

我想得到:

<a href="#/resourceA/val1/val2/val3">Go here</a>

val1,val2,val3 的顺序很重要,如果顺序更改,它们也应该在最终 url 中更改.

The order of the val1,val2,val3 matters, if the order is changed they should also be changed in the final url.

这篇关于在 Emberjs 中使用不同的模型从一条路线过渡到另一条路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆