1
1
package vue
2
2
3
- import (
4
- "github.com/gopherjs/gopherjs/js"
5
- )
3
+ import "github.com/gopherjs/gopherjs/js"
4
+ import "github.com/oskca/gopherjs-dom"
6
5
7
- type DirectiveContext struct {
6
+ type DirectiveBinding struct {
8
7
* js.Object
9
- // el: the element the directive is bound to.
10
- El * js.Object `js:"el"`
11
- // vm: the context ViewModel that owns this directive.
12
- Vm * ViewModel `js:"vm"`
8
+ // name: the name of the directive, without the prefix.
9
+ Name string `js:"name"`
10
+ // value: The value passed to the directive. For example in v-my-directive="1 + 1", the value would be 2.
11
+ Value string `js:"value"`
12
+ // oldValue: The previous value, only available in update and componentUpdated. It is available whether or not the value has changed.
13
+ OldValue string `js:"oldValue"`
13
14
// expression: the expression of the binding, excluding arguments and filters.
14
15
Expression string `js:"expression"`
15
16
// arg: the argument, if present.
16
17
Arg string `js:"arg"`
17
- // name: the name of the directive, without the prefix.
18
- Name string `js:"name"`
19
18
// modifiers: an object containing modifiers, if any.
20
19
Modifiers * js.Object `js:"modifiers"`
21
- // descriptor: an object that contains the parsing result of the entire directive.
22
- Descriptor * js.Object `js:"descriptor"`
23
- // params: an object containing param attributes. Explained below.
24
- Params * js.Object `js:"params"`
25
20
}
26
21
27
- func makeUpdater (fn func (ctx * DirectiveContext , val * js.Object )) * js.Object {
28
- return js .MakeFunc (func (this * js.Object , args []* js.Object ) interface {} {
29
- ctx := & DirectiveContext {
30
- Object : this ,
31
- }
32
- fn (ctx , args [0 ])
33
- return nil
34
- })
35
- }
36
-
37
- func makeBinder (fn func (* DirectiveContext )) * js.Object {
38
- return js .MakeFunc (func (this * js.Object , args []* js.Object ) interface {} {
39
- ctx := & DirectiveContext {
40
- Object : this ,
41
- }
42
- fn (ctx )
43
- return nil
44
- })
45
- }
22
+ // DirectiveCallback can be used in every directive callback functions
23
+ type DirectiveCallback func (el * dom.Element , b * DirectiveBinding , vNode , oldVnode * js.Object )
46
24
47
25
type Directive struct {
48
26
* js.Object
49
- Name string
50
- // advanced options
51
- // Custom directive can provide a params array,
52
- // and the Vue compiler will automatically extract
53
- // these attributes on the element that the directive is bound to.
54
- Params []string `js:"params"`
55
- // If your custom directive is expected to be used on an Object,
56
- // and it needs to trigger update when a nested property inside
57
- // the object changes, you need to pass in deep: true in your directive definition.
58
- Deep bool `js:"deep"`
59
- // If your directive expects to write data back to
60
- // the Vue instance, you need to pass in twoWay: true.
61
- // This option allows the use of this.set(value) inside
62
- // the directive:If your directive expects to write data back to
63
- // the Vue instance, you need to pass in twoWay: true.
64
- // This option allows the use of this.set(value) inside the directive
65
- TwoWay bool `js:"twoWay"`
66
- // Passing in acceptStatement:true enables
67
- // your custom directive to accept inline statements like v-on does
68
- AcceptStatement bool `js:"acceptStatement"`
69
- // Vue compiles templates by recursively walking the DOM tree.
70
- // However when it encounters a terminal directive,
71
- // it will stop walking that element’s children.
72
- // The terminal directive takes over the job of compiling the element and
73
- // its children. For example, v-if and v-for are both terminal directives.
74
- Terminal bool `js:"terminal"`
75
- // You can optionally provide a priority number for your directive.
76
- // If no priority is specified, a default priority will be used
77
- // - 1000 for normal directives and 2000 for terminal directives.
78
- // A directive with a higher priority will be processed earlier than
79
- // other directives on the same element. Directives with
80
- // the same priority will be processed in the order they appear in
81
- // the element’s attribute list, although that order is not
82
- // guaranteed to be consistent in different browsers.
83
- Priority int `js:"priority"`
27
+ // // Name string
28
+ // // advanced options
29
+ // // Custom directive can provide a params array,
30
+ // // and the Vue compiler will automatically extract
31
+ // // these attributes on the element that the directive is bound to.
32
+ // Params []string `js:"params"`
33
+ // // If your custom directive is expected to be used on an Object,
34
+ // // and it needs to trigger update when a nested property inside
35
+ // // the object changes, you need to pass in deep: true in your directive definition.
36
+ // Deep bool `js:"deep"`
37
+ // // If your directive expects to write data back to
38
+ // // the Vue instance, you need to pass in twoWay: true.
39
+ // // This option allows the use of this.set(value) inside
40
+ // // the directive:If your directive expects to write data back to
41
+ // // the Vue instance, you need to pass in twoWay: true.
42
+ // // This option allows the use of this.set(value) inside the directive
43
+ // TwoWay bool `js:"twoWay"`
44
+ // // Passing in acceptStatement:true enables
45
+ // // your custom directive to accept inline statements like v-on does
46
+ // AcceptStatement bool `js:"acceptStatement"`
47
+ // // Vue compiles templates by recursively walking the DOM tree.
48
+ // // However when it encounters a terminal directive,
49
+ // // it will stop walking that element’s children.
50
+ // // The terminal directive takes over the job of compiling the element and
51
+ // // its children. For example, v-if and v-for are both terminal directives.
52
+ // Terminal bool `js:"terminal"`
53
+ // // You can optionally provide a priority number for your directive.
54
+ // // If no priority is specified, a default priority will be used
55
+ // // - 1000 for normal directives and 2000 for terminal directives.
56
+ // // A directive with a higher priority will be processed earlier than
57
+ // // other directives on the same element. Directives with
58
+ // // the same priority will be processed in the order they appear in
59
+ // // the element’s attribute list, although that order is not
60
+ // // guaranteed to be consistent in different browsers.
61
+ // Priority int `js:"priority"`
84
62
}
85
63
86
- func NewDirective (updater ... func (ctx * DirectiveContext , val * js.Object )) * Directive {
64
+ func NewDirective (updater ... func (el * js. Object , b * DirectiveBinding , val * js.Object )) * Directive {
87
65
d := & Directive {
88
66
Object : js .Global .Get ("Object" ).New (),
89
67
}
@@ -93,18 +71,18 @@ func NewDirective(updater ...func(ctx *DirectiveContext, val *js.Object)) *Direc
93
71
return d
94
72
}
95
73
96
- func (d * Directive ) SetBinder (fn func (ctx * DirectiveContext )) * Directive {
97
- d .Set ("bind" , makeBinder ( fn ) )
74
+ func (d * Directive ) SetBinder (fn func (b * DirectiveBinding )) * Directive {
75
+ d .Set ("bind" , fn )
98
76
return d
99
77
}
100
78
101
- func (d * Directive ) SetUnBinder (fn func (ctx * DirectiveContext )) * Directive {
102
- d .Set ("unbind" , makeBinder ( fn ) )
79
+ func (d * Directive ) SetUnBinder (fn func (b * DirectiveBinding )) * Directive {
80
+ d .Set ("unbind" , fn )
103
81
return d
104
82
}
105
83
106
- func (d * Directive ) SetUpdater (fn func (ctx * DirectiveContext , val * js.Object )) * Directive {
107
- d .Set ("update" , makeUpdater ( fn ) )
84
+ func (d * Directive ) SetUpdater (fn func (el * js. Object , b * DirectiveBinding , val * js.Object )) * Directive {
85
+ d .Set ("update" , fn )
108
86
return d
109
87
}
110
88
@@ -131,7 +109,7 @@ type ElementDirective struct {
131
109
* Directive
132
110
}
133
111
134
- func NewElementDirective (updater ... func (ctx * DirectiveContext , val * js.Object )) * ElementDirective {
112
+ func NewElementDirective (updater ... func (el * js. Object , b * DirectiveBinding , val * js.Object )) * ElementDirective {
135
113
d := & ElementDirective {
136
114
Directive : NewDirective (updater ... ),
137
115
}
0 commit comments