Skip to content
This repository was archived by the owner on Sep 6, 2020. It is now read-only.

Commit a65b563

Browse files
Merge pull request #15 from aarondfrancis/2.0
2.0
2 parents 59eb94b + 943645c commit a65b563

File tree

12 files changed

+723
-17739
lines changed

12 files changed

+723
-17739
lines changed

dist/vue-model.js

Lines changed: 0 additions & 17103 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-model",
3-
"version": "0.0.3",
3+
"version": "2.0.3",
44
"description": "A Javascript plugin for the excellent Vue.js framework that gives you the ability to transform your plain data into rich models with built-in and customizable HTTP actions.",
55
"main": "src/VueModel.js",
66
"scripts": {
@@ -23,6 +23,10 @@
2323
},
2424
"homepage": "https://github.com/aarondfrancis/vue-model#readme",
2525
"dependencies": {
26-
"lodash": "^4.13.1"
26+
"axios": "^0.16.2",
27+
"lodash": "^4.17.4",
28+
"vue": "^2.0",
29+
"promise": "^8.0.1",
30+
"promise.prototype.finally": "^3.0.1"
2731
}
2832
}

src/DataPipeline.js

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/Defaults.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var _ = require('lodash');
2+
3+
module.exports = {
4+
// Names of any attributes that you want to be reactive
5+
attributes: [],
6+
7+
http: {
8+
baseRoute: null,
9+
10+
// The _minimum_ amount of time that a successful ajax
11+
// request should take. If the request fails it will
12+
// return immediately, but if it's successful it won't
13+
// return until `takeAtLeast` milliseconds have passed
14+
takeAtLeast: 0,
15+
16+
// Return an axios request configuration object
17+
// https://github.com/axios/axios#request-config
18+
axios(configuration, action, definition, runtimeArgs) {
19+
return configuration;
20+
},
21+
22+
// Turn an axios response into the model data
23+
getDataFromResponse(response) {
24+
return response.data.data;
25+
},
26+
27+
// Where you want your errors to live. You can use a
28+
// dot delimited path or put them at the top level
29+
errorKey: 'http.errors',
30+
31+
// Determine whether an error response is a model
32+
// validation error. 422 is the correct status code,
33+
// so if you use Laravel, no need to update this.
34+
isValidationError (error) {
35+
return _.get(error, 'response.status') === 422;
36+
},
37+
38+
// The error object should have the field names
39+
// as the keys and an array of errors as the
40+
// values. This default is set for Laravel 5.5.
41+
getErrorsFromResponse(response) {
42+
return response.data.errors;
43+
},
44+
45+
// Base defaults for every action
46+
actionDefaults: {
47+
// Apply the response data to the model
48+
apply: false,
49+
50+
// The server validates this request and could
51+
// return a validation error in response
52+
validation: true,
53+
54+
// Modify what data we should send to the server
55+
data: {
56+
// only: [],
57+
// with: [],
58+
// without: [],
59+
// custom: function(data, definition) {
60+
// return data;
61+
// }
62+
}
63+
},
64+
65+
// Default HTTP Actions that every model gets. You can
66+
// set an action to `false` to disable it in a model
67+
actions: {
68+
index: {
69+
method: 'GET',
70+
route: '',
71+
data: {
72+
only: []
73+
}
74+
},
75+
store: {
76+
method: 'POST',
77+
route: '',
78+
},
79+
fetch: {
80+
method: 'GET',
81+
route: '{id}',
82+
apply: true,
83+
data: {
84+
only: []
85+
}
86+
},
87+
update: {
88+
method: 'PUT',
89+
route: '{id}',
90+
apply: true,
91+
},
92+
destroy: {
93+
method: 'DELETE',
94+
route: '{id}',
95+
data: {
96+
only: []
97+
}
98+
}
99+
},
100+
},
101+
102+
};

src/Errors.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var _ = require('lodash');
2+
var Vue = require('vue');
3+
4+
module.exports = class Errors {
5+
6+
constructor(errors) {
7+
this.set(errors);
8+
}
9+
10+
set (errors) {
11+
if (_.isEmpty(errors)) {
12+
errors = {};
13+
}
14+
15+
if (!_.isPlainObject(errors)) {
16+
throw new Error('ModelErrors data must be a plain object');
17+
}
18+
19+
for (const [field, messages] of Object.entries(errors)) {
20+
errors[field] = _.castArray(messages);
21+
}
22+
23+
Vue.set(this, 'errors', errors);
24+
}
25+
26+
all() {
27+
return this.errors;
28+
}
29+
30+
flat() {
31+
var flat = [];
32+
33+
for (const [field, messages] of Object.entries(this.errors)) {
34+
for (var i = 0; i < messages.length; i++) {
35+
flat.push({
36+
field: field,
37+
message: messages[i]
38+
});
39+
}
40+
}
41+
42+
return flat;
43+
}
44+
45+
any() {
46+
return this.flat().length > 0;
47+
}
48+
49+
has(field) {
50+
return !!_.get(this.errors, field, []).length;
51+
}
52+
53+
get (field) {
54+
return _.get(this.errors, field, []);
55+
}
56+
57+
first(field) {
58+
return _.head(this.get(field));
59+
}
60+
61+
clear(field) {
62+
if (_.isUndefined(field)) {
63+
this.errors = {};
64+
return;
65+
}
66+
67+
this.errors[field] = [];
68+
}
69+
70+
add(field, message) {
71+
return this.push(field, message);
72+
}
73+
74+
push(field, message) {
75+
Vue.set(this.errors, field, this.get(field).concat(_.castArray(message)));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)