diff --git a/public/src/admin/manage/categories.js b/public/src/admin/manage/categories.js index c3e1dacab0..2d1cf08d5e 100644 --- a/public/src/admin/manage/categories.js +++ b/public/src/admin/manage/categories.js @@ -94,19 +94,18 @@ define('admin/manage/categories', ['vendor/jquery/serializeObject/jquery.ba-seri } }; - Categories.toggle = function(cid, state) { + Categories.toggle = function(cid, disabled) { var payload = {}; payload[cid] = { - disabled: !state | 0 + disabled: disabled ? 1 : 0 }; socket.emit('admin.categories.update', payload, function(err, result) { if (err) { return app.alertError(err.message); - } else { - ajaxify.refresh(); } + ajaxify.refresh(); }); }; @@ -114,8 +113,9 @@ define('admin/manage/categories', ['vendor/jquery/serializeObject/jquery.ba-seri newCategoryId = e.to.dataset.cid; } - function itemDragDidEnd(e){ + function itemDragDidEnd(e) { var isCategoryUpdate = (newCategoryId != -1); + //Update needed? if((e.newIndex != undefined && e.oldIndex != e.newIndex) || isCategoryUpdate){ var parentCategory = isCategoryUpdate ? sortables[newCategoryId] : sortables[e.from.dataset.cid], diff --git a/src/categories/update.js b/src/categories/update.js index 9ad8acf61e..4ebfda2f11 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -10,44 +10,51 @@ module.exports = function(Categories) { Categories.update = function(modified, callback) { - function updateCategory(cid, next) { - Categories.exists(cid, function(err, exists) { - if (err || !exists) { - return next(err); - } + var cids = Object.keys(modified); + + async.each(cids, function(cid, next) { + updateCategory(cid, modified[cid], next); + }, function(err) { + callback(err, cids); + }); + }; - var modifiedFields = modified[cid]; + function updateCategory(cid, modifiedFields, callback) { + Categories.exists(cid, function(err, exists) { + if (err || !exists) { + return callback(err); + } + + + if (modifiedFields.hasOwnProperty('name')) { + modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name); + } - if (modifiedFields.hasOwnProperty('name')) { - modifiedFields.slug = cid + '/' + utils.slugify(modifiedFields.name); + plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) { + if (err) { + return callback(err); } - plugins.fireHook('filter:category.update', {category: modifiedFields}, function(err, categoryData) { + var category = categoryData.category; + var fields = Object.keys(category); + // move parent to front, so its updated first + var parentCidIndex = fields.indexOf('parentCid'); + if (parentCidIndex !== -1 && fields.length > 1) { + fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]); + } + + async.eachSeries(fields, function(key, next) { + updateCategoryField(cid, key, category[key], next); + }, function(err) { if (err) { - return next(err); + return callback(err); } - - var category = categoryData.category; - var fields = Object.keys(category); - async.each(fields, function(key, next) { - updateCategoryField(cid, key, category[key], next); - }, function(err) { - if (err) { - return next(err); - } - plugins.fireHook('action:category.update', {cid: cid, modified: category}); - next(); - }); + plugins.fireHook('action:category.update', {cid: cid, modified: category}); + callback(); }); }); - } - - var cids = Object.keys(modified); - - async.each(cids, updateCategory, function(err) { - callback(err, cids); }); - }; + } function updateCategoryField(cid, key, value, callback) { if (key === 'parentCid') { diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index 8a833699f9..fe80afaaf4 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -68,7 +68,7 @@ module.exports = function(db, module) { return callback(err, null); } - callback(null, item[field] || null); + callback(null, item.hasOwnProperty(field) ? item[field] : null); }); };