Skip to content

Commit 3770d19

Browse files
committed
add Render and computed support
1 parent f278311 commit 3770d19

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

option.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,6 @@ type Option struct {
113113
// use of the common <script type="x-template"> trick to include templates.
114114
Template string `js:"template"`
115115

116-
// Type: Boolean
117-
//
118-
// Default: true
119-
//
120-
// Restriction: only respected if the template option is also present.
121-
//
122-
// Details:
123-
//
124-
// Determines whether to replace the element being mounted on with the
125-
// template. If set to false, the template will overwrite the element’s inner
126-
// content without replacing the element itself.
127-
Replace bool `js:"replace"`
128-
129116
// parent
130117
//
131118
// Type: Vue instance
@@ -186,6 +173,9 @@ func (c *Option) prepare() (opts *js.Object) {
186173
// SetDataWithMethods set data and methods of the genereated VueJS instance
187174
// based on `structPtr` and `js.MakeWrapper(structPtr)`
188175
func (c *Option) SetDataWithMethods(structPtr interface{}) *Option {
176+
if structPtr == nil {
177+
return c
178+
}
189179
c.Set("data", structPtr)
190180
c.Set("methods", js.MakeWrapper(structPtr))
191181
return c
@@ -203,6 +193,43 @@ func (o *Option) AddMethod(name string, fn func(vm *ViewModel, args []*js.Object
203193
})
204194
}
205195

196+
type CreateElement func(tagName string, data interface{}, children []interface{}) (vnode *js.Object)
197+
type Render func(vm *ViewModel, fn CreateElement)
198+
199+
func (o *Option) SetRender(r Render) {
200+
fn := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
201+
vm := newViewModel(this)
202+
jsCreateElement := arguments[0]
203+
createElement := func(tagName string, data interface{}, children []interface{}) (vnode *js.Object) {
204+
return jsCreateElement.Call(tagName, data, children)
205+
}
206+
r(vm, createElement)
207+
return nil
208+
})
209+
o.Object.Set("render", fn)
210+
}
211+
212+
// AddComputed set computed data
213+
func (o *Option) AddComputed(name string, getter func(vm *ViewModel) interface{}, setter ...func(vm *ViewModel, val *js.Object)) {
214+
conf := make(map[string]js.M)
215+
conf[name] = make(js.M)
216+
fnGetter := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
217+
vm := newViewModel(this)
218+
return getter(vm)
219+
})
220+
conf[name]["get"] = fnGetter
221+
if len(setter) > 0 {
222+
fnSetter := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
223+
vm := newViewModel(this)
224+
setter[0](vm, arguments[0])
225+
return nil
226+
})
227+
conf[name]["set"] = fnSetter
228+
}
229+
// using mixin here
230+
o.addMixin("computed", conf)
231+
}
232+
206233
func (o *Option) OnLifeCycleEvent(evt LifeCycleEvent, fn func(vm *ViewModel)) *Option {
207234
return o.addMixin(
208235
string(evt),

0 commit comments

Comments
 (0)