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

Commit

Permalink
Merge pull request #15 from aarondfrancis/2.0
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
aarondfrancis authored Oct 12, 2017
2 parents 59eb94b + 943645c commit a65b563
Show file tree
Hide file tree
Showing 12 changed files with 723 additions and 17,739 deletions.
17,103 changes: 0 additions & 17,103 deletions dist/vue-model.js

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-model",
"version": "0.0.3",
"version": "2.0.3",
"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.",
"main": "src/VueModel.js",
"scripts": {
Expand All @@ -23,6 +23,10 @@
},
"homepage": "https://github.com/aarondfrancis/vue-model#readme",
"dependencies": {
"lodash": "^4.13.1"
"axios": "^0.16.2",
"lodash": "^4.17.4",
"vue": "^2.0",
"promise": "^8.0.1",
"promise.prototype.finally": "^3.0.1"
}
}
125 changes: 0 additions & 125 deletions src/DataPipeline.js

This file was deleted.

102 changes: 102 additions & 0 deletions src/Defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var _ = require('lodash');

module.exports = {
// Names of any attributes that you want to be reactive
attributes: [],

http: {
baseRoute: null,

// The _minimum_ amount of time that a successful ajax
// request should take. If the request fails it will
// return immediately, but if it's successful it won't
// return until `takeAtLeast` milliseconds have passed
takeAtLeast: 0,

// Return an axios request configuration object
// https://github.com/axios/axios#request-config
axios(configuration, action, definition, runtimeArgs) {
return configuration;
},

// Turn an axios response into the model data
getDataFromResponse(response) {
return response.data.data;
},

// Where you want your errors to live. You can use a
// dot delimited path or put them at the top level
errorKey: 'http.errors',

// Determine whether an error response is a model
// validation error. 422 is the correct status code,
// so if you use Laravel, no need to update this.
isValidationError (error) {
return _.get(error, 'response.status') === 422;
},

// The error object should have the field names
// as the keys and an array of errors as the
// values. This default is set for Laravel 5.5.
getErrorsFromResponse(response) {
return response.data.errors;
},

// Base defaults for every action
actionDefaults: {
// Apply the response data to the model
apply: false,

// The server validates this request and could
// return a validation error in response
validation: true,

// Modify what data we should send to the server
data: {
// only: [],
// with: [],
// without: [],
// custom: function(data, definition) {
// return data;
// }
}
},

// Default HTTP Actions that every model gets. You can
// set an action to `false` to disable it in a model
actions: {
index: {
method: 'GET',
route: '',
data: {
only: []
}
},
store: {
method: 'POST',
route: '',
},
fetch: {
method: 'GET',
route: '{id}',
apply: true,
data: {
only: []
}
},
update: {
method: 'PUT',
route: '{id}',
apply: true,
},
destroy: {
method: 'DELETE',
route: '{id}',
data: {
only: []
}
}
},
},

};
78 changes: 78 additions & 0 deletions src/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var _ = require('lodash');
var Vue = require('vue');

module.exports = class Errors {

constructor(errors) {
this.set(errors);
}

set (errors) {
if (_.isEmpty(errors)) {
errors = {};
}

if (!_.isPlainObject(errors)) {
throw new Error('ModelErrors data must be a plain object');
}

for (const [field, messages] of Object.entries(errors)) {
errors[field] = _.castArray(messages);
}

Vue.set(this, 'errors', errors);
}

all() {
return this.errors;
}

flat() {
var flat = [];

for (const [field, messages] of Object.entries(this.errors)) {
for (var i = 0; i < messages.length; i++) {
flat.push({
field: field,
message: messages[i]
});
}
}

return flat;
}

any() {
return this.flat().length > 0;
}

has(field) {
return !!_.get(this.errors, field, []).length;
}

get (field) {
return _.get(this.errors, field, []);
}

first(field) {
return _.head(this.get(field));
}

clear(field) {
if (_.isUndefined(field)) {
this.errors = {};
return;
}

this.errors[field] = [];
}

add(field, message) {
return this.push(field, message);
}

push(field, message) {
Vue.set(this.errors, field, this.get(field).concat(_.castArray(message)));
}

}
Loading

0 comments on commit a65b563

Please sign in to comment.