[英]Relative time for views in Play Framework 2.1
It seems like the Play Framework used(?) to have a since()
function to get relative time (like "4 minutes ago") in views for Play 1.x. 似乎Play框架使用(?)有一个
since()
函数来获取Play 1.x视图中的相对时间(如“4分钟前”)。 This is mentioned as a Date Extension here 这在这里被称为日期扩展
However in Play 2.1 this doesn't appear to work anymore. 但是在Play 2.1中,这似乎不再起作用了。 I get
value since is not a member of java.util.Date
... Additionally I can't find any other references to since()
(in the context of Play 2.1) online. 我得到的
value since is not a member of java.util.Date
...另外我找不到任何其他对于since()
(在Play 2.1的上下文中)的在线引用。
Is there a proper/default way of handling this common case? 是否有正确/默认的方式来处理这种常见情况? I feel like I must be missing something important since this seems to no longer be supported?
我觉得我必须遗漏一些重要的东西,因为这似乎不再受到支持了?
Thanks! 谢谢!
The since()
method is unavailable since beginning of the 2.0 branch. since()
2.0分支开始since()
方法不可用。
Not a direct answer, but if it's possible I'd suggest to use JavaScript plugin for this task for an example: timeago jQuery plugin reasons: 不是直接的答案,但如果有可能我建议使用JavaScript插件来完成此任务的一个例子: timeago jQuery插件原因:
since
time will be still displayed correctly as it's updated on the client-side. since
时间将在客户端更新时仍然正确显示。 As far as I can tell this isn't possible to do any more (by default in Play2.1). 据我所知,这是不可能再做的(默认情况下在Play2.1中)。 Please correct me if I am wrong.
如果我错了,请纠正我。 Here's how I recreated it.
这是我重新创建它的方式。 As mentioned here I "pimped" the "Date" class:
如上所述,我“拉皮条”了“日期”类:
// File app/views/pimps.scala //文件app / views / pimps.scala
package views
package object pimps {
import java.util.Date
import org.joda.time.DateTime;
import org.joda.time.Period;
class PimpedDate(col: Date) {
def since() = {
def addS(b: Int) = if (b == 1) "" else "s"
val now: DateTime = new DateTime();
val period: Period = new Period(new DateTime(col), now);
var r: String = "";
if (period.getYears() > 0) {
r = period.getYears() + " year" + addS(period.getYears()) + " ago";
} else if (period.getWeeks() > 0) {
r = period.getWeeks() + " week" + addS(period.getWeeks()) + " ago";
} else if (period.getMonths() > 0) {
r = period.getMonths() + " month" + addS(period.getMonths()) + " ago";
} else if (period.getDays() > 0) {
r = period.getDays() + " day" + addS(period.getDays()) + " ago";
} else if (period.getHours() > 0) {
r = period.getHours() + " hour" + addS(period.getHours()) + " ago";
} else if (period.getMinutes() > 0) {
r = period.getMinutes() + " minute" + addS(period.getMinutes()) + " ago";
} else {
r = period.getSeconds() + " second" + addS(period.getSeconds()) + " ago";
}
r
}
}
implicit def pimpDate(col: Date) = new PimpedDate(col)
}
Then in my view I can just import the above: 然后在我看来我可以导入上面的内容:
@import views.pimps._
and then use since()
just as you could in Play1 然后像在Play1中一样使用from
since()
<td>@record.created_on.since()</td>
Please comment/answer if there's a better way to do this or write the scala code... 请评论/回答是否有更好的方法来执行此操作或编写scala代码...
@Jack suggested a pretty good answer. @Jack提出了一个很好的答案。
Here is a version of his code that might be useful because it'll enable some composition if needed (the check
function is not composing but could be easily changed to compose and show a more detailed since value) 这是他的代码的一个版本可能很有用,因为它会在需要时启用一些组合(
check
功能不是组合,但可以很容易地改变组成并显示更详细的值)
package object pimps {
import java.util.Date
import org.joda.time.DateTime;
import org.joda.time.Period;
def step(f:Period => Int)(fi:String):Period => Option[String] = {
def g(i:Int = 1) = i + " " + fi + (if (i==1) "" else "s") + " ago"
(p:Period) => {
f(p) match {
case 0 => None
case 1 => Some(g())
case x => Some(g(x))
}
}
}
val yearsStep = step(_.getYears)("year")
val monthsStep = step(_.getMonths)("month")
val daysStep = step(_.getDays)("day")
val hoursStep = step(_.getHours)("hour")
val minutesStep = step(_.getMinutes)("minute")
val secondsStep = step(_.getSeconds)("second")
val steps = Seq(yearsStep, monthsStep, daysStep, hoursStep, minutesStep, secondsStep)
val check =
(p:Period) =>
steps.collectFirst {
case f if f(p).isDefined => f(p).get
}
implicit class PimpedDate(col: Date) {
def since() = {
val period: Period = new Period(new DateTime(col), DateTime.now);
check(period)
}
}
}
As you can see, for now we stop at the first matching level, and also we repeat the getter (getYears if matching will be called twice). 正如您所看到的,现在我们停在第一个匹配级别,并且我们也重复getter(getYears如果匹配将被调用两次)。
Nevertheless, another thing to note is the usage of implicit class
which has been introduced in Scala 2.10 to ease the pimping 然而,需要注意的另一件事是使用Scala 2.10中引入的
implicit class
来缓解拉皮条
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.