Skip to content

Commit

Permalink
refine(data-elements) sharing with monostate objects
Browse files Browse the repository at this point in the history
reworked user-data and countries-data to use a monostate
pattern. What this does is allow the data recieved from etools-ajax
to be shared with sub-sequent stamps of the element, reducing
performance cost of stamping.
A similar but more complicated approach was used for the now
unused etool-data element.
  • Loading branch information
itsjustbrian committed Oct 19, 2016
1 parent ad5383d commit 331fd75
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 258 deletions.
12 changes: 4 additions & 8 deletions src/app-config/etools-app-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
App global configuration
-->
<link rel="import" href="../../bower_components/etools-dexiejs/etools-dexiejs.html">
<link rel="import" href="../app-elements/lodash/lodash.html">
<link rel="import" href="../../scripts/lodash/lodash.html">

<script>
'use strict';
Expand All @@ -17,7 +17,7 @@
// app endpoints
endpoints: {
userInfo: {
template: '/users/api/profile/',
url: '/users/api/profile/',
exp: 30 * 60 * 1000, // 30min
cachingKey: 'userData'
},
Expand Down Expand Up @@ -53,6 +53,8 @@

// permissions
permissions: {
superPermissions: ['loggedInDefault', 'userInfoMenu',
'interventionsMenu', 'statsMenu'], //for testing
partnerOnlyPermissions: ['interventionsMenu'],
defaultPermissions: ['loggedInDefault','userInfoMenu'],
partnerPermissions: ['interventionsMenu'],
Expand All @@ -70,10 +72,4 @@
countries: 'id, name'
});

/**
* test endpoint url generation using _.template
*/
console.log(etoolsAppConfig.globals.getEndpoint('testTemplateEndpointUse', {id: 13}).url,
etoolsAppConfig.globals.getEndpoint('countries').url);

</script>
63 changes: 45 additions & 18 deletions src/app-elements/countries-data/countries-data.html
Original file line number Diff line number Diff line change
@@ -1,43 +1,70 @@
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/etools-ajax/etools-ajax.html">
<link rel="import" href="../../app-elements/lodash/lodash.html">


<dom-module id="countries-data">

<template>

<etools-ajax id="ajax"
endpoint="[[endpoint]]"
on-success="_handleResponse"
caching-storage="dexie"></etools-ajax>

</template>

<script>

Polymer({
var _data = {};
var _countryEls = [];

is: 'countries-data',
var _setCountryData = function(data) {
_data.countries = data || {};
this.load(_data);
_countryEls.forEach(function(el) {
if (el !== this) { el.load(_data); }
});
};

Polymer({

properties: {
is: 'countries-data',

countries: {
type: Array,
value: null,
notify: true
}
properties: {

},
countries: {
type: Array,
readOnly: true,
notify: true
}

ready: function() {
},

ready: function() {
this.load(_data);
},

attached: function() {
if (_countryEls.length === 0 && !_data.countries) {
this.endpoint = etoolsAppConfig.globals.getEndpoint('countries');
},
}
_countryEls.push(this);
},

_handleResponse: function(response) {
if (!_.isEmpty(response.detail)) {
this.set('countries', response.detail);
}
detached: function() {
_countryEls.splice(_countryEls.indexOf(this), 1);
},

load: function(data) {
if (data.countries) {
this._setCountries(data.countries);
}
},

});
_handleResponse: function(res) {
_setCountryData.bind(this)(res.detail);
}

});

</script>

Expand Down
136 changes: 0 additions & 136 deletions src/app-elements/etools-data/etools-data.html

This file was deleted.

Loading

0 comments on commit 331fd75

Please sign in to comment.