Skip to content

Commit

Permalink
Allow direct call to function
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Mar 11, 2018
1 parent 7d2880c commit 7ef9a29
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -62,7 +62,8 @@ And use the following helper variables in your view:

```js
articles // this is a computed that aliases fetchArticles.resolvedWith
fetchArticles.execute // executes the method
fetchArticles //call this function to fetch the articles
fetchArticles.execute // executes the method, legacy support
fetchArticles.promise // the current or last promise
fetchArticles.isCalled // false until first called
fetchArticles.isPending
Expand All @@ -78,7 +79,7 @@ It also registers a component called `catch-async-error` that enables you to cat


```html
<button type="button" @click="fetchArticles.execute">Load data</button>
<button type="button" @click="fetchArticles">Load data</button>
<div v-if="!fetchArticles.isCalled">Click button to load data</div>
<div v-if="fetchArticles.isPending">Loading data...</div>

Expand Down
14 changes: 11 additions & 3 deletions index.js
Expand Up @@ -167,8 +167,12 @@ module.exports = {
var asyncMethods = this.$options.asyncMethods || {}

for (var key in asyncMethods) {
Vue.util.defineReactive(this, key, {
execute: wrapMethod(asyncMethods[key], this, key),
var func = wrapMethod(asyncMethods[key], this, key)

Vue.util.defineReactive(this, key, func)

var extra = {
execute: func,
promise: null,
isCalled: false,
isPending: false,
Expand All @@ -179,7 +183,11 @@ module.exports = {
resolvedWithEmpty: false,
rejectedWith: null,
handleErrorInView: false
})
}

for (var prop in extra) {
Vue.util.defineReactive(func, prop, extra[prop])
}

// add computed
if (options.createComputed) {
Expand Down
15 changes: 14 additions & 1 deletion test.js
Expand Up @@ -48,6 +48,19 @@ describe('vue-async-methods custom options', function() {

expect(create).to.throw(/Computed name for method fetch is empty/)
})

describe('direct call', function() {
var article = {}
beforeEach(function() {
var call = vm.fetchArticle()
resolvePromise(article)
return call
})

it('updates the computed', function() {
expect(vm.article).to.equal(article)
})
})

describe('when it succeds', function() {
var article = {}
Expand Down Expand Up @@ -109,7 +122,7 @@ describe('vue-async-methods default options', function() {
describe('after called', function() {
var call
beforeEach(function() {
call = vm.fetch.execute()
call = vm.fetch()
})

it('is called', function() {
Expand Down

0 comments on commit 7ef9a29

Please sign in to comment.