Skip to content

Commit 05a4086

Browse files
committed
lot of reworks. ready for use
1 parent 2cdf82b commit 05a4086

File tree

7 files changed

+159
-76
lines changed

7 files changed

+159
-76
lines changed

LICENSE.mit

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Cody Mercer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# vue-laravel-forms (NOT READY FOR USE)
1+
# vue-laravel-forms
22
Form helpers for Laravel backed Vue.js projects.
33

44

@@ -31,7 +31,8 @@ import { Form, FormErrors, Http } from 'vue-laravel-forms'
3131
window.AppForm = Form;
3232
window.AppFormErrors = FormErrors;
3333
34-
_.extend(App, Http)
34+
_.extend(App, new Http()) // Vue.http config not needed
35+
_.extend(App, new Http(Vue.http)) // Vue.http config needed
3536
```
3637

3738
## Usage
@@ -110,6 +111,7 @@ _Components installed Separately_
110111
* `App.putForm(uri, form)`
111112
* `App.sendForm(method, uri, form)`
112113

114+
113115
### Template Helpers
114116
##### Check a field for errors
115117
```
@@ -145,17 +147,11 @@ Vue.component('user-registration-form', {
145147
146148
methods: {
147149
getFieldClass(field) {
148-
return {
149-
'form-group': true,
150-
'email-field': this.field == 'email'
151-
}
150+
return `form-group ${field}`;
152151
},
153152
154153
getFieldErrorClass(field) {
155-
return {
156-
'has-error': true,
157-
'password-error': this.field == 'password'
158-
}
154+
return `has-error ${field}-error`;
159155
}
160156
}
161157

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{
22
"name": "vue-laravel-forms",
3-
"version": "0.0.1-alpha",
3+
"version": "1.0.0-beta",
44
"description": "Form helpers for Laravel backed Vue.js projects.",
55
"main": "dist/index.js",
66
"scripts": {
77
"compile": "babel --presets=es2015 -d dist src",
88
"prepublish": "npm run compile"
99
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/cklmercer/vue-laravel-forms.git"
13+
},
1014
"keywords": [
1115
"vue",
1216
"laravel",
1317
"forms"
1418
],
1519
"author": "Cody Mercer <[email protected]>",
16-
"license": "UNLICENSED",
20+
"license": "MIT",
1721
"devDependencies": {
1822
"babel-cli": "^6.11.4",
1923
"babel-preset-es2015": "^6.13.2",

src/form.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import FormErrors from './form-errors';
2+
import { assignIn, forIn, keys, pick } from 'lodash';
23

34
class Form
45
{
@@ -8,8 +9,25 @@ class Form
89
constructor(fields) {
910
this.busy = false;
1011
this.errors = new FormErrors();
11-
this.fields = fields;
12-
this.success = false;
12+
this.initialFields = fields;
13+
this.successful = false;
14+
assignIn(this, fields);
15+
}
16+
17+
/*
18+
* Get the form's fields.
19+
*/
20+
get fields() {
21+
let fields = pick(this, keys(this.initialFields));
22+
23+
// Here we unset null fields.
24+
forIn(fields, function (value, key) {
25+
if (value == null) {
26+
delete fields[key];
27+
}
28+
})
29+
30+
return fields;
1331
}
1432

1533
/*
@@ -30,6 +48,21 @@ class Form
3048
this.successful = true;
3149
}
3250

51+
/*
52+
* Completely reset the form.
53+
*/
54+
reset() {
55+
this.resetFields();
56+
this.resetStatus();
57+
}
58+
59+
/*
60+
* Reset the fields to their initial state..
61+
*/
62+
resetFields() {
63+
keys(this.initialFields).forEach((key) => this[key] = this.initialFields[key]);
64+
}
65+
3366
/*
3467
* Reset the errors and other state for the form.
3568
*/

src/helpers.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function getFileExtension(file) {
2+
return file.name.substr(file.name.lastIndexOf('.') + 1);
3+
}
4+
5+
function isFile(field) {
6+
return Boolean(field) && Boolean(field.size);
7+
}
8+
9+
export { getFileExtension, isFile }

src/http.js

+59-49
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,66 @@
11
import { forIn } from 'lodash';
2+
import { getFileExtension, isFile } from './helpers';
23
import Vue from 'vue';
34
import VueResource from 'vue-resource';
45

56
Vue.use(VueResource)
67

7-
export default {
8-
/*
9-
* Helper method for submitting a form as a DELETE request.
10-
*/
11-
deleteForm(uri, form) {
12-
return this.sendForm('delete', uri, form);
13-
},
14-
15-
/*
16-
* Helper method for submitting a form as a POST request.
17-
*/
18-
postForm(uri, form) {
19-
let formData = new FormData;
20-
21-
forIn(form.fields, (value, key) => formData.append(key, value));
22-
23-
return this.sendForm('post', uri, form, formData);
24-
},
25-
26-
27-
/*
28-
* Helper method for submitting a form as a PUT request.
29-
*/
30-
putForm(uri, form) {
31-
return this.sendForm('put', uri, form);
32-
},
33-
34-
/*
35-
* Helper method for submitting a form.
36-
*/
37-
sendForm(method, uri, form, formData = null) {
38-
let data = formData ? formData : JSON.parse(JSON.stringify(form.fields));
39-
40-
return new Promise((resolve, reject) => {
41-
form.startProcessing();
42-
43-
Vue.http[method](uri, data)
44-
.then(response => {
45-
form.finishProcessing();
46-
resolve(response.data);
47-
})
48-
.catch(errors => {
49-
form.setErrors(errors.data);
50-
form.busy = false;
51-
52-
reject(errors.data);
53-
});
54-
});
55-
}
8+
export default function (Http = null) {
9+
return {
10+
/*
11+
* Helper method for submitting a form as a DELETE request.
12+
*/
13+
deleteForm(uri, form) {
14+
return this.sendForm('delete', uri, form);
15+
},
16+
17+
/*
18+
* Helper method for submitting a form as a POST request.
19+
*/
20+
postForm(uri, form) {
21+
let formData = new FormData;
22+
23+
forIn(form.fields, (value, key) => {
24+
if (isFile(value)) {
25+
let ext = getFileExtension(value);
26+
formData.append(key, value, `${key}.${ext}`);
27+
} else {
28+
formData.append(key, value)
29+
}
30+
});
31+
32+
return this.sendForm('post', uri, form, formData);
33+
},
34+
35+
36+
/*
37+
* Helper method for submitting a form as a PUT request.
38+
*/
39+
putForm(uri, form) {
40+
return this.sendForm('put', uri, form);
41+
},
42+
43+
/*
44+
* Helper method for submitting a form.
45+
*/
46+
sendForm(method, uri, form, formData = null) {
47+
let submitter = Http || Vue.http;
48+
let data = formData ? formData : JSON.parse(JSON.stringify(form.fields));
49+
50+
return new Promise((resolve, reject) => {
51+
form.startProcessing();
52+
53+
submitter[method](uri, data)
54+
.then(response => {
55+
form.finishProcessing();
56+
resolve(response.data);
57+
})
58+
.catch(errors => {
59+
form.setErrors(errors.data);
60+
form.busy = false;
61+
reject(errors.data);
62+
});
63+
});
64+
}
65+
}
5666
}

src/index.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import FormErrors from './form-errors';
44

55
function FormHelpers (Vue) {
66

7+
if (typeof Vue.http == 'undefined') {
8+
console.error('Please install vue-resource before using vue-laravel-forms')
9+
return;
10+
}
11+
12+
let formHelper = new Http(Vue.http);
13+
714
Object.defineProperty(Vue.prototype, '$forms', {
815
get() {
916
return {
@@ -25,28 +32,28 @@ function FormHelpers (Vue) {
2532
* Submit the given Form to the given URI via a DELETE request.
2633
*/
2734
delete(uri, form) {
28-
return Http.deleteForm(uri, form);
35+
return formHelper.deleteForm(uri, form);
2936
},
3037

3138
/*
3239
* Submit the given Form to the given URI via a POST request.
3340
*/
3441
post(uri, form) {
35-
return Http.postForm(uri, form);
42+
return formHelper.postForm(uri, form);
3643
},
3744

3845
/*
3946
* Submit the given Form to the given URI via a PUT request
4047
*/
4148
put(uri, form) {
42-
return Http.putForm(uri, form);
49+
return formHelper.putForm(uri, form);
4350
},
4451

4552
/*
4653
* Submit the given Form to the given URI using the given HTTP method.
4754
*/
4855
submit(method, uri, form, formData = null) {
49-
return Http.sendForm(method, uri, form, formData);
56+
return formHelper.sendForm(method, uri, form, formData);
5057
}
5158
}
5259
}
@@ -58,22 +65,25 @@ function FormHelpers (Vue) {
5865
* The 'beforeCreate' life-cycle hook.
5966
*/
6067
beforeCreate() {
61-
if (typeof this.$options.forms == 'object') {
62-
registerForms(this.$options.forms).bind(this);
68+
let forms = this.$options.forms;
69+
70+
if (typeof forms == 'object') {
71+
let dataIsFunction = typeof this.$options.data == 'function';
72+
let data = dataIsFunction ? this.$options.data() : this.$options.data || {};
73+
74+
for (var form in forms) {
75+
data[form] = forms[form];
76+
}
77+
78+
this.$options.data = dataIsFunction ? function () { return data } : data;
6379
}
6480
}
6581

6682
});
6783
}
6884

69-
function registerForms(forms) {
70-
for (var form in forms) {
71-
Object.defineProperty(this._data, form, forms[form]);
72-
}
73-
}
74-
7585
if (typeof window !== 'undefined' && window.Vue) {
7686
window.Vue.use(FormHelpers);
7787
}
7888

79-
export { FormHelpers, Http, Form, FormErrors }
89+
export { FormHelpers, Form, FormErrors }

0 commit comments

Comments
 (0)