Skip to content

Commit

Permalink
#176 Make a setModel as async
Browse files Browse the repository at this point in the history
  • Loading branch information
amsik committed Nov 21, 2019
1 parent 02dff2f commit 192c2b4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 46 deletions.
3 changes: 3 additions & 0 deletions demo/pages/async.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
}
return data
}).catch(e => console.log(e))
},
onFetchError(error) {
console.error(error)
}
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "liquor-tree",
"description": "A Vue.js tree component.",
"version": "0.2.67",
"version": "0.2.68",
"author": "Kostiantyn <[email protected]>",
"library": "LiquorTree",
"homepage": "https://amsik.github.io/liquor-tree/",
Expand Down
96 changes: 51 additions & 45 deletions src/lib/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ export default class Tree {

return result
.then(data => {
return this.parse(data, this.options.modelParse)
try {
return this.parse(data, this.options.modelParse)
} catch (e) {
throw new Error(e)
}
})
.catch(this.options.onFetchError)
}
Expand All @@ -236,61 +240,64 @@ export default class Tree {
}

setModel (data) {
this.model = this.parse(data, this.options.modelParse)

/* eslint-disable */
requestAnimationFrame(_ => {
this.vm.model = this.model
})
/* eslint-enable */

/**
* VueJS transform properties to reactives when constructor is running
* And we lose List object (extended from Array)
*/
this.selectedNodes = new List()
this.checkedNodes = new List()
return new Promise(resolve => {
this.model = this.parse(data, this.options.modelParse)

recurseDown(this.model, node => {
node.tree = this
/* eslint-disable */
requestAnimationFrame(_ => {
this.vm.model = this.model
resolve()
})
/* eslint-enable */

if (node.selected()) {
this.selectedNodes.add(node)
}
/**
* VueJS transform properties to reactives when constructor is running
* And we lose List object (extended from Array)
*/
this.selectedNodes = new List()
this.checkedNodes = new List()

if (node.checked()) {
this.checkedNodes.add(node)
recurseDown(this.model, node => {
node.tree = this

if (node.parent) {
node.parent.refreshIndeterminateState()
if (node.selected()) {
this.selectedNodes.add(node)
}
}

if (this.options.autoDisableChildren && node.disabled()) {
node.recurseDown(child => {
child.state('disabled', true)
})
}
})
if (node.checked()) {
this.checkedNodes.add(node)

if (!this.options.multiple && this.selectedNodes.length) {
const top = this.selectedNodes.top()
if (node.parent) {
node.parent.refreshIndeterminateState()
}
}

this.selectedNodes.forEach(node => {
if (top !== node) {
node.state('selected', false)
if (this.options.autoDisableChildren && node.disabled()) {
node.recurseDown(child => {
child.state('disabled', true)
})
}
})

this.selectedNodes
.empty()
.add(top)
}
if (!this.options.multiple && this.selectedNodes.length) {
const top = this.selectedNodes.top()

// Nodes can't be selected on init. By it's possible to select through API
if (this.options.checkOnSelect && this.options.checkbox) {
this.unselectAll()
}
this.selectedNodes.forEach(node => {
if (top !== node) {
node.state('selected', false)
}
})

this.selectedNodes
.empty()
.add(top)
}

// Nodes can't be selected on init. By it's possible to select through API
if (this.options.checkOnSelect && this.options.checkbox) {
this.unselectAll()
}
})
}

recurseDown (node, fn) {
Expand Down Expand Up @@ -720,7 +727,6 @@ export default class Tree {
try {
return TreeParser.parse(data, this, options)
} catch (e) {
console.error(e)
return []
}
}
Expand Down

0 comments on commit 192c2b4

Please sign in to comment.