资源嵌套是启用多个动态段的唯一方法吗? [英] Is resource nesting the only way to enable multiple dynamic segments?

查看:78
本文介绍了资源嵌套是启用多个动态段的唯一方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎表明答案是肯定的:



从Ember Pre1到Pre4:每路由多个动态段?更新:动态段的允许语法是什么?



...但我只想确认。



在我的情况下,作为一个学习练习,我正在Ember中建立一个日历,每月显示。我需要能够从一个特定的月份到上个月和下一个月的链接。



所以我想要能够

  {{linkto calendar_month year month}} 

  this.transitionTo('calendarMonth',year,month)

想知道如果没有使用嵌套资源,这是否可行。我可以使用以下工具:

  App.Router.map(function(){
this.resource (year,{path:calendar /:year},function(){
this.resource(calendar_month,{path:/:month},function(){
this.route('index');
});
});
});

...但这涉及到从建模角度介绍可能不需要存在的Year对象,所以我可以在linkTo中使用它的id



我宁愿设置一个包含两个参数/动态段的路由:

  App.Router.map(function(){
this.route('calendar_month',{path:'calendar /:year /:month'} );
});

但是我是否正确,这是不可能的?我只是想确保我这样做最干净,最卑鄙的方式。



换句话说:



我理解这个概念,如果你的用户界面是嵌套的,那么你的路由应该是嵌套的,但是如果我的url是嵌套的,这并不一定意味着我的接口也是嵌套的。所以我想知道:如果我的网址是嵌套的,建立相应的嵌套模型总是最好的做法?



非常感谢任何指导/澄清。 b
$ b

谢谢,

解决方案

由于我是您引用的问题的提问者,在这里回答我已经更新了我的问题,这是完全可能的。



您的方法应该工作:

  App.Router.map function(){
this.route('calendar_month',{path:'calendar /:year /:month'});
});

您必须添加的是序列化和模型挂钩实现:

  serialize:function(context){
//我假设你在一个对象中包裹年和月(可能是App.YearAndMonthObject)
var ret = {
year:context.get(year),
month:context.get(month)
};
return ret;
},
model:function(params){
//以某种方式从params中创建对象
var model = App.YearAndMonthObject.create({
year: params.year,
month:params.month
});
返回模型;
}

这也需要修改你对linkTo的使用:

  {{linkto calendar_month year month}} 

...而您只需使用:

  {{linkTo calendar_month yearAndMonth}} 

我认为这是一个废ish的方式来处理这个。



总结:所有路线是什么?


他们是 / strong>即可。在你的情况下,似乎这样,那个年和月是同样的关注(=路线)的一部分。因此,它们应该被包裹在一个新的Ember对象中,并且您的路线(CalendarMonthRoute)应该处理这个新对象(也许是YearAndMonthObject或CalendarMonthObject?)。



This seems to suggest that the answer is yes:

From Ember Pre1 to Pre4: Multiple dynamic segments per route? Update: What is the allowed syntax for dynamic segments?

... but I just want to confirm.

In my case, as a learning exercise, I'm building a calendar in Ember, with monthly displays. I need to be able to link from a given month to the previous month, and to the next month.

So I'd like to be able to

{{ linkTo calendar_month year month }}

and

this.transitionTo('calendarMonth', year, month)

Wondering if this is feasible without using nested resources. I can get it working with something like:

App.Router.map(function() {
  this.resource("year", { path: "calendar/:year" }, function() {
    this.resource("calendar_month", { path: "/:month" }, function() {
      this.route('index');
    });
  });
});

... but this involves introducing a Year object which might not really need to exist from a modeling perspective, just so I can use its id in linkTo

I'd prefer to set up a route with two parameters/dynamic segments:

App.Router.map(function() {
  this.route('calendar_month', { path: 'calendar/:year/:month'});
});

But am I correct that this is not possible? I just want to make sure I'm doing this the cleanest, emberiest way possible.

Put another way:

I understand this notion that "If your user interface is nested, then your routes should be nested", but, if my url is nested, this does not necessarily imply that my interface will be nested as well. So I'm wondering: if my url is nested, is it always best practice to build corresponding nested models?

Any guidance / clarification much appreciated.

thanks,

解决方案

As i am the asker of the question you referenced, i am answering here. I have already updated my question that this is perfectly possible.

Your approach should work:

App.Router.map(function() {
  this.route('calendar_month', { path: 'calendar/:year/:month'});
});

All you have to add are the serialize and model hook implementations:

serialize: function(context){
    // i assume that you wrap year and month in an Object (maybe App.YearAndMonthObject)
    var ret = {
        year : context.get("year"),
        month : context.get("month")
    };
    return ret;
},
model : function(params){
    //somehow create your object from the params
    var model = App.YearAndMonthObject.create({
        year : params.year,
        month : params.month
    });
    return model;
}

This would also need a modification on your use of linkTo:

{{ linkTo calendar_month year month }}

...instead you would just use:

{{ linkTo calendar_month yearAndMonth }}

I think this an emberish way to deal with this.

Summary: So what are routes all about?

They are used to separate concerns. And in your case it seems so, that year and month are part of the same concern (=route). Therefore they should be wrapped together in an new Ember Object and your route (CalendarMonthRoute) should deal with this new object (maybe YearAndMonthObject or CalendarMonthObject?).

这篇关于资源嵌套是启用多个动态段的唯一方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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