Exploring Computed Properties, Filters and Watchers
1. Computed Properties
On the main page, we added a few things to demonstrate this..
vue.setdata("firstname", "Anele")
vue.SetData("lastname", "Mbanga")
'
Dim fname As VMElement = vue.CreateH1("").SetText("The fullname is {{ getfullname }}")
We want the {{ getfullname }} property to be "computed" by concatinating the first name and last name.
1. We then tell the vue instance, that we have a computed property. This has a call back method.
vue.SetComputed("getfullname", Me, "getfullname1")
This gets the saved state values and binds them together and returns the result.
Sub getfullname1 As String
Dim fname As String = vue.getdata("firstname")
Dim lname As String = vue.GetData("lastname")
Return $"${fname} ${lname}"$
End Sub
Each time a new state is fed for "firstname" or "lastname" with vue.SetData(?, ?) then "getfullname" property will change with the result of the callback.
2. Filters
These act like computed properties but are useful for formatting output.
We want to show the total length of the "getfullname" result. We define a filter like element.
Dim fnamecount As VMElement = vue.CreateH1("").SetText("The count of strings is {{ getfullname | countletters }}")
We define that we get the details from a filter callback.
vue.SetFilter("countletters", Me, "countletters1")
This takes the obtained value and then executes what is needed. In this case it will return 12 as depicted.
Sub countletters1(value As String) As Int
Return value.length
End Sub
3. Watchers
You can watch changes in states and then executing callbacks when these state propeties change. For this we have added some input boxes and are watching the kilometers and meters. So each time a value changes, we do some calculation.
vue.setdata("kilometers", "0")
vue.Setdata("meters", "0")
'
Dim inpk As VMElement = vue.CreateINPUT("").settype("text").setvmodel("kilometers")
Dim inpm As VMElement = vue.createinput("").settype("text").Setvmodel("meters")
div.AddComponent(inpk.tostring)
div.AddComponent(inpm.tostring)
vue.SetWatch("kilometers", True, True, Me, "kilometers1")
vue.SetWatch("meters", True, True, Me, "meters1")
Remember: For re-active states, these need to be initialized first, ALL THE TIME for the first time!
So each time the "values" for kilometers and meters as defined by the vmodels change, we need to chall kilometers1 and meters1 callbacks.
These receive the entered values and then you can process them.
Sub kilometers1(val As Object)
Dim dmeters As Int = val * 1000
vue.setdata("kilometers", val)
vue.setdata("meters", dmeters)
End Sub
Sub meters1(val As Object)
Dim dkilometers As Int = val / 1000
vue.setdata("kilometers", dkilometers)
vue.setdata("meters", val)
End Sub
vue.setdata is updating the view state and
vue.getstate retries the stored values. For these watches, we do some calculations and assign the values to the view state, which in turn updates the vmodel i.e. the values in the input boxes.
We will now explore this with actual Vuetify components and routers. This covers some VueJS functionality that is useful.
Enjoy!