vue-async-methods
Advanced tools
Comparing version 0.3.1 to 0.4.0
25
index.js
@@ -6,2 +6,6 @@ 'use strict' | ||
options = options || {} | ||
options.getComputedName = options.getComputedName || function (vm, funcName) { | ||
var withoutPrefix = funcName.replace(/^(fetch|get|load)/, '') | ||
return withoutPrefix.slice(0, 1).toLowerCase() + withoutPrefix.slice(1) | ||
} | ||
@@ -40,3 +44,3 @@ function isEmpty(val) { | ||
if (result && result.then) { | ||
return result.then(function(res) { | ||
return vm[funcName].promise = result.then(function(res) { | ||
vm[funcName].isPending = false | ||
@@ -55,3 +59,3 @@ vm[funcName].isResolved = true | ||
vm[funcName].rejectedWith = err | ||
if (isFunction(options.onError)) { | ||
@@ -65,3 +69,3 @@ options.onError(err, vm, funcName, args) | ||
// always return a promise for consistency | ||
return new Promise(function(resolve) { | ||
return vm[funcName].promise = new Promise(function(resolve) { | ||
var empty = isEmpty(result) | ||
@@ -76,3 +80,3 @@ vm[funcName].resolvedWithEmpty = empty | ||
// always return a promise for consistency | ||
return new Promise(function(resolve, reject) { | ||
return vm[funcName].promise = new Promise(function(resolve, reject) { | ||
vm[funcName].isPending = false | ||
@@ -99,2 +103,3 @@ vm[funcName].isRejected = true | ||
execute: wrapMethod(this.$options.asyncMethods[key], this, key), | ||
promise: null, | ||
isCalled: false, | ||
@@ -109,2 +114,14 @@ isPending: false, | ||
}) | ||
// add computed | ||
if (options.createComputed) { | ||
this.$options.computed = this.$options.computed || {} | ||
var computedName = options.getComputedName(this, key) | ||
if (!computedName.length){ | ||
throw new Error('computed name for method ' + key + ' is empty, return a non zero length string') | ||
} | ||
this.$options.computed[computedName] = () => { | ||
return this[key].resolvedWith | ||
} | ||
} | ||
} | ||
@@ -111,0 +128,0 @@ } |
{ | ||
"name": "vue-async-methods", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Vue async methods support", | ||
@@ -28,2 +28,3 @@ "license": "MIT", | ||
"chai": "^4.1.2", | ||
"decache": "^4.1.0", | ||
"eslint": "^4.7.0", | ||
@@ -35,2 +36,3 @@ "eslint-config-standard": "^10.2.1", | ||
"nyc": "^11.0.0", | ||
"sinon": "^3.3.0", | ||
"vue": "^2.4.4", | ||
@@ -37,0 +39,0 @@ "xo": "^0.18.2" |
@@ -16,5 +16,23 @@ # vue-async-methods [![Build Status](https://travis-ci.org/mokkabonna/vue-async-methods.svg?branch=master)](https://travis-ci.org/mokkabonna/vue-async-methods) | ||
Vue.use(AsyncMethods) | ||
Vue.use(AsyncMethods [,options]) | ||
``` | ||
### Options | ||
#### createComputed | ||
default `false`, if true: creates computeds that proxies fetchArticle.resolvedWith | ||
#### getComputedName | ||
A function that should return the name of the desired computed if createComputed is `true` | ||
default | ||
```js | ||
// turns "fetchArticle", "getArticle" or "loadArticle" into "article" computed | ||
function (vm, funcName) { | ||
var withoutPrefix = funcName.replace(/^(fetch|get|load)/, '') | ||
return withoutPrefix.slice(0, 1).toLowerCase() + withoutPrefix.slice(1) | ||
} | ||
``` | ||
Now you can define async methods on your vm: | ||
@@ -25,3 +43,3 @@ | ||
asyncMethods: { | ||
fetchData() { | ||
fetchArticle() { | ||
return ajax('http://example.com/data.json') //must return a promise | ||
@@ -36,11 +54,13 @@ } | ||
```js | ||
fetchData.execute // executes the method | ||
fetchData.isCalled // false until first called | ||
fetchData.isPending | ||
fetchData.isResolved | ||
fetchData.isRejected | ||
fetchData.resolvedWith | ||
fetchData.resolvedWithEmpty //empty means empty object or empty array | ||
fetchData.resolvedWithSomething //opposite of empty | ||
fetchData.rejectedWith //Error object | ||
article // this is a computed that aliases fetchArticle.resolvedWith | ||
fetchArticle.execute // executes the method | ||
fetchArticle.promise // the current or last promise | ||
fetchArticle.isCalled // false until first called | ||
fetchArticle.isPending | ||
fetchArticle.isResolved | ||
fetchArticle.isRejected | ||
fetchArticle.resolvedWith | ||
fetchArticle.resolvedWithEmpty //empty means empty object or empty array | ||
fetchArticle.resolvedWithSomething //opposite of empty | ||
fetchArticle.rejectedWith //Error object | ||
``` | ||
@@ -50,10 +70,10 @@ | ||
```html | ||
<button type="button" @click="fetchData.execute">Load data</button> | ||
<div v-if="!fetchData.isCalled">Click button to load data</div> | ||
<div v-if="fetchData.isPending">Loading data...</div> | ||
<button type="button" @click="fetchArticle.execute">Load data</button> | ||
<div v-if="!fetchArticle.isCalled">Click button to load data</div> | ||
<div v-if="fetchArticle.isPending">Loading data...</div> | ||
<div v-if="fetchData.isResolved"> | ||
<div v-if="fetchData.resolvedWithSomething"> | ||
<div v-if="fetchArticle.isResolved"> | ||
<div v-if="fetchArticle.resolvedWithSomething"> | ||
<ul> | ||
<li v-for="item in fetchData.resolvedWith"> | ||
<li v-for="item in fetchArticle.resolvedWith"> | ||
{{item.name}} | ||
@@ -63,3 +83,3 @@ </li> | ||
</div> | ||
<div v-if="fetchData.resolvedWithEmpty"> | ||
<div v-if="fetchArticle.resolvedWithEmpty"> | ||
Empty list returned | ||
@@ -69,4 +89,4 @@ </div> | ||
<div v-if="fetchData.isRejected"> | ||
<div v-if="fetchData.rejectedWith"> | ||
<div v-if="fetchArticle.isRejected"> | ||
<div v-if="fetchArticle.rejectedWith"> | ||
Could not load data due to an error. Details: {{fetchData.rejectedWith.message}} | ||
@@ -73,0 +93,0 @@ </div> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8243
110
94
12