Lift Web 框架 DRY 调度
我有一个 Image 类:
class Image extends LongKeyedMapper[Image] with IdPK with Logger {
它重写了 toHtml 方法:
override def toHtml =
<img src={"/gallery/image/%s/%s/%s/%s" format (slug.is, "fit", 100, 100)} />
它的工作原理是这样的:
def dispatch = {
LiftRules.dispatch.append {
case Req("gallery" :: "image" :: slug :: method :: width :: height :: Nil, _, _) => {
() => Image.stream(slug, method, width, height)
}
}
}
正如您所看到的,这是非 DRY 方法,因为您必须定义 URL (/gallery/image) 两次。
可以使其干燥吗?你能从 LiftRules 或其他东西获得路径吗?
提前致谢, 埃塔姆。
I have an Image class:
class Image extends LongKeyedMapper[Image] with IdPK with Logger {
which overrides toHtml method:
override def toHtml =
<img src={"/gallery/image/%s/%s/%s/%s" format (slug.is, "fit", 100, 100)} />
and it works beacause of this:
def dispatch = {
LiftRules.dispatch.append {
case Req("gallery" :: "image" :: slug :: method :: width :: height :: Nil, _, _) => {
() => Image.stream(slug, method, width, height)
}
}
}
As you can see this is not DRY approach, since you have to define the URL (/gallery/image) twice.
Is it possible to make it DRY? Can you get the path from LiftRules or something?
Thanks in advance,
Etam.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
David Pollak 在电梯列表中回答了这个问题:
https://groups.google.com/d/topic/liftweb/VG0uOut9hb4/discussion
简而言之,您:
将共同的事物(在本例中为路径)封装在一个对象中:
创建一个自定义 unapply 方法,该方法允许您在调度方法中的模式匹配中使用该对象。
您的代码现在是:
...并且:
请参阅消息线程以获取更多建议。
This was answered by David Pollak on the lift list:
https://groups.google.com/d/topic/liftweb/VG0uOut9hb4/discussion
In short, you:
encapsulate the things in common (in this case, the path) in an object:
create a custom unapply method that allows you to use the object in the pattern match in your dispatch method.
Your code is now:
...and:
See the message thread for more suggestions.