简体   繁体   English

如何维护页面状态,以便您可以使用emberjs提供永久链接?

[英]How do you maintain the page state, so that you can provide permalinks using emberjs?

I can't to get a good idea of how you support permalinks. 我不能很好地了解你如何支持永久链接。 This is perhaps simply because emberjs doesn't support permalinks. 这可能仅仅是因为emberjs不支持永久链接。

I'm building a UI that allows users to choose certain reports, so in ember this is easy enough you just add a route 'reports' that takes in an id so the URL looks like this: 我正在构建一个允许用户选择某些报告的UI,所以在ember中这很简单,你只需要添加一个带有id的路由“报告”,这样URL就像这样:

#/reports/10/

What I need to have is some extra parameters to also be present in that URL for example start and end dates and metric types eg 我需要的是一些额外的参数也存在于该URL中,例如开始和结束日期以及度量标准类型

#/reports/10/metric/impressions/start/10-08-2013/end/11-08-2013

So those parameters need to be picked up if I paste them in the browser, AND importantly when the user changes those settings eg via a date picker the URL should update to reflect the new parameters. 因此,如果我将它们粘贴到浏览器中,则需要拾取这些参数,重要的是当用户更改这些设置时,例如通过日期选择器,URL应该更新以反映新参数。

Edit: 编辑:

Here is a link to a jsbin with a solution based on the answer below. 以下是基于以下答案的jsbin链接和解决方案。 http://jsbin.com/ucanam/703 http://jsbin.com/ucanam/703

Just throwing my 2 cents into this topic. 把我的2美分投入到这个主题中。 Please note that i am using this approach in production and it works fine. 请注意, 我在生产中使用这种方法 ,它工作正常。 Actually there are 2 parts to this question. 实际上这个问题有2个部分。

1. How can i have multiple dynamic segments? 1.我如何拥有多个动态细分?

One approach is described by Mike Grasotti using nested resources. Mike Grasotti使用嵌套资源描述了一种方法。 This approach works but i think this approach is a little bit cumbersome in this case. 这种方法有效,但我认为这种方法在这种情况下有点麻烦。

Why do i think it is cumbersome? 为什么我认为这很麻烦?

Routes are a means to separate concerns in Ember. 路线是分离Ember关注点的一种手段。 In this case i do not see separate concerns. 在这种情况下,我没有看到单独的担忧。 It looks to me like you are trying to mirror the state of a form in your URL. 在我看来,您正在尝试镜像URL中表单的状态。 I think it should be one route that is responsible for the concern "state of the form". 我认为它应该是一个负责关注“形式状态”的途径。 Therefore i recommend to have a look at the following post, in which i describe how to implement multiple dynamic parameters per Route: Is resource nesting the only way to enable multiple dynamic segments? 因此,我建议查看以下帖子,其中我描述了如何为每个路由实现多个动态参数: 资源嵌套是启用多个动态段的唯一方法吗?

2. How is it possible to trigger the serialize hook to update the URL, when you have changed just one parameter in your form? 2.当您在表单中只更改了一个参数时,如何触发序列化挂钩来更新URL?

The problem is that the serialize hook is only triggered, when the Route gets entered with a new model. 问题是只有在使用新模型输入Route时才会触发serialize hook I guess you have some logic in place, that deals with the event of changing the parameters start or end . 我猜你有一些逻辑,处理更改参数startend的事件。 I suppose that you do not re enter the Route in this case. 我想你不会在这种情况下重新进入路线。 So how do you trigger the serialize hook in this case again to update the URL? 那么如何在这种情况下再次触发序列化挂钩以更新URL? I am handling a event like this in my router and there i am using the following code: 我在我的路由器中处理这样的事件,我使用以下代码:

var currentRouteName = this.controllerFor("application").get("currentPath");//the ApplicationController stores the name of the current Route
var url = App.Router.router.generate(currentRouteName);
App.Router.router.updateURL(url);

PS: You can have a look at my production app here . PS: 您可以在这里查看我的制作应用程序 This app shows the best movies in cinemas in Germany. 这个应用程序显示在德国电影院最好的电影。 Even if you do not know german, you can click on one of the controls in the top area and see the URL getting updated. 即使您不懂德语,也可以单击顶部区域中的某个控件,然后查看更新的URL。 I guess this is pretty much the same you want? 我想这几乎和你想要的一样?

