Weitere ähnliche Inhalte Ähnlich wie Vaadin+Scala (20) Mehr von Vasil Remeniuk (20) Vaadin+Scala5. No javascript
- No bad parts
- Good parts still accessible
- Built on GWT
- Large test suite for each browser
11. +Scala
6 class TransactionPage(controller:TransactionController=new TransactionController) extends BasePage
{
7 model.params = params
8 compositionRoot = new VerticalLayout {
9 val form = new Form {
10 caption = 'hello
11 val beanItem = new BeanItem(model) {
12 item = this
13 visibleItemProperties = propertyIds().filter(_ != "progress")
14 }
15 field("price").foreach { p =>
16 p.required = true
17 p.validators += >=(10.0)
18 }
19 }
20 components ++=
21 Button('check, Notification.show(model.toString)) ::
22 Button('start, validated(form)(controller.start(done))) ::
23 form ::
24 new ProgressIndicator {property = form.beanItem.property("progress")} ::
25 Nil
26 }
27 def done = Notification.show('done, Type.Tray)
28 def model = controller.model
29 }
13. +Scala => declarative
- comparable to JSON
1 {"transactionPage": {
2 "controller": "TransactionController",
3 "compositionRoot": {
4 "type": "VerticalLayout",
5 "components": [
6 {
7 "type": "Button",
8 "caption": "hello"
....
9 }
10 ]
11 }
12 }}
- no need to reference view from controller
- statically checked
- support for HTML templates
14. +Scala => inline constructors
class TransactionPage(controller: TransactionController = new TransactionController) extends BasePage {
//body goes here
}
15. +Scala => DelayedInit
8 trait BasePage extends CustomComponent with Validations with DelayedInit {
9 var params: String = ""
...
16 def init(params: String) = {
17 this.params = params
18 initCode.foreach(_())
19 }
20 }
14 case class TabView(name: String)(content: => Seq[Component] = stubContent(name))
15 extends VerticalLayout with View {
16 def enter(event: ViewChangeEvent) {
17 //showing that state persists and lazy loading is possible
18 val params = event.getParameters
19 content.collect {
20 case page: BasePage => add(page).init(params)
21 case view => add(view)
22 }
23 Notification.show("Entered " + name + " with params " + params)
24 }
25 }
class TransactionPage(controller: TransactionController = new TransactionController) extends BasePage {
model.params = this.params
16. +Scala => callbacks and HOFs
Button('start, validated(form)(controller.start(done)))
def apply(caption: String, clickListener: => Unit): Button
def validated(form: Form)(action: => Unit)
def start(done: => Unit)
- increase code reuse
- decrease coupling
17. +Scala => symbols
- localization
- natural names
caption = 'hello
implicit def translateSymbol(s: Symbol) = s"${s.name} locale:${self.locale}"
implicit def translateTuple[T](s: (Symbol, T))
(implicit toParams: T => TranslationParams) =
translateSymbol(s._1).format(toParams(s._2).p: _*)
validators += >=(10.0)
def >=[T](bound: T)(implicit ord: Ordering[T]): Option[Any] => Validation = {
import ord._; v => v match {
case Some(v: T) if v >= bound => Valid
case _ => Invalid(Symbol("should be greater %s") -> bound :: Nil)
}
}
18. +Scala => structural types
- navigate in nested objects
val form = new Form {
val beanItem = new BeanItem(model) {
...
form.beanItem.property("progress")
- eliminate dependencies
trait Translation {self: {def locale: Option[Locale]} =>