Skip to content

Commit

Permalink
updated simply.viewmodel to allow caching of plugin data/result
Browse files Browse the repository at this point in the history
added simply.viewmodel.etag() function for random ids in the cached data
  • Loading branch information
poef committed Jun 4, 2024
1 parent ccd1144 commit 6cec1d1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
27 changes: 22 additions & 5 deletions dist/simply.everything.js
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,7 @@ properties for a given parent, keep seperate index for this?
load();
} else {
global.document.addEventListener('simply-content-loaded', function() {
console.log('switching...')
load();
});
}
Expand All @@ -1619,10 +1620,18 @@ properties for a given parent, keep seperate index for this?
})(this);
(function(global) {
'use strict';

function etag() {
let d = '';
while (d.length < 32) d += Math.random().toString(16).substr(2);
const vr = ((parseInt(d.substr(16, 1), 16) & 0x3) | 0x8).toString(16);
return `${d.substr(0, 8)}-${d.substr(8, 4)}-4${d.substr(13, 3)}-${vr}${d.substr(17, 3)}-${d.substr(20, 12)}`;
}

function ViewModel(name, data, options) {
this.name = name;
this.data = data || [];
this.data.etag = etag();
this.view = {
options: {},
data: [] //Array.from(this.data).slice()
Expand All @@ -1645,15 +1654,22 @@ properties for a given parent, keep seperate index for this?
// this.data is a reference to the data passed, so that any changes in it will get applied
// to the original
this.data = params.data;
this.data.etag = etag()
}
// the view is a shallow copy of the array, so that changes in sort order and filtering
// won't get applied to the original, but databindings on its children will still work
this.view.data = Array.from(this.data).slice();
var plugins = this.plugins.start.concat(this.plugins.select, this.plugins.order, this.plugins.render, this.plugins.finish);
var self = this;
plugins.forEach(function(plugin) {
plugin.call(self, params);
this.view.data.etag = this.data.etag;
let data = this.view.data;
let plugins = this.plugins.start.concat(this.plugins.select, this.plugins.order, this.plugins.render, this.plugins.finish);
plugins.forEach(plugin => {
data = plugin.call(this, params, data);
if (!data) {
data = this.view.data;
}
this.view.data = data
});
this.view.data = data;

if (global.editor) {
global.editor.addDataSource(this.name,{
Expand Down Expand Up @@ -1778,7 +1794,8 @@ properties for a given parent, keep seperate index for this?
createFilter: createFilter,
createSort: createSort,
createPaging: createPaging,
updateDataSource: updateDataSource
updateDataSource: updateDataSource,
etag
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
Expand Down
25 changes: 20 additions & 5 deletions js/simply.viewmodel.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
(function(global) {
'use strict';

function etag() {
let d = '';
while (d.length < 32) d += Math.random().toString(16).substr(2);
const vr = ((parseInt(d.substr(16, 1), 16) & 0x3) | 0x8).toString(16);
return `${d.substr(0, 8)}-${d.substr(8, 4)}-4${d.substr(13, 3)}-${vr}${d.substr(17, 3)}-${d.substr(20, 12)}`;
}

function ViewModel(name, data, options) {
this.name = name;
this.data = data || [];
this.data.etag = etag();
this.view = {
options: {},
data: [] //Array.from(this.data).slice()
Expand All @@ -26,14 +34,20 @@
// this.data is a reference to the data passed, so that any changes in it will get applied
// to the original
this.data = params.data;
this.data.etag = etag()
}
// the view is a shallow copy of the array, so that changes in sort order and filtering
// won't get applied to the original, but databindings on its children will still work
this.view.data = Array.from(this.data).slice();
var plugins = this.plugins.start.concat(this.plugins.select, this.plugins.order, this.plugins.render, this.plugins.finish);
var self = this;
plugins.forEach(function(plugin) {
plugin.call(self, params);
this.view.data.etag = this.data.etag;
let data = this.view.data;
let plugins = this.plugins.start.concat(this.plugins.select, this.plugins.order, this.plugins.render, this.plugins.finish);
plugins.forEach(plugin => {
data = plugin.call(this, params, data);
if (!data) {
data = this.view.data;
}
this.view.data = data
});

if (global.editor) {
Expand Down Expand Up @@ -159,7 +173,8 @@
createFilter: createFilter,
createSort: createSort,
createPaging: createPaging,
updateDataSource: updateDataSource
updateDataSource: updateDataSource,
etag
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@
"homepage": "https://github.com/simplyedit/simplyview#readme",
"devDependencies": {
"jest": "^24.9.0"
}
},
"files": [
"README.md",
"dist/",
"js/"
]
}

0 comments on commit 6cec1d1

Please sign in to comment.