Skip to content

Commit 78fae02

Browse files
committed
v. 3.3.0 - Added PostBack method (client script) and corresponding server event.
1 parent 07770c4 commit 78fae02

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Note: When using .vue files, the JavaScript output is cached between requests (i
115115
- `Options` - Vue options for the component in JavaScript format. Only used with simple in-line templates.
116116
- `SquashWS` - Boolean value (default true) indicating if all white space in HTML templates should be squashed (sequences of space, `<LF>`, `<CR>`, `<Tab>` are replaced with a single space).
117117

118+
Events (server side):
119+
- `PostBack(eventArgument)`- Raised when script in the Vue.js component calls `this.$options.PostBack(eventArgument)`. Note that the server control must have an `ID` attribute for this to be available.
120+
118121
- **App**
119122

120123
Makes it easy to render a Vue.js application instance.
@@ -126,6 +129,9 @@ Note: When using .vue files, the JavaScript output is cached between requests (i
126129
- `Mount` - Boolean value (default true) indicating if a `<div>` tag with a random id should be generated and the Vue instance mounted to this.
127130
- `SquashWS` - Boolean value (default true) indicating if all white space in HTML templates should be squashed (sequences of space, `<LF>`, `<CR>`, `<Tab>` are replaced with a single space).
128131

132+
Events (server side):
133+
- `PostBack(eventArgument)`- Raised when script in the Vue.js application calls `this.$options.PostBack(eventArgument)`. Note that the server control must have an `ID` attribute for this to be available.
134+
129135

130136
- **ServerTemplate**
131137

src/Module1.vb

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
Friend Module Module1
44

5-
Friend Function MakeVueOptions(content As String, prpFile As String, prpOptions As String, prpsquashWS As Boolean) As String
5+
Friend Function MakeVueOptions(content As String, prpFile As String, prpOptions As String, prpsquashWS As Boolean, embedExtra As String) As String
66
Dim ctx = System.Web.HttpContext.Current
77
If String.IsNullOrEmpty(prpFile) Then
88
REM In-line template
99
If content.Length = 0 Then Throw New Exception("Template is empty")
1010
If content.StartsWith("<template>", StringComparison.InvariantCultureIgnoreCase) Then
1111
If Not String.IsNullOrEmpty(prpOptions) Then Throw New Exception("Control cannot have 'Options' property when content starts with <template>")
12-
Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), ctx.Request.Url.AbsolutePath, prpsquashWS, content) & ")()"
12+
Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), ctx.Request.Url.AbsolutePath, embedExtra, prpsquashWS, content) & ")()"
1313
Else
1414
prpOptions = If(prpOptions, "").Trim
1515
If prpOptions.Length = 0 Then prpOptions = "{}"
1616
If Not prpOptions.StartsWith("{") OrElse Not prpOptions.EndsWith("}") Then Throw New Exception("Invalid 'Options' property")
1717
If prpsquashWS Then content = VueFilesToJS.SquashWhiteSpace(content)
18-
Return "{" &
18+
Return "{" & embedExtra &
1919
"template:" & VueFilesToJS.JSStringEncode(content) & "," &
2020
prpOptions.Substring(1)
2121
End If
@@ -32,7 +32,7 @@ Friend Module Module1
3232
Dim obj = ctx.Cache.Get(CacheKey)
3333
If obj Is Nothing Then
3434
Dim FileList As New List(Of String)
35-
f = VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, prpsquashWS, Nothing, AddressOf FileList.Add)
35+
f = VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, embedExtra, prpsquashWS, Nothing, AddressOf FileList.Add)
3636
ctx.Cache.Add(CacheKey, f, New Web.Caching.CacheDependency(FileList.ToArray), Web.Caching.Cache.NoAbsoluteExpiration, Web.Caching.Cache.NoSlidingExpiration, Web.Caching.CacheItemPriority.Normal, Nothing)
3737
Else
3838
f = DirectCast(obj, String)

src/My Project/AssemblyInfo.vb

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Imports System.Runtime.InteropServices
1212
<Assembly: AssemblyDescription("")>
1313
<Assembly: AssemblyCompany("")>
1414
<Assembly: AssemblyProduct("Vue.js ASP.NET Web Forms helpers")>
15-
<Assembly: AssemblyCopyright("Copyright © 2018-2019 Jesper Høy")>
15+
<Assembly: AssemblyCopyright("Copyright © 2018-2021 Jesper Høy")>
1616
<Assembly: AssemblyTrademark("")>
1717

1818
<Assembly: ComVisible(False)>
@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
3131
' by using the '*' as shown below:
3232
' <Assembly: AssemblyVersion("1.0.*")>
3333

34-
<Assembly: AssemblyVersion("3.2.0.0")>
35-
<Assembly: AssemblyFileVersion("3.2.0.0")>
34+
<Assembly: AssemblyVersion("3.3.0.0")>
35+
<Assembly: AssemblyFileVersion("3.3.0.0")>

src/VueApp.vb

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
<ParseChildren>
44
Public Class App
55
Inherits System.Web.UI.WebControls.WebControl
6+
Implements IPostBackEventHandler
67

78
Property File As String
89
Property VarName As String
910
Property Options As String
1011
Property Mount As Boolean = True
1112
Property SquashWS As Boolean = True
1213

14+
Public Event PostBack(eventArgument As String)
15+
1316
Protected Overrides Sub Render(writer As HtmlTextWriter)
1417
Dim sw = New IO.StringWriter
1518
Dim htw = New HtmlTextWriter(sw)
1619
MyBase.RenderChildren(htw)
1720
Dim Content = sw.ToString.Trim
1821

19-
Dim opt = MakeVueOptions(Content, File, Options, SquashWS)
22+
Dim opt = MakeVueOptions(Content, File, Options, SquashWS,
23+
If(Me.ID Is Nothing, "", "PostBack(ea) {" & Me.Page.ClientScript.GetPostBackEventReference(Me, "#").Replace("'#'", "ea") & "},"))
2024

2125
If Mount Then
2226
Dim DivName = "VueApp"
@@ -46,4 +50,8 @@ Public Class App
4650
REM nothing
4751
End Sub
4852

53+
Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
54+
RaiseEvent PostBack(eventArgument)
55+
End Sub
56+
4957
End Class

src/VueComponent.vb

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
<ParseChildren>
44
Public Class Component
55
Inherits System.Web.UI.WebControls.WebControl
6+
Implements IPostBackEventHandler
67

78
Property File As String
89
Property Name As String
910
Property Options As String
1011
Property SquashWS As Boolean = True
1112

13+
Public Event PostBack(eventArgument As String)
14+
1215
Protected Overrides Sub Render(writer As HtmlTextWriter)
1316
If String.IsNullOrEmpty(Name) Then
1417
If String.IsNullOrEmpty(File) Then Throw New Exception("'Name' or 'File' property is required")
@@ -21,7 +24,9 @@ Public Class Component
2124
MyBase.RenderChildren(htw)
2225
Dim Content = sw.ToString.Trim
2326

24-
Dim opt = MakeVueOptions(Content, File, Options, SquashWS)
27+
Dim opt = MakeVueOptions(Content, File, Options, SquashWS,
28+
If(Me.ID Is Nothing, "", "PostBack(ea) {" & Me.Page.ClientScript.GetPostBackEventReference(Me, "#").Replace("'#'", "ea") & "},"))
29+
2530
writer.WriteLine("<script>")
2631
writer.WriteLine("Vue.component('" & Name & "'," & opt & ");")
2732
writer.WriteLine("</script>")
@@ -35,4 +40,8 @@ Public Class Component
3540
REM nothing
3641
End Sub
3742

43+
Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
44+
RaiseEvent PostBack(eventArgument)
45+
End Sub
46+
3847
End Class

src/VueFilesToJS.vb

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
Private SquashWS As Boolean
55
Private FileReadCallback As Action(Of String) = Nothing
66

7-
Public Shared Function Compile(wsRootPath As String, sourceFile As String, Optional squashWS As Boolean = True, Optional rootFileContent As String = Nothing, Optional fileReadCallback As Action(Of String) = Nothing) As String
7+
Public Shared Function Compile(wsRootPath As String, sourceFile As String, embedExtra As String, Optional squashWS As Boolean = True, Optional rootFileContent As String = Nothing, Optional fileReadCallback As Action(Of String) = Nothing) As String
88
If wsRootPath.EndsWith("\") Then wsRootPath = wsRootPath.Substring(0, wsRootPath.Length - 1)
99
Dim inst = New VueFilesToJS With {.WsRoot = wsRootPath, .SquashWS = squashWS, .FileReadCallback = fileReadCallback}
10-
Return inst.ProcRoot(sourceFile, rootFileContent)
10+
Return inst.ProcRoot(sourceFile, rootFileContent, embedExtra)
1111
End Function
1212

1313
Private Sub New()
1414
REM private constructor so only Compile function can create instance
1515
End Sub
1616

17-
Private Function ProcRoot(vueFile As String, FileContent As String) As String
17+
Private Function ProcRoot(vueFile As String, FileContent As String, embedExtra As String) As String
1818
Dim res = ParseVueFile(vueFile, Nothing, FileContent)
1919

2020
Dim sb As New System.Text.StringBuilder
@@ -25,6 +25,7 @@
2525
Next
2626

2727
sb.AppendLine(" return {" & vbCrLf &
28+
embedExtra &
2829
" template: " & JSStringEncode(res.Template) & "," & vbCrLf &
2930
res.Script.Substring(1).Trim & ";")
3031
sb.AppendLine("}")

0 commit comments

Comments
 (0)