- Change: Scholar.Notifier.trigger() can be passed an array of ids in some cases rather than a single id -- trees that implement notify() should use Scholar.flattenArguments(ids) to get an array either way

- Added Notifier triggers to Item.erase() (which only runs if not in a nested transaction) and Collections.erase()

- notify() in ItemTreeView updated with example of taking multiple ids, though it doesn't actually work (and notify() implementations may decide just to refresh the whole tree when ids.length>1 rather than dealing with changes individually)

- When deleting collections, use DB tables that actually exist
This commit is contained in:
Dan Stillman 2006-06-01 22:19:21 +00:00
parent 4e0dbb3885
commit 70630a2e70
3 changed files with 50 additions and 34 deletions

View file

@ -156,9 +156,13 @@ Scholar.ItemTreeView.prototype.getCollectionID = function()
}
//CALLED BY DATA LAYER ON CHANGE:
Scholar.ItemTreeView.prototype.notify = function(action, type, id)
Scholar.ItemTreeView.prototype.notify = function(action, type, ids)
{
var row = this._itemRowMap[id];
ids = Scholar.flattenArguments(ids);
for (var i=0, len=ids.length; i<len; i++){
var row = this._itemRowMap[ids[i]];
if(action == 'remove' && row)
{
this._hideItem(row);
@ -170,7 +174,7 @@ Scholar.ItemTreeView.prototype.notify = function(action, type, id)
}
else if(action == 'add' && !row)
{
var item = Scholar.Items.get(id);
var item = Scholar.Items.get(ids[i]);
if(this._itemGroup.isLibrary() || item.inCollection(this.getCollectionID()))
{
@ -184,5 +188,7 @@ Scholar.ItemTreeView.prototype.notify = function(action, type, id)
return;
}
}
this._refreshHashMap();
}

View file

@ -324,6 +324,7 @@ Scholar.Item.prototype.setField = function(field, value, loadIn){
}
}
/*
* Save changes back to database
*/
@ -634,9 +635,12 @@ Scholar.Item.prototype.erase = function(){
throw (e);
}
Scholar.Items.unload(this.getID());
// If we're not in the middle of a larger commit, trigger the notifier now
if (!Scholar.DB.transactionInProgress()){
Scholar.Notifier.trigger('remove', 'item', this.getID());
}
// TODO: trigger reloading of treeview
Scholar.Items.unload(this.getID());
}
@ -1043,6 +1047,7 @@ Scholar.Collection.prototype.erase = function(deleteItems){
var descendents = this._getDescendents();
var collections = new Array(this._id);
var items = new Array();
for(var i=0, len=descendents.length; i<len; i++){
// Descendent collections
@ -1055,21 +1060,25 @@ Scholar.Collection.prototype.erase = function(deleteItems){
// Delete items from DB
Scholar.Items.get(descendents[i]['id']).erase();
}
items.push(descendents[i]['id']);
}
}
// Remove item associations for all descendent collections
Scholar.DB.query('DELETE FROM itemCollections WHERE collectionID IN ('
Scholar.DB.query('DELETE FROM collectionItems WHERE collectionID IN ('
+ collections.join() + ')');
// And delete all descendent collections
Scholar.DB.query('DELETE FROM collection WHERE collectionID IN ('
Scholar.DB.query('DELETE FROM collections WHERE collectionID IN ('
+ collections.join() + ')');
Scholar.DB.commitTransaction();
Scholar.Notifier.trigger('remove', 'collection', collections);
Scholar.Notifier.trigger('remove', 'item', items);
// Clear deleted collection from internal memory
Scholar.Collections.unload(collections);
Scholar.DB.commitTransaction();
}

View file

@ -26,10 +26,11 @@ Scholar.Notifier = new function(){
}
/**
* event is one of 'add', 'remove', 'modify'
* type is one of 'collection', 'smartcollection', 'item'
* event - 'add', 'remove', 'modify'
* type - 'collection', 'smartcollection', 'item'
* ids - single id or array of ids
**/
function trigger(event, type, id){
function trigger(event, type, ids){
switch (type){
case 'item':
var treeType = 'itemTree';
@ -44,8 +45,8 @@ Scholar.Notifier = new function(){
for (i in _observers[treeType]){
Scholar.debug("Calling _observers['" + treeType + "']['" + i + "'].notify('" + event
+ "', " + type + "', " + id + ")", 4);
_observers[treeType][i].notify(event, type, id);
+ "', " + type + "', " + ids.join() + ")", 4);
_observers[treeType][i].notify(event, type, ids);
}
}