1
1
package example
2
2
import org .scalajs .dom
3
+
3
4
import scalajs .js .annotation .JSExport
4
5
import scala .scalajs .concurrent .JSExecutionContext .Implicits .runNow
5
6
import dom .ext .Ajax
7
+
6
8
import scalajs .js
7
9
import js .Dynamic .literal
8
10
import com .felstar .scalajs .vue ._
11
+ import org .scalajs .dom .raw .HTMLElement
12
+
9
13
import js .annotation .JSName
10
14
11
15
@ JSExport
12
16
object Todo extends {
13
17
14
- // Strongly typed wrapper around below dynamic Vue
15
- // You don't have to do this, can access as js.Dynamic
16
- // But obviously type safety is a good thing, a Scala thing.
17
18
@ js.native
18
19
trait DemoVue extends Vue {
19
20
var title : String = js.native
20
21
var n : Double = js.native
21
- var todos : VueArray [DemoVueTodo ]= js.native
22
- }
23
-
22
+ var todos : js. Array [DemoVueTodo ]= js.native
23
+ }
24
+
24
25
type DemoVueMethod = js.ThisFunction0 [DemoVue ,_]
25
-
26
+
26
27
@ js.native
27
- trait DemoVueTodo extends js.Object {
28
+ trait DemoVueTodo extends js.Object {
28
29
var done : Boolean = js.native
29
30
var content : String = js.native
30
31
}
31
-
32
+
32
33
object DemoVueTodo {
33
34
def apply (done: Boolean ,content: String )= literal(done= done,content= content).asInstanceOf [DemoVueTodo ]
34
35
}
@@ -38,59 +39,47 @@ object Todo extends {
38
39
// could have returned raw Vue of course
39
40
def main (): DemoVue = {
40
41
41
- val tasks = js.Array (" Learn JavaScript" ," Learn Vue.js" ," Learn Scala.js" )
42
-
42
+ val tasks = js.Array (" Learn JavaScript" ," Learn Vue.js" ," Learn Scala.js" )
43
+ //
43
44
def ts = new java.util.Date ().toString
44
45
45
46
Vue .component(" my-component" , literal(
46
47
props= js.Array (" myMsg" ),
47
- template= " <p>A custom component with msg {{myMsg}} <content >default content</content ></p>" ))
48
+ template= " <p>A custom component with msg {{myMsg}} <slot >default content</slot ></p>" ))
48
49
49
- // Note, don't need to define as js.ThisFunction, as signature expects this
50
- // Vue.directive("my-directive", (directive:Directive,value:js.Any)=> {
51
- // println(s"myDirective! expression=${directive.expression} arg=${directive.arg} raw=${directive.raw} name=${directive.name} value=$value ")
52
- // directive.el.innerHTML="This comes from my-directive "+directive.raw+" "+value
53
- // })
54
-
55
-
50
+ Vue .directive(" mydirective" , literal(
51
+ update= (el: HTMLElement , directive: Directive )=> {el.innerHTML= " This comes from my-directive with contents " + directive.value+ " and expression " + directive.expression}
52
+ )
53
+ )
54
+
56
55
val demo = new Vue (
57
56
literal(el= " #demo" ,
58
57
data= literal(
58
+ message= " Hello Vue.js!!!!!" ,
59
59
title= " Todo App" ,
60
- todos= tasks.map(content=> literal(done= content== tasks.head,content= content)),
60
+ todos= tasks.map(content=> literal(done= content== tasks.head,content= content)),
61
61
barValue= 100 ,
62
- n= 0
63
- ),
62
+ n= 0
63
+ ),// ,
64
64
// js.ThisFunction would be fine, just trying to be more type specific
65
65
methods= literal(clickHandler= ((demoVue: DemoVue )=> demoVue.n-= 1 ): DemoVueMethod ,
66
66
addTask= ((demoVue: DemoVue )=> demoVue.todos.append(DemoVueTodo (false ,s " new $ts" ))): DemoVueMethod ,
67
- change1st= ((demoVue: DemoVue )=> demoVue.todos.$ set(0 ,DemoVueTodo (false ,ts))): DemoVueMethod ,
68
- remove= ((demoVue: DemoVue ,idx: Int )=> demoVue.todos.$remove( idx)): js.ThisFunction1 [DemoVue ,Int ,_],
69
- flipAll= ((demoVue: DemoVue )=> demoVue.todos.foreach(td=> td.done= ! td.done)): DemoVueMethod
67
+ change1st= ((demoVue: DemoVue )=> Vue . set(demoVue.todos, 0 ,DemoVueTodo (false ,ts))): DemoVueMethod ,
68
+ remove= ((demoVue: DemoVue ,idx: Int )=> Vue .delete( demoVue.todos, idx)): js.ThisFunction1 [DemoVue ,Int ,_],
69
+ flipAll= ((demoVue: DemoVue )=> demoVue.todos.foreach(td=> td.done= ! td.done)): DemoVueMethod
70
70
),
71
- computed= literal(todosFiltered = (demoVue: DemoVue )=> demoVue.todos.map(_.content)),
72
-
73
- filters= literal(reverse= ((value: js.Any )=> value.toString.reverse),
71
+ computed= literal(todosComputed = (demoVue: DemoVue )=> demoVue.todos.map(_.content)),
72
+ //
73
+ filters= literal(reverse= ((value: js.Any )=> value.toString.reverse),
74
74
wrap= (value: js.Any ,begin: String , end: String )=> begin+ value.toString+ end,
75
75
extract= (array: js.Array [js.Dynamic ],field: String )=>
76
- if (js.isUndefined(array)) array else array.map(_.selectDynamic(field))
77
- ),
78
- events= literal(greeting= ((demoVue: DemoVue ,msg: js.Any )=> println(s " Greeting $msg" )): js.ThisFunction ,
79
- greeting2= (msg: js.Any )=> println(s " Greeting2 $msg" )
80
- ),
81
- directives= literal( // directives get passed a Directive object.
82
- myDirective= ((directive: Directive ,value: String )=> {
83
- println(s " myDirective expression= ${directive.expression} arg= ${directive.arg} raw= ${directive.raw} name= ${directive.name} value= $value " );
84
- directive.el.innerHTML= " This comes from my-directive " + directive.raw+ " " + value
85
- }): js.ThisFunction
86
- )
87
- )
76
+ if (js.isUndefined(array)) array else array.map(_.selectDynamic(field))
77
+ )
78
+ )
88
79
)
89
80
90
- demo.$watch(" title+' <<title'" ,(newValue: String , oldValue: String ) => println(" changed " + newValue))
91
- demo.$emit(" greeting" ," hello" )
92
- demo.$emit(" greeting2" ," goodbye" )
93
-
81
+ demo.$watch(" title" ,(newValue: String , oldValue: String ) => println(" changed " + newValue))
82
+
94
83
val demoVue = demo.asInstanceOf [DemoVue ]
95
84
96
85
// filters declared above inline, can be also done as below
@@ -99,7 +88,7 @@ object Todo extends {
99
88
100
89
// println(js.JSON.stringify(demo.$data))
101
90
102
- demo.$log
91
+ // demo.$log
103
92
104
93
demoVue
105
94
}
0 commit comments