I have wondered how to do this as well. 我也想知道如何做到这一点。 The data has to go in the URI since we want to share links, but you don't want to confuse the application location with the application state. 由于我们想要共享链接,因此数据必须包含在URI中,但您不希望将应用程序位置与应用程序状态混淆。

The hash: #/reports/10 would be the minimal information required to tell the application where to go. 哈希: #/reports/10将是告诉应用程序去哪里所需的最少信息。 All the other data which is independent of location should probably go in the search portion of the URI. 所有其他独立于位置的数据应该可以放在URI的搜索部分。 I would suggest something like this: 我会建议这样的事情:

#/reports?metrics=impressions&start=10-08-2013&end=11-08-2013

In theory you could parse the query string when you enter a route and then update your model accordingly. 理论上,您可以在输入路径时解析查询字符串,然后相应地更新模型。 From my understanding the route's model() function is called when navigating to a route by changing the URL directly or clicking a link so that would be the place. 根据我的理解,当通过直接更改URL或单击链接以导航到路径时,将调用路径的model()函数。

Unfortunately, in practice this didn't work as I expected. 不幸的是,在实践中,这并不像我预期的那样有效。 I'm not sure if it's just JSBin, but there is some weird behavior where the link with the extra application data doesn't actually navigate which is the whole point for a permalink. 我不确定它是否只是JSBin,但是有一些奇怪的行为,其中带有额外应用程序数据的链接实际上没有导航,这是永久链接的全部要点。 Notice that if you follow the directions in the JSBin the start and end dates are taken from the url instead of the default values. 请注意,如果您按照JSBin中的说明操作,则会从网址而不是默认值中获取开始日期和结束日期。 This concept could be extended to send extra requests for different model data using findQuery etc so almost any thing is possible. 这个概念可以扩展为使用findQuery等发送对不同模型数据的额外请求,因此几乎任何事情都是可能的。

http://jsbin.com/abadet/7 http://jsbin.com/abadet/7

Anyways, it might give you some ideas. 无论如何,它可能会给你一些想法。

There are a couple of ways to get this done in ember. 有几种方法可以在ember中完成。 If you need a lot of flexibility for misc parameters that might be passed to a report, check out ember-query which adds query-string support to ember applications. 如果您需要为可能传递给报表的misc参数提供很大的灵活性,请查看ember-query ,它为ember应用程序添加了查询字符串支持。

Another option is to use nested resources. 另一种选择是使用嵌套资源。 So for example: 例如:

App = Ember.Application.create({});

App.Router.map(function() {
  this.resource('report', {path: '/reports/:report'}, function() {
    this.resource('metric', {path: '/:metric'}, function() {
      this.resource('start', {path: '/:start'}, function() {
        this.route('end', {path: '/:end'});
      });
    });

  });
});

App.StartEndRoute = Ember.Route.extend({
  model: function(params, transition){
    return transition.params
  }
});

<script type="text/x-handlebars" data-template-name="start/end">
<pre>
Report ID: {{report}}
metric: {{metric}}
start: {{start}}
end: {{end}}
{{log this}}
</pre>
</script>

See this jsbin for working example 有关工作示例,请参阅此jsbin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 结合使用emberjs和turbolinks,可以介绍使用方法吗? - Using emberjs with turbolinks, Can you introduce how to use? 如何在iframe中维护角度应用的页面重新加载路线? - How do you maintain the route on page reload for an angular app in an iframe? 每当您使用javascript或jsf返回页面时,如何在jsp页面上维护复选框状态而不将其存储到数据库中 - how to maintain check boxes state across jsp pages whenever you come back on a page using javascript or jsf without storing it into database 可以,如果可以,如何获取默认光标作为图像? - Can you, and if so how do you get the default cursor as an Image? 您如何命名一个函数,以便您可以对其进行自省? - How do you name a function so that you can introspect it? 如何将状态作为道具传递,以便您可以对其进行修改 - How to pass state as props so you can modify them 如何使用$ provide和$ q对工厂进行单元测试 - How do you use unit test a factory using $provide and $q 悬停时如何保持元素宽度? - How do you maintain element width on hover? 你如何“拦截”EmberJs应用程序中的所有HTTP请求? - How do you 'intercept' all HTTP requests in an EmberJs app? 如何在V8 Javascript引擎中公开C ++类,以便可以使用new创建它? - How do you expose a C++ class in the V8 Javascript Engine so it can be created using new?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM