Commit 23936633 authored by Sven Franck's avatar Sven Franck

Gadgets/details: added RAM storage for items and cleanup

parent 743b346c
...@@ -357,9 +357,9 @@ define([ ...@@ -357,9 +357,9 @@ define([
that.settings = priv.settings; that.settings = priv.settings;
// keep items in here, so all gadgets can access them vs. having // RAM cache of items
// to query jIO over and over from every gadget that.cacheItems = {};
that.memoryItems = []; that.activeItem = undefined;
// ======================================================================== // ========================================================================
// PUBLISH APP // PUBLISH APP
......
...@@ -11,6 +11,9 @@ config_call({ ...@@ -11,6 +11,9 @@ config_call({
"seo": { "seo": {
"friendly_links": "true" "friendly_links": "true"
}, },
"cache": {
"max_items_in_ram": 25
},
"default_language":"de-DE", "default_language":"de-DE",
"language_current":"de-DE", "language_current":"de-DE",
"language_selector":"translate", "language_selector":"translate",
......
...@@ -10,8 +10,10 @@ define([ ...@@ -10,8 +10,10 @@ define([
var matchesSelector = elem.matches || var matchesSelector = elem.matches ||
elem.webkitMatchesSelector || elem.webkitMatchesSelector ||
elem.mozMatchesSelector || elem.mozMatchesSelector ||
elem.msMatchesSelector; elem.msMatchesSelector,
fallback;
if (matchesSelector) {
while (elem) { while (elem) {
if (matchesSelector.bind(elem)(selector)) { if (matchesSelector.bind(elem)(selector)) {
return elem; return elem;
...@@ -20,6 +22,25 @@ define([ ...@@ -20,6 +22,25 @@ define([
} }
} }
return false; return false;
} else {
// opera...
fallback = $( elem ).closest(selector);
if (fallback.length > 0) {
return fallback;
}
return false;
}
};
// object size
that.objectSize = function (obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
}
}
return size;
}; };
App.util = that; App.util = that;
......
...@@ -7,7 +7,103 @@ define([ ...@@ -7,7 +7,103 @@ define([
var that = {}; var that = {};
var priv = {}; var priv = {};
// cleanup memory
priv.cleanupMemory = function (memory) {
var item, created,
oldest = 0,
limit = App.settings.max_items_in_ram || 15,
size = App.util.objectSize(memory);
if (size > limit) {
// find oldest
for (item in memory) {
if (memory.hasOwnProperty(item)) {
created = memory[item][1];
if (oldest < created) {
oldest = created;
}
}
}
// purge
for (item in memory) {
if (memory.hasOwnProperty(item)) {
if (created === memory[item][1]) {
delete memory[item];
}
}
}
}
};
// retrieve and store item to display
priv.storeItem = function (params) {
var id, now, spec, module;
// we will have an item to display. Grab it and go!
if (location.search !== "") {
spec = {};
spec.pointer = location.search.split("=")[1];
spec.storage = "items"
spec.method = "get"
spec.doc = {"_id": spec.pointer }
// TODO: should be passed via JSON?
module = "storage";
now = new Date().getTime();
} else {
// route to all products
$.mobile.changePage("products.html", {"transition":"fade"});
}
// jio response
spec.callback = function(err, response) {
if (err) {
// error handling
} else {
id = response._id;
App.cacheItems[id] = [response, now];
App.activeItem = id;
// memory
priv.cleanupMemory(App.cacheItems);
}
// this is not response.callback(!!)
params.callback_mockup(params.source);
}
// query for items
App[module].switchboard(spec);
};
// response object
that.data = source; that.data = source;
that.before = function (source, callback_mockup) {
var spec = {};
spec.gadget = RenderJs.getSelfGadget();
// TODO: this is for page event bindings. Once JQM content replaces
// page, remove this and make the gadget the content section to
// be updated.
spec.page = App.util.closest(spec.gadget.dom.get(0), "div[data-role='page']");
spec.pageId = "#" + spec.page.id;
spec.source = source;
spec.callback_mockup = callback_mockup;
if (!spec.page.getAttribute("events_details")) {
spec.page.setAttribute("events_details", true);
$(document).on("pagebeforeshow.details", spec.pageId, function (e, data) {
// TODO: if we allow to reload, we also need to trigger refresh on
// so we need to reload this every time a page is shown. question
// is how to trigger a refresh on this element ONLY
priv.storeItem(spec);
});
// and run intial
priv.storeItem(spec);
}
};
that.after = function (self) { that.after = function (self) {
if (App === undefined) { if (App === undefined) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment