Permalink
Browse files

feat(paged): add loading flag

  • Loading branch information...
jasoncarreira committed Jul 6, 2017
1 parent dcefaf9 commit 6886e6b6a09e979903b094a9f0751912af250b09
Showing with 111 additions and 107 deletions.
  1. +111 −107 src/component/paged.js
@@ -10,126 +10,130 @@ import {resolvedView} from 'aurelia-view-manager';
export class Paged {
// https://github.com/aurelia/templating/issues/73, you still had to set `data` on .two-way when global
@bindable({defaultBindingMode: bindingMode.twoWay}) data = [];
@bindable({defaultBindingMode: bindingMode.twoWay}) page = 1;
@bindable({defaultBindingMode: bindingMode.twoWay}) error;
@bindable criteria
@bindable repository = null;
@bindable resource
@bindable limit = 30;
constructor(entityManager) {
this.entityManager = entityManager;
@bindable({defaultBindingMode: bindingMode.twoWay}) data = [];
@bindable({defaultBindingMode: bindingMode.twoWay}) loading = false;
@bindable({defaultBindingMode: bindingMode.twoWay}) page = 1;
@bindable({defaultBindingMode: bindingMode.twoWay}) error;
@bindable criteria
@bindable repository = null;
@bindable resource
@bindable limit = 30;
constructor(entityManager) {
this.entityManager = entityManager;
}
/**
* Attach to view
*/
attached() {
if (!this.page) {
this.page = 1;
}
/**
* Attach to view
*/
attached() {
if (!this.page) {
this.page = 1;
}
if (!this.criteria) {
this.criteria = {};
}
if (!this.criteria) {
this.criteria = {};
}
this.reloadData();
}
this.reloadData();
}
/**
* Reload data
*/
reloadData() {
this.getData();
}
/**
* Reload data
*/
reloadData() {
this.getData();
}
/**
* Check if the element property has changed
*
* @param {string} property New element property
* @param {string|{}} newVal New value
* @param {string|{}} oldVal Old value
*
* @return {boolean}
*/
isChanged(property, newVal, oldVal) {
return !this[property] || !newVal || (newVal === oldVal);
}
/**
* Check if the element property has changed
*
* @param {string} property New element property
* @param {string|{}} newVal New value
* @param {string|{}} oldVal Old value
*
* @return {boolean}
*/
isChanged(property, newVal, oldVal) {
return !this[property] || !newVal || (newVal === oldVal);
/**
* Changed page handler
*
* @param {integer} newVal New page value
* @param {integer} oldVal Old page value
*/
pageChanged(newVal, oldVal) {
if (this.isChanged('resource', newVal, oldVal)
|| this.isChanged('criteria', newVal, oldVal)
) {
return;
}
/**
* Changed page handler
*
* @param {integer} newVal New page value
* @param {integer} oldVal Old page value
*/
pageChanged(newVal, oldVal) {
if (this.isChanged('resource', newVal, oldVal)
|| this.isChanged('criteria', newVal, oldVal)
) {
return;
}
this.reloadData();
}
this.reloadData();
}
/**
* Changed resource handler
*
* @param {{}} newVal New resource value
* @param {{}} oldVal Old resource value
*/
resourceChanged(newVal, oldVal) {
if (this.isChanged('resource', newVal, oldVal)) {
return;
}
this.reloadData();
/**
* Changed resource handler
*
* @param {{}} newVal New resource value
* @param {{}} oldVal Old resource value
*/
resourceChanged(newVal, oldVal) {
if (this.isChanged('resource', newVal, oldVal)) {
return;
}
/**
* Changed criteria handler
*
* @param {{}} newVal New criteria value
* @param {{}} oldVal Old criteria value
*/
criteriaChanged(newVal, oldVal) {
if (this.isChanged('criteria', newVal, oldVal)) {
return;
}
this.reloadData();
}
this.reloadData();
}
/**
* Changed resource handler
*
* @param {string} resource New resource value
*/
resourceChanged(resource) {
if (!resource) {
logger.error(`resource is ${typeof resource}. It should be a string or a reference`);
}
this.repository = this.entityManager.getRepository(resource);
/**
* Changed criteria handler
*
* @param {{}} newVal New criteria value
* @param {{}} oldVal Old criteria value
*/
criteriaChanged(newVal, oldVal) {
if (this.isChanged('criteria', newVal, oldVal)) {
return;
}
/**
* Get data from repository
*/
getData() {
let criteria = JSON.parse(JSON.stringify(this.criteria));
criteria.skip = (this.page * this.limit) - this.limit;
criteria.limit = this.limit;
this.error = null;
this.repository.find(criteria, true)
.then(result => {
this.data = result;
})
.catch(error => {
this.error = error;
});
this.reloadData();
}
/**
* Changed resource handler
*
* @param {string} resource New resource value
*/
resourceChanged(resource) {
if (!resource) {
logger.error(`resource is ${typeof resource}. It should be a string or a reference`);
}
this.repository = this.entityManager.getRepository(resource);
}
/**
* Get data from repository
*/
getData() {
let criteria = JSON.parse(JSON.stringify(this.criteria));
criteria.skip = (this.page * this.limit) - this.limit;
criteria.limit = this.limit;
this.error = null;
this.loading = true;
this.repository.find(criteria, true)
.then(result => {
this.data = result;
this.loading = false;
})
.catch(error => {
this.error = error;
this.loading = false;
});
}
}

0 comments on commit 6886e6b

Please sign in to comment.