Skip to content

Commit 3b8d695

Browse files
committed
remove axios dependencies, update readme, git ignore files
1 parent d7a1ca4 commit 3b8d695

File tree

7 files changed

+123
-146
lines changed

7 files changed

+123
-146
lines changed

.gitignore

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
yarn-error.log
2-
/node_modules
1+
.DS_Store
2+
node_modules
3+
npm-debug.log
4+
.vscode
5+
.coveralls.yml
6+
dist/build.js
7+
dist/build.js.map
8+
test/unit/coverage
39
yarn-debug.log*
4-
.yarn-integrity
10+
yarn-error.log*
11+
.idea
12+
src/todos

.npmignore

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@
1010
config.gypi
1111
CVS
1212
npm-debug.log
13-
13+
.DS_Store
14+
node_modules
15+
npm-debug.log
16+
.vscode
17+
.coveralls.yml
18+
dist/build.js
19+
dist/build.js.map
20+
test/unit/coverage
21+
yarn-debug.log*
22+
yarn-error.log*
23+
.idea

package-lock.json

+27-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "vue-autorequest",
3-
"version": "1.0.0",
4-
"description": "Self-loading Vue Component",
3+
"version": "1.1.0",
4+
"description": "Self-loading Vue Component with zero dependencies",
55
"main": "vue-autorequest.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
99
"keywords": [
1010
"vue",
11-
"axios",
11+
"rails",
12+
"webpacker",
1213
"component",
1314
"vue-component",
1415
"vue-on-rails"
@@ -18,8 +19,7 @@
1819
"email": "[email protected]",
1920
"url": "http://github.com/ytbryan/vue-autorequest",
2021
"dependencies": {
21-
"axios": "^0.18.0",
22-
"vue": "^2.5.16"
22+
"vue": "^2.5.17"
2323
},
2424
"devDependencies": {
2525
"babel-helper-vue-jsx-merge-props": "^2.0.3",

readme.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Vue-Autorequest
1+
# Welcome to Vue-Autorequest
22

3-
`vue-autorequest` is a Vue plugin that enables the automatic loading feature of the component and reduces duplicates of `axios.get` across your Vue instance's life-cycle hooks. It is designed to simplify small to medium components. It is created with [Vue on Rails](vueonrails.com) in mind.
3+
Autorequest is a Vue component plugin to load the component part with initial data. It uses XMLHttpRequest. It is created with [Vue on Rails](http://vueonrails.com) in mind.
44

5-
`vue-autorequest` is not meant for complex components with multiple endpoints.
5+
> `vue-autorequest` is designed for a single endpoint. And it is probably not for complex components with multiple endpoints.
66
7-
Instead of writing the following `axios.get` request at `mounted` function:
7+
### Lesser `axios.get` on more component parts
8+
9+
One of autorequest's goals is to reduce the `axios.get` spinkles and give you a smaller component part. A common axios.get spinkle is like the one below.
810

911
```javascript
1012
mounted: function(){
@@ -18,9 +20,10 @@ mounted: function(){
1820
}
1921
```
2022

21-
We could simply set the endpoint at Vue's `data` at the `this.mounted.url`
23+
Autorequest allows me to write the same data loading code like this:
2224

2325
```javascript
26+
//1
2427
data: function(){
2528
return {
2629
mounted: { url: 'https://yesno.wtf/api'}
@@ -29,11 +32,13 @@ data: function(){
2932
```
3033
> Replace `mounted` with `created` or `updated`
3134
32-
Receive response or catch error at its corresponding `watch` method onMounted().
35+
At the `watch` method, receive response or catch error.
36+
3337
```javascript
38+
//2
3439
watch: {
3540
onMounted:function(res){
36-
console.log(res.data)
41+
console.log({res})
3742
}
3843
}
3944
```

vue-autorequest.js

+37-33
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/*!
22
* Vue-Autorequest.js
3-
* v1.0.0
3+
* v1.1.0
44
* (c) Bryan Lim; MIT License
55
*/
66

7-
import axios from 'axios'
8-
97
const Autorequest = {
108
install(Vue) {
119
Vue.mixin({
@@ -20,41 +18,47 @@ const Autorequest = {
2018
}
2119
},
2220
updated: function(){
23-
var appl = this
24-
if(this.updated.url != ""){
25-
axios.get(this.updated.url)
26-
.then(function(response){
27-
appl.onUpdated = response
28-
})
29-
.catch(function(err){
30-
appl.onUpdated = err
31-
})
32-
}
21+
this.request_for("updated", this.updated.url)
3322
},
3423
mounted: function(){
35-
var appl = this
36-
if(this.mounted.url != ""){
37-
axios.get(this.mounted.url)
38-
.then(function(response){
39-
appl.onMounted = response
40-
})
41-
.catch(function(err){
42-
appl.onMounted = err
43-
})
44-
}
24+
this.request_for("mounted", this.mounted.url)
4525
},
4626
created: function(){
47-
var appl = this
48-
if(this.created.url != ""){
49-
axios.get(this.created.url)
50-
.then(function(response){
51-
appl.onCreated = response
52-
})
53-
.catch(function(err){
54-
appl.onCreated = err
55-
})
27+
this.request_for("created", this.created.url)
28+
},
29+
methods: {
30+
request_for: function(stage, endpoint){
31+
if(endpoint == "") return
32+
var appl = this
33+
var request = new XMLHttpRequest();
34+
request.open("GET", endpoint, true)
35+
request.onreadystatechange = function() {
36+
if (this.readyState == 4 && this.status == 200) {
37+
if(this.response == undefined) return
38+
var reply = JSON.parse(this.response)
39+
if(stage == "updated"){
40+
appl.onUpdated = reply
41+
}else if(stage == "mounted"){
42+
appl.onMounted = reply
43+
} else if(stage == "created") {
44+
appl.onCreated = reply
45+
}
46+
}
47+
}
48+
request.onerror = function () {
49+
if(this.err == undefined) return
50+
var err = JSON.parse(this.err)
51+
if(stage == "updated"){
52+
appl.onUpdated = err
53+
} else if(stage == "mounted"){
54+
appl.onMounted = err
55+
} else if(stage == "created") {
56+
appl.onCreated = err
57+
}
58+
}
59+
request.send()
5660
}
57-
}
61+
}
5862
})
5963
}
6064
};

0 commit comments

Comments
 (0)