Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(data): add option to load data from event
Browse files Browse the repository at this point in the history
  • Loading branch information
VMBindraban committed May 24, 2016
1 parent 43f9c14 commit c0f72b5
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 91 deletions.
4 changes: 4 additions & 0 deletions config.js
Expand Up @@ -14,6 +14,7 @@ System.config({
},

map: {
"aurelia-event-aggregator": "npm:aurelia-event-aggregator@1.0.0-beta.1.2.1",
"aurelia-framework": "npm:aurelia-framework@1.0.0-beta.1.0.5",
"aurelia-orm": "npm:aurelia-orm@3.0.0-rc2",
"aurelia-polyfills": "npm:aurelia-polyfills@1.0.0-beta.1.1.4",
Expand Down Expand Up @@ -75,6 +76,9 @@ System.config({
"npm:aurelia-event-aggregator@1.0.0-beta.1": {
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.2.1"
},
"npm:aurelia-event-aggregator@1.0.0-beta.1.2.1": {
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.2.1"
},
"npm:aurelia-framework@1.0.0-beta.1.0.5": {
"aurelia-binding": "npm:aurelia-binding@1.0.0-beta.1.3.5",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0-beta.1.2.3",
Expand Down
4 changes: 2 additions & 2 deletions dist/amd/data-table.html
Expand Up @@ -24,7 +24,7 @@
</th>

<!-- Actions -->
<th if.bind="destroy != null || update != null" t="Actions"></th>
<th if.bind="destroy != null || update != null" t="Actions" show.bind="showActions"></th>
</tr>
</thead>
<tbody>
Expand All @@ -36,7 +36,7 @@
</td>

<!-- Actions -->
<td style="white-space: nowrap; width: 1px;" if.bind="destroy !== null || update !== null">
<td style="white-space: nowrap; width: 1px;" if.bind="destroy !== null || update !== null" show.bind="showActions">
<button if.bind="update !== null" class="btn btn-sm btn-white" click.delegate="doUpdate(row)">
<i class="fa fa-pencil"></i>
</button>
Expand Down
68 changes: 47 additions & 21 deletions dist/amd/data-table.js
@@ -1,4 +1,4 @@
define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], function (exports, _aureliaFramework, _aureliaRouter, _jsonStatham) {
define(['exports', 'aurelia-framework', 'aurelia-event-aggregator', 'aurelia-router', 'json-statham'], function (exports, _aureliaFramework, _aureliaEventAggregator, _aureliaRouter, _jsonStatham) {
'use strict';

Object.defineProperty(exports, "__esModule", {
Expand Down Expand Up @@ -73,10 +73,10 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct
throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');
}

var _dec, _dec2, _dec3, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10;
var _dec, _dec2, _dec3, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10, _descriptor11;

var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)('data-table'), _dec2 = (0, _aureliaFramework.inject)(_aureliaRouter.Router, Element), _dec3 = (0, _aureliaFramework.computedFrom)('columns'), _dec(_class = _dec2(_class = (_class2 = function () {
function DataTable(Router, element) {
var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)('data-table'), _dec2 = (0, _aureliaFramework.inject)(_aureliaRouter.Router, Element, _aureliaEventAggregator.EventAggregator), _dec3 = (0, _aureliaFramework.computedFrom)('columns'), _dec(_class = _dec2(_class = (_class2 = function () {
function DataTable(Router, element, eventAggregator) {
_classCallCheck(this, DataTable);

_initDefineProp(this, 'repository', _descriptor, this);
Expand All @@ -93,11 +93,13 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct

_initDefineProp(this, 'destroy', _descriptor7, this);

_initDefineProp(this, 'select', _descriptor8, this);
_initDefineProp(this, 'showActions', _descriptor8, this);

_initDefineProp(this, 'data', _descriptor9, this);
_initDefineProp(this, 'select', _descriptor9, this);

_initDefineProp(this, 'route', _descriptor10, this);
_initDefineProp(this, 'data', _descriptor10, this);

_initDefineProp(this, 'route', _descriptor11, this);

this.count = 0;
this.columnsArray = [];
Expand All @@ -106,18 +108,33 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct

this.router = Router;
this.element = element;
this.ea = eventAggregator;
}

DataTable.prototype.attached = function attached() {
return this.load();
var _this = this;

this.ea.subscribe('publishData', function (response) {
_this.data = response.data;
});

this.load();
};

DataTable.prototype.load = function load() {
var _this = this;
var _this2 = this;

var criteria = this.buildCriteria();

this.ea.publish('updateCriteria', criteria);

if (!this.repository) {
this.showActions = false;
return;
}

this.repository.find(criteria, true).then(function (result) {
_this.data = result;
_this2.data = result;
}).catch(function (error) {
console.error('Something went wrong.', error);
});
Expand All @@ -134,12 +151,14 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct
criteria['where'][propertyName]['contains'] = this.searchCriteria[propertyName];
}
}

if (this.sortable !== null && Object.keys(this.sortingCriteria).length) {
var _propertyName = Object.keys(this.sortingCriteria)[0];
if (this.sortingCriteria[_propertyName]) {
criteria['sort'] = _propertyName + ' ' + this.sortingCriteria[_propertyName];
}
}

return criteria;
};

Expand All @@ -148,32 +167,32 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct
};

DataTable.prototype.doDelete = function doDelete(row) {
var _this2 = this;
var _this3 = this;

if (typeof this.delete === 'function') {
return this.delete(this.populate(row));
}

this.populate(row).destroy().then(function (ah) {
_this2.load();
_this2.triggerEvent('deleted', row);
_this3.load();
_this3.triggerEvent('deleted', row);
}).catch(function (error) {
_this2.triggerEvent('exception', { on: 'delete', error: error });
_this3.triggerEvent('exception', { on: 'delete', error: error });
});
};

DataTable.prototype.doUpdate = function doUpdate(row) {
var _this3 = this;
var _this4 = this;

if (typeof this.update === 'function') {
return this.update(this.populate(row));
}

this.populate(row).update().then(function () {
_this3.load();
_this3.triggerEvent('updated', row);
_this4.load();
_this4.triggerEvent('updated', row);
}).catch(function (error) {
_this3.triggerEvent('exception', { on: 'update', error: error });
_this4.triggerEvent('exception', { on: 'update', error: error });
});
};

Expand All @@ -200,7 +219,9 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct
if (!(this.defaultColumn in this.searchCriteria)) {
this.searchCriteria = {};
}

this.searchCriteria[this.defaultColumn] = searchInput;

this.load();
};

Expand Down Expand Up @@ -321,13 +342,18 @@ define(['exports', 'aurelia-framework', 'aurelia-router', 'json-statham'], funct
initializer: function initializer() {
return null;
}
}), _descriptor8 = _applyDecoratedDescriptor(_class2.prototype, 'select', [_aureliaFramework.bindable], {
}), _descriptor8 = _applyDecoratedDescriptor(_class2.prototype, 'showActions', [_aureliaFramework.bindable], {
enumerable: true,
initializer: function initializer() {
return true;
}
}), _descriptor9 = _applyDecoratedDescriptor(_class2.prototype, 'select', [_aureliaFramework.bindable], {
enumerable: true,
initializer: null
}), _descriptor9 = _applyDecoratedDescriptor(_class2.prototype, 'data', [_aureliaFramework.bindable], {
}), _descriptor10 = _applyDecoratedDescriptor(_class2.prototype, 'data', [_aureliaFramework.bindable], {
enumerable: true,
initializer: null
}), _descriptor10 = _applyDecoratedDescriptor(_class2.prototype, 'route', [_aureliaFramework.bindable], {
}), _descriptor11 = _applyDecoratedDescriptor(_class2.prototype, 'route', [_aureliaFramework.bindable], {
enumerable: true,
initializer: null
}), _applyDecoratedDescriptor(_class2.prototype, 'columnLabels', [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, 'columnLabels'), _class2.prototype)), _class2)) || _class) || _class);
Expand Down
4 changes: 2 additions & 2 deletions dist/commonjs/data-table.html
Expand Up @@ -24,7 +24,7 @@
</th>

<!-- Actions -->
<th if.bind="destroy != null || update != null" t="Actions"></th>
<th if.bind="destroy != null || update != null" t="Actions" show.bind="showActions"></th>
</tr>
</thead>
<tbody>
Expand All @@ -36,7 +36,7 @@
</td>

<!-- Actions -->
<td style="white-space: nowrap; width: 1px;" if.bind="destroy !== null || update !== null">
<td style="white-space: nowrap; width: 1px;" if.bind="destroy !== null || update !== null" show.bind="showActions">
<button if.bind="update !== null" class="btn btn-sm btn-white" click.delegate="doUpdate(row)">
<i class="fa fa-pencil"></i>
</button>
Expand Down
68 changes: 48 additions & 20 deletions dist/commonjs/data-table.js
Expand Up @@ -7,10 +7,12 @@ exports.DataTable = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _dec, _dec2, _dec3, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10;
var _dec, _dec2, _dec3, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10, _descriptor11;

var _aureliaFramework = require('aurelia-framework');

var _aureliaEventAggregator = require('aurelia-event-aggregator');

var _aureliaRouter = require('aurelia-router');

var _jsonStatham = require('json-statham');
Expand Down Expand Up @@ -60,8 +62,8 @@ function _initializerWarningHelper(descriptor, context) {
throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');
}

var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)('data-table'), _dec2 = (0, _aureliaFramework.inject)(_aureliaRouter.Router, Element), _dec3 = (0, _aureliaFramework.computedFrom)('columns'), _dec(_class = _dec2(_class = (_class2 = function () {
function DataTable(Router, element) {
var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)('data-table'), _dec2 = (0, _aureliaFramework.inject)(_aureliaRouter.Router, Element, _aureliaEventAggregator.EventAggregator), _dec3 = (0, _aureliaFramework.computedFrom)('columns'), _dec(_class = _dec2(_class = (_class2 = function () {
function DataTable(Router, element, eventAggregator) {
_classCallCheck(this, DataTable);

_initDefineProp(this, 'repository', _descriptor, this);
Expand All @@ -78,11 +80,13 @@ var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)

_initDefineProp(this, 'destroy', _descriptor7, this);

_initDefineProp(this, 'select', _descriptor8, this);
_initDefineProp(this, 'showActions', _descriptor8, this);

_initDefineProp(this, 'select', _descriptor9, this);

_initDefineProp(this, 'data', _descriptor9, this);
_initDefineProp(this, 'data', _descriptor10, this);

_initDefineProp(this, 'route', _descriptor10, this);
_initDefineProp(this, 'route', _descriptor11, this);

this.count = 0;
this.columnsArray = [];
Expand All @@ -91,18 +95,33 @@ var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)

this.router = Router;
this.element = element;
this.ea = eventAggregator;
}

DataTable.prototype.attached = function attached() {
return this.load();
var _this = this;

this.ea.subscribe('publishData', function (response) {
_this.data = response.data;
});

this.load();
};

DataTable.prototype.load = function load() {
var _this = this;
var _this2 = this;

var criteria = this.buildCriteria();

this.ea.publish('updateCriteria', criteria);

if (!this.repository) {
this.showActions = false;
return;
}

this.repository.find(criteria, true).then(function (result) {
_this.data = result;
_this2.data = result;
}).catch(function (error) {
console.error('Something went wrong.', error);
});
Expand All @@ -119,12 +138,14 @@ var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)
criteria['where'][propertyName]['contains'] = this.searchCriteria[propertyName];
}
}

if (this.sortable !== null && Object.keys(this.sortingCriteria).length) {
var _propertyName = Object.keys(this.sortingCriteria)[0];
if (this.sortingCriteria[_propertyName]) {
criteria['sort'] = _propertyName + ' ' + this.sortingCriteria[_propertyName];
}
}

return criteria;
};

Expand All @@ -133,32 +154,32 @@ var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)
};

DataTable.prototype.doDelete = function doDelete(row) {
var _this2 = this;
var _this3 = this;

if (typeof this.delete === 'function') {
return this.delete(this.populate(row));
}

this.populate(row).destroy().then(function (ah) {
_this2.load();
_this2.triggerEvent('deleted', row);
_this3.load();
_this3.triggerEvent('deleted', row);
}).catch(function (error) {
_this2.triggerEvent('exception', { on: 'delete', error: error });
_this3.triggerEvent('exception', { on: 'delete', error: error });
});
};

DataTable.prototype.doUpdate = function doUpdate(row) {
var _this3 = this;
var _this4 = this;

if (typeof this.update === 'function') {
return this.update(this.populate(row));
}

this.populate(row).update().then(function () {
_this3.load();
_this3.triggerEvent('updated', row);
_this4.load();
_this4.triggerEvent('updated', row);
}).catch(function (error) {
_this3.triggerEvent('exception', { on: 'update', error: error });
_this4.triggerEvent('exception', { on: 'update', error: error });
});
};

Expand All @@ -185,7 +206,9 @@ var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)
if (!(this.defaultColumn in this.searchCriteria)) {
this.searchCriteria = {};
}

this.searchCriteria[this.defaultColumn] = searchInput;

this.load();
};

Expand Down Expand Up @@ -306,13 +329,18 @@ var DataTable = exports.DataTable = (_dec = (0, _aureliaFramework.customElement)
initializer: function initializer() {
return null;
}
}), _descriptor8 = _applyDecoratedDescriptor(_class2.prototype, 'select', [_aureliaFramework.bindable], {
}), _descriptor8 = _applyDecoratedDescriptor(_class2.prototype, 'showActions', [_aureliaFramework.bindable], {
enumerable: true,
initializer: function initializer() {
return true;
}
}), _descriptor9 = _applyDecoratedDescriptor(_class2.prototype, 'select', [_aureliaFramework.bindable], {
enumerable: true,
initializer: null
}), _descriptor9 = _applyDecoratedDescriptor(_class2.prototype, 'data', [_aureliaFramework.bindable], {
}), _descriptor10 = _applyDecoratedDescriptor(_class2.prototype, 'data', [_aureliaFramework.bindable], {
enumerable: true,
initializer: null
}), _descriptor10 = _applyDecoratedDescriptor(_class2.prototype, 'route', [_aureliaFramework.bindable], {
}), _descriptor11 = _applyDecoratedDescriptor(_class2.prototype, 'route', [_aureliaFramework.bindable], {
enumerable: true,
initializer: null
}), _applyDecoratedDescriptor(_class2.prototype, 'columnLabels', [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, 'columnLabels'), _class2.prototype)), _class2)) || _class) || _class);

0 comments on commit c0f72b5

Please sign in to comment.