本书中出现了以下使用 grails 邮件插件提供的 sendMail 方法的示例。
sendMail {
to "foo@example.org"
subject "Registration Complete"
body view:"/foo/bar", model:[user:new User()]
}
我知道 {} 中的代码是一个作为参数传递给 sendMail 的闭包。我也明白to
,subject
并且body
是方法调用。
我试图弄清楚实现 sendMail 方法的代码是什么样的,我最好的猜测是这样的:
MailService {
String subject
String recipient
String view
def model
sendMail(closure) {
closure.call()
// Code to send the mail now that all the
// various properties have been set
}
to(recipient) {
this.recipient = recipient
}
subject(subject) {
this.subject = subject;
}
body(view, model) {
this.view = view
this.model = model
}
}
这是合理的,还是我错过了什么?特别是,闭包(to
, subject
, body
)中调用的方法是否必须与 ? 属于同一类的成员sendMail
?
谢谢,唐