don't request lists twice: load in folders, then update menu

This commit is contained in:
rolux 2014-01-19 09:41:38 +00:00
parent fbbda1ed61
commit ddef87afd3
2 changed files with 17 additions and 40 deletions

View file

@ -150,12 +150,17 @@ pandora.ui.folderList = function(id, section) {
], operator: '&'}; ], operator: '&'};
} else if (id == 'featured') { } else if (id == 'featured') {
query = {conditions: [ query = {conditions: [
{key: 'status', value: 'featured', operator: '='} // fixme: '==' performs better {key: 'status', value: 'featured', operator: '='}
], operator: '&'}; ], operator: '&'};
} }
return pandora.api['find' + folderItems](Ox.extend(data, { return pandora.api['find' + folderItems](Ox.extend(data, {
query: query query: query
}), callback); }), function(result) {
if (Ox.isArray(result.data.items)) {
pandora.$ui.mainMenu.updateLists(id, result.data.items);
}
callback(result);
});
}; };
} else { } else {
columns = [ columns = [

View file

@ -8,6 +8,7 @@ pandora.ui.mainMenu = function() {
findState = pandora.getFindState(ui.find), findState = pandora.getFindState(ui.find),
fromMenu = false, fromMenu = false,
fullscreenState = Ox.Fullscreen.getState(), fullscreenState = Ox.Fullscreen.getState(),
lists = {},
that = Ox.MainMenu({ that = Ox.MainMenu({
extras: pandora.site.menuExtras.map(function(menuExtra) { extras: pandora.site.menuExtras.map(function(menuExtra) {
if (menuExtra == 'user') { if (menuExtra == 'user') {
@ -752,6 +753,7 @@ pandora.ui.mainMenu = function() {
]('findsimilar'); ]('findsimilar');
}, },
pandora_section: function(data) { pandora_section: function(data) {
lists = {};
that.checkItem('viewMenu_section_' + data.value); that.checkItem('viewMenu_section_' + data.value);
that.replaceMenu('listMenu', getListMenu()); that.replaceMenu('listMenu', getListMenu());
that.replaceMenu('itemMenu', getItemMenu()); that.replaceMenu('itemMenu', getItemMenu());
@ -931,7 +933,7 @@ pandora.ui.mainMenu = function() {
] }; ] };
} }
function getListMenu(lists) { function getListMenu() {
var itemNameSingular = ui.section == 'items' ? 'List' : ui.section == 'edits' ? 'Edit' : 'Text', var itemNameSingular = ui.section == 'items' ? 'List' : ui.section == 'edits' ? 'Edit' : 'Text',
itemNamePlural = ui.section == 'items' ? 'Lists' : ui.section == 'edits' ? 'Edits' : 'Texts'; itemNamePlural = ui.section == 'items' ? 'Lists' : ui.section == 'edits' ? 'Edits' : 'Texts';
return { id: 'listMenu', title: Ox._(itemNameSingular), items: [].concat( return { id: 'listMenu', title: Ox._(itemNameSingular), items: [].concat(
@ -947,7 +949,7 @@ pandora.ui.mainMenu = function() {
return { return {
id: folder + 'lists', id: folder + 'lists',
title: Ox._(Ox.toTitleCase(folder) + ' ' + itemNamePlural), title: Ox._(Ox.toTitleCase(folder) + ' ' + itemNamePlural),
items: Ox.isUndefined(lists) items: Ox.isUndefined(lists[folder])
? [{id: 'loading', title: Ox._('Loading...'), disabled: true}] ? [{id: 'loading', title: Ox._('Loading...'), disabled: true}]
: lists[folder].length == 0 : lists[folder].length == 0
? [{id: 'nolists', title: Ox._('No ' + Ox.toTitleCase(folder) + ' ' + itemNamePlural), disabled: true}] ? [{id: 'nolists', title: Ox._('No ' + Ox.toTitleCase(folder) + ' ' + itemNamePlural), disabled: true}]
@ -1086,42 +1088,12 @@ pandora.ui.mainMenu = function() {
return that; return that;
}; };
// fixme: the sidebar makes (almost) the same requests. that.updateLists = function(folder, items) {
// is it ok to make them twice, or should the sidebar trigger the menu replace? lists[folder] = items;
if (Ox.len(lists) == 3) {
var counter = 0, pandora.$ui.mainMenu.replaceMenu('listMenu', getListMenu());
lists = {},
queries = {
// fixme: duplicated
personal: {conditions: [
{key: 'user', value: pandora.user.username, operator: '=='},
{key: 'status', value: 'featured', operator: '!='}
], operator: '&'},
favorite: {conditions: [
{key: 'subscribed', value: true, operator: '='},
{key: 'status', value: 'featured', operator: '!='},
], operator: '&'},
featured: {conditions: [
{key: 'status', value: 'featured', operator: '='}
], operator: '&'}
};
Ox.forEach(queries, function(query, folder) {
pandora.api[
ui.section == 'items' ? 'findLists'
: ui.section == 'edits' ? 'findEdits'
: 'findTexts'
]({
query: query,
keys: ['id', 'name', 'user'],
sort: [{key: 'position', operator: '+'}]
}, function(result) {
lists[folder] = result.data.items;
if (++counter == 3) {
pandora.$ui.mainMenu.replaceMenu('listMenu', getListMenu(lists));
} }
}); };
});
return that; return that;