From 013af36855fd9e412f8f6d2f296af348eaccc8fd Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 2 Jun 2006 19:48:22 +0000 Subject: [PATCH] Fixed some issues with object reloading -- because of the way JS closures work, simply overwriting the old object doesn't work, since the interface code retains a reference to the old object -- in other words, a memory leak and stale data. Existing item objects now reload in place instead. Same change made to collections, though I'll have to test that after I add Collection editing and save(). This behavior of JS has some implications for how interface code handles things like deletes. The following won't do what one might expect/desire (and my apologies if this is obvious or redundant, but I figure it's worth pointing out): var item = Scholar.Items.get(1); Scholar.debug(item); Scholar.DB.query("DELETE FROM items WHERE itemID=1"); Scholar.Items.reloadAll(); Scholar.debug(item); The last line will still display the old object, even though _items[1] has been deleted inside Scholar.Items. The following does work, however: var item = Scholar.Items.get(1); Scholar.debug(item); Scholar.DB.query("DELETE FROM items WHERE itemID=1"); Scholar.Items.reloadAll(); var item = Scholar.Items.get(1); Scholar.debug(item); Now item is properly undefined. Moral of the story: object references need to be deleted manually after receiving delete notifications, for if external code still has references to deleted data objects, the data layer can't do much about it. --- .../content/scholar/xpcom/data_access.js | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/chrome/chromeFiles/content/scholar/xpcom/data_access.js b/chrome/chromeFiles/content/scholar/xpcom/data_access.js index 4b120330f7..160866c9bc 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/data_access.js +++ b/chrome/chromeFiles/content/scholar/xpcom/data_access.js @@ -857,15 +857,11 @@ Scholar.Items = new function(){ /* - * Reloads all currently cached items + * Reloads all items */ function reloadAll(){ - var ids = new Array(); - for (itemID in _items){ - ids.push(itemID); - } - _load(ids); - return true; + _items = new Array(); + _load(); } @@ -908,10 +904,6 @@ Scholar.Items = new function(){ function _load(){ - if (!arguments){ - return false; - } - // Should be the same as query in Scholar.Item.loadFromID, just // without itemID clause var sql = 'SELECT I.*, lastName AS firstCreator ' @@ -920,7 +912,7 @@ Scholar.Items = new function(){ + 'LEFT JOIN creators C ON (IC.creatorID=C.creatorID) ' + 'WHERE (IC.orderIndex=0 OR IC.orderIndex IS NULL)'; - if (arguments[0]!='all'){ + if (arguments[0]){ sql += ' AND I.itemID IN (' + Scholar.join(arguments,',') + ')'; } @@ -928,9 +920,16 @@ Scholar.Items = new function(){ if (result){ for (var i=0,len=result.length; i