Skip to content

Commit fcafe91

Browse files
committed
Adds vue Hooks + Http methods (delete, put...)
1 parent 3457b28 commit fcafe91

File tree

4 files changed

+188
-4
lines changed

4 files changed

+188
-4
lines changed

src/main/java/io/github/jeemv/springboot/vuejs/AbstractVueJS.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.github.jeemv.springboot.vuejs;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
import io.github.jeemv.springboot.vuejs.parts.VueComputeds;
47
import io.github.jeemv.springboot.vuejs.parts.VueData;
8+
import io.github.jeemv.springboot.vuejs.parts.VueHook;
59
import io.github.jeemv.springboot.vuejs.parts.VueMethods;
610
import io.github.jeemv.springboot.vuejs.parts.VueWatcher;
711
import io.github.jeemv.springboot.vuejs.parts.VueWatchers;
@@ -11,12 +15,14 @@ public abstract class AbstractVueJS {
1115
protected VueMethods methods;
1216
protected VueComputeds computed;
1317
protected VueWatchers watchers;
18+
protected Map<String, VueHook> hooks;
1419

1520
public AbstractVueJS() {
1621
data=new VueData();
1722
methods=new VueMethods();
1823
computed=new VueComputeds();
1924
watchers=new VueWatchers();
25+
hooks=new HashMap<>();
2026
}
2127

2228
protected String wrapScript(String script) {
@@ -38,6 +44,14 @@ public void addData(String key,Object value) {
3844
data.put(key, value);
3945
}
4046

47+
/**
48+
* Declare a data
49+
* @param key the name of the data
50+
*/
51+
public void addData(String key) {
52+
data.put(key, "");
53+
}
54+
4155
/**
4256
* Adds a method
4357
* @param name the method name
@@ -184,6 +198,78 @@ public void addDeepWatcher(String variableName,String...handlers) {
184198
this.addWatcher(variableName, true, false, handlers);
185199
}
186200

201+
protected void addHook(String name,String body) {
202+
hooks.put(name, new VueHook(body));
203+
}
204+
205+
/**
206+
* Adds code (body) for the beforeCreate hook
207+
* @param body the code to execute
208+
*/
209+
public void onBeforeCreate(String body) {
210+
addHook("beforeCreate", body);
211+
}
212+
213+
/**
214+
* Adds code (body) for the created hook
215+
* @param body the code to execute
216+
*/
217+
public void onCreated(String body) {
218+
addHook("created", body);
219+
}
220+
221+
/**
222+
* Adds code (body) for the beforeMount hook
223+
* @param body the code to execute
224+
*/
225+
public void onBeforeMount(String body) {
226+
addHook("beforeMount", body);
227+
}
228+
229+
/**
230+
* Adds code (body) for the mounted hook
231+
* @param body the code to execute
232+
*/
233+
public void onMounted(String body) {
234+
addHook("mounted", body);
235+
}
236+
237+
/**
238+
* Adds code (body) for the beforeUpdate hook
239+
* @param body the code to execute
240+
*/
241+
public void onBeforeUpdate(String body) {
242+
addHook("beforeUpdate", body);
243+
}
244+
245+
/**
246+
* Adds code (body) for the updated hook
247+
* @param body the code to execute
248+
*/
249+
public void onUpdated(String body) {
250+
addHook("updated", body);
251+
}
252+
253+
/**
254+
* Adds code (body) for the beforeDestroy hook
255+
* @param body the code to execute
256+
*/
257+
public void onBeforeDestroy(String body) {
258+
addHook("beforeDestroy", body);
259+
}
260+
261+
/**
262+
* Adds code (body) for the destroyed hook
263+
* @param body the code to execute
264+
*/
265+
public void onDestroyed(String body) {
266+
addHook("destroyed", body);
267+
}
268+
269+
public Map<String, VueHook> getHooks() {
270+
return hooks;
271+
}
272+
187273
/**
188274
* Returns the generated script creating the instance
189275
* @return the generated script (javascript)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.jeemv.springboot.vuejs.parts;
2+
3+
public class VueHook {
4+
private String body;
5+
6+
public VueHook(String body) {
7+
this.body=body;
8+
}
9+
public String getBody() {
10+
return body;
11+
}
12+
13+
public void setBody(String body) {
14+
this.body = body;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "function(){"+body+"}";
20+
}
21+
}

src/main/java/io/github/jeemv/springboot/vuejs/utilities/Http.java

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,35 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44

5+
/**
6+
* Http class for Http requests
7+
* @author jcheron
8+
* @version 1.0.1
9+
*/
510
public class Http {
611

712
public static String submitForm(String formRef,String action,String successCallback,String errorCallback) {
813
String result= "let form=this.$refs['"+formRef+"'];"+
9-
"let formData=form.model;"+request("form.$vnode.data.attrs.method", action, "formData", successCallback,errorCallback);
14+
"let formData=form.model;"+request("form.$vnode.data.attrs.method.toLowerCase()", action, "formData", successCallback,errorCallback);
1015
return result;
1116
}
1217

1318
public static String submitForm(String formRef,String action,String successCallback) {
1419
return submitForm(formRef, action, successCallback, null);
1520
}
1621

22+
/**
23+
* Submits a form (form action must be provided on the html form)
24+
* @param formRef the ref attribute of the form
25+
* @param successCallback the javascript code to execute if success
26+
* @return the generated javascript code
27+
*/
1728
public static String submitForm(String formRef,String successCallback) {
1829
return Http.submitForm(formRef, "form.$vnode.data.attrs.action", successCallback,null);
1930
}
2031

2132
public static String request(String method,String url,Object data,String successCallback,String errorCallback){
22-
if(!url.contains("$")) {
33+
if(!url.contains("$")&& !url.startsWith("this")) {
2334
url="'"+url+"'";
2435
}
2536
if(!(data instanceof String)) {
@@ -42,14 +53,70 @@ public static String request(String method,String url,Object data,String success
4253
return request(method, url, data, successCallback, null);
4354
}
4455

45-
public static String get(String url,String successCallback,String errorCallback) {
46-
return request("'get'", url, "{}", successCallback, errorCallback);
56+
public static String get(String url,Object data,String successCallback,String errorCallback) {
57+
return request("'get'", url, data, successCallback, errorCallback);
58+
}
59+
60+
public static String get(String url,Object data,String successCallback) {
61+
return request("'get'", url, data, successCallback, null);
4762
}
4863

4964
public static String get(String url,String successCallback) {
5065
return request("'get'", url, "{}", successCallback, null);
5166
}
5267

68+
public static String get(String url,String successCallback,String errorCallback) {
69+
return request("'get'", url, "{}", successCallback, errorCallback);
70+
}
71+
72+
public static String delete(String url,Object data,String successCallback,String errorCallback) {
73+
return request("'delete'", url, data, successCallback, errorCallback);
74+
}
75+
76+
public static String delete(String url,Object data,String successCallback) {
77+
return request("'delete'", url, data, successCallback, null);
78+
}
79+
80+
public static String delete(String url,String successCallback) {
81+
return request("'delete'", url, "{}", successCallback, null);
82+
}
83+
84+
public static String delete(String url,String successCallback,String errorCallback) {
85+
return request("'delete'", url, "{}", successCallback, errorCallback);
86+
}
87+
88+
public static String put(String url,Object data,String successCallback,String errorCallback) {
89+
return request("'put'", url, data, successCallback, errorCallback);
90+
}
91+
92+
public static String put(String url,Object data,String successCallback) {
93+
return request("'put'", url, data, successCallback, null);
94+
}
95+
96+
public static String put(String url,String successCallback) {
97+
return request("'put'", url, "{}", successCallback, null);
98+
}
99+
100+
public static String put(String url,String successCallback,String errorCallback) {
101+
return request("'put'", url, "{}", successCallback, errorCallback);
102+
}
103+
104+
public static String patch(String url,Object data,String successCallback,String errorCallback) {
105+
return request("'patch'", url, data, successCallback, errorCallback);
106+
}
107+
108+
public static String patch(String url,Object data,String successCallback) {
109+
return request("'patch'", url, data, successCallback, null);
110+
}
111+
112+
public static String patch(String url,String successCallback) {
113+
return request("'patch'", url, "{}", successCallback, null);
114+
}
115+
116+
public static String patch(String url,String successCallback,String errorCallback) {
117+
return request("'patch'", url, "{}", successCallback, errorCallback);
118+
}
119+
53120
public static String post(String url,Object data,String successCallback,String errorCallback) {
54121
return request("'post'", url, data, successCallback, errorCallback);
55122
}
@@ -58,6 +125,10 @@ public static String post(String url,Object data,String successCallback) {
58125
return request("'post'", url, data, successCallback, null);
59126
}
60127

128+
public static String post(String url,String successCallback) {
129+
return request("'post'", url, "{}", successCallback, null);
130+
}
131+
61132
public static String setRequestHeader(String key, String value) {
62133
return "this.$http.headers.set('"+key+"', '"+value+"');";
63134
}

src/main/java/io/github/jeemv/springboot/vuejs/utilities/Serialization.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.databind.SerializerProvider;
99

1010
import io.github.jeemv.springboot.vuejs.AbstractVueJS;
11+
import io.github.jeemv.springboot.vuejs.parts.VueHook;
1112
import io.github.jeemv.springboot.vuejs.parts.VuePart;
1213

1314
public class Serialization {
@@ -47,5 +48,10 @@ public static void serializeVueElements(AbstractVueJS value, JsonGenerator gen,
4748
gen.writeFieldName("watch");
4849
gen.writeRawValue(value.getWatchers()+"");
4950
}
51+
52+
for(Map.Entry<String, VueHook> entry:value.getHooks().entrySet()) {
53+
gen.writeFieldName(entry.getKey());
54+
gen.writeRawValue(entry.getValue()+"");
55+
}
5056
}
5157
}

0 commit comments

Comments
 (0)