@@ -16,13 +16,42 @@ type pool struct {
16
16
17
17
// Component is actually an Extended Vue SubClass,
18
18
// which acts as a Component constructor in VueJS world
19
+ // thus you can use Component.New to create a
20
+ // preConfigured VueJS instance(*ViewModel).
21
+ type Component struct {
22
+ * ViewModel
23
+ }
24
+
25
+ // New create the component instance
26
+ func (c * Component ) New () * ViewModel {
27
+ return newViewModel (c .Object .New ())
28
+ }
29
+
30
+ func newComponent (o * js.Object ) * Component {
31
+ return & Component {
32
+ ViewModel : newViewModel (o ),
33
+ }
34
+ }
35
+
36
+ // Register register Component:c in the global namespace
37
+ func (c * Component ) Register (name string ) * Component {
38
+ vue .Call ("component" , name , c )
39
+ return c
40
+ }
41
+
42
+ func GetComponent (name string ) * Component {
43
+ return newComponent (vue .Call ("component" , name ))
44
+ }
45
+
46
+ // NewComponent creates and registers a named global Component
19
47
//
20
- // CreateComponent creats a new VueJS component
21
- func CreateComponent (
48
+ // vmCreator should return a gopherjs struct pointer. see New for more details
49
+ func NewComponent (
22
50
vmCreator func () (structPtr interface {}),
23
- templateOrSharpId string ,
51
+ templateStr string ,
24
52
replaceMountPoint ... bool ,
25
- ) * Vue {
53
+ ) * Component {
54
+ // args
26
55
idx := len (creatorPool )
27
56
creatorPool = append (creatorPool , new (pool ))
28
57
creatorPool [idx ].creator = vmCreator
@@ -34,57 +63,18 @@ func CreateComponent(
34
63
p .counter += 1
35
64
return p .structPtr
36
65
}
37
- // args
38
66
replace := true
39
67
if len (replaceMountPoint ) > 0 {
40
68
replace = replaceMountPoint [0 ]
41
69
}
42
- // create the VueInstance
43
- com := Extend (js.M {
44
- "data" : vmfn ,
45
- "init" : js .MakeFunc (func (this * js.Object , arguments []* js.Object ) interface {} {
46
- // set methods dynamicly before VueInstance doing all the other init
47
- this .Get ("$options" ).Set ("methods" , js .MakeWrapper (vmfn ()))
48
- // register this component instance to vMap
49
- vMap [vmfn ()] = & Vue {Object : this }
50
- return nil
51
- }),
52
- "template" : templateOrSharpId ,
53
- "replace" : replace ,
70
+ // opts
71
+ opt := NewOption ()
72
+ opt .Data = vmfn
73
+ opt .Template = templateStr
74
+ opt .Replace = replace
75
+ opt .OnLifeCycleEvent (EvtInit , func (vm * ViewModel ) {
76
+ vm .Options .Set ("methods" , js .MakeWrapper (vmfn ()))
77
+ vMap [vmfn ()] = vm
54
78
})
55
- return com
56
- }
57
-
58
- // Extend Create a “subclass” of the base Vue constructor which is a `Component`
59
- // The argument should be an object containing component options.
60
- // The special cases to note here are el and data options
61
- // - they must be functions when used with Vue.extend().
62
- func Extend (opt js.M ) * Vue {
63
- return & Vue {
64
- Object : vue .Call ("extend" , opt ),
65
- }
66
- }
67
-
68
- func RegisterComponent (name string , component * Vue ) * Vue {
69
- vue .Call ("component" , name , component .Object )
70
- return component
71
- }
72
-
73
- func GetComponent (name string ) * Vue {
74
- return & Vue {
75
- Object : vue .Call ("component" , name ),
76
- }
77
- }
78
-
79
- // Component creates and registers a named global component. (automatically call Vue.extend)
80
- //
81
- // vmCreator should return a gopherjs struct pointer. see New for more details
82
- func Component (
83
- name string ,
84
- vmCreator func () (structPtr interface {}),
85
- templateOrSharpId string ,
86
- replaceMountPoint ... bool ,
87
- ) * Vue {
88
- com := CreateComponent (vmCreator , templateOrSharpId , replaceMountPoint ... )
89
- return RegisterComponent (name , com )
79
+ return opt .NewComponent ()
90
80
}
0 commit comments