mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-13 17:35:46 +01:00
added callback to category update
This commit is contained in:
@@ -14,7 +14,20 @@ define(['uploader'], function(uploader) {
|
|||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
if(Object.keys(modified_categories).length) {
|
if(Object.keys(modified_categories).length) {
|
||||||
socket.emit('admin.categories.update', modified_categories);
|
socket.emit('admin.categories.update', modified_categories, function(err, result) {
|
||||||
|
if (err) {
|
||||||
|
return app.alertError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result && result.length) {
|
||||||
|
app.alert({
|
||||||
|
title: 'Updated Categories',
|
||||||
|
message: 'Category IDs ' + result.join(', ') + ' was successfully updated.',
|
||||||
|
type: 'success',
|
||||||
|
timeout: 2000
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
modified_categories = {};
|
modified_categories = {};
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,35 +1,45 @@
|
|||||||
var db = require('./../database'),
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var async = require('async'),
|
||||||
|
db = require('./../database'),
|
||||||
utils = require('./../../public/src/utils'),
|
utils = require('./../../public/src/utils'),
|
||||||
categories = require('./../categories');
|
categories = require('./../categories');
|
||||||
|
|
||||||
(function(CategoriesAdmin) {
|
(function(CategoriesAdmin) {
|
||||||
|
|
||||||
CategoriesAdmin.update = function(modified, socket) {
|
CategoriesAdmin.update = function(modified, socket, callback) {
|
||||||
var updated = [];
|
|
||||||
|
|
||||||
for (var cid in modified) {
|
function updateCategory(cid, next) {
|
||||||
var category = modified[cid];
|
var category = modified[cid];
|
||||||
|
var fields = Object.keys(category);
|
||||||
|
|
||||||
for (var key in category) {
|
async.each(fields, function(key, next) {
|
||||||
db.setObjectField('category:' + cid, key, category[key]);
|
updateCategoryField(cid, key, category[key], next);
|
||||||
|
}, next);
|
||||||
if (key === 'name') {
|
|
||||||
// reset slugs if name is updated
|
|
||||||
var slug = cid + '/' + utils.slugify(category[key]);
|
|
||||||
db.setObjectField('category:' + cid, 'slug', slug);
|
|
||||||
} else if (key === 'order') {
|
|
||||||
db.sortedSetAdd('categories:cid', category[key], cid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
updated.push(cid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.emit('event:alert', {
|
function updateCategoryField(cid, key, value, next) {
|
||||||
title: 'Updated Categories',
|
db.setObjectField('category:' + cid, key, value, function(err) {
|
||||||
message: 'Category IDs ' + updated.join(', ') + ' was successfully updated.',
|
if(err) {
|
||||||
type: 'success',
|
return next(err);
|
||||||
timeout: 2000
|
}
|
||||||
|
|
||||||
|
if (key === 'name') {
|
||||||
|
var slug = cid + '/' + utils.slugify(value);
|
||||||
|
db.setObjectField('category:' + cid, 'slug', slug, next);
|
||||||
|
} else if (key === 'order') {
|
||||||
|
db.sortedSetAdd('categories:cid', value, cid, next);
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var cids = Object.keys(modified);
|
||||||
|
|
||||||
|
async.each(cids, updateCategory, function(err) {
|
||||||
|
callback(err, cids);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -118,12 +118,12 @@ SocketAdmin.categories.create = function(socket, data, callback) {
|
|||||||
categories.create(data, callback);
|
categories.create(data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketAdmin.categories.update = function(socket, data) {
|
SocketAdmin.categories.update = function(socket, data, callback) {
|
||||||
if(!data) {
|
if(!data) {
|
||||||
throw new Error('invalid data');
|
return callback(new Error('invalid data'));
|
||||||
}
|
}
|
||||||
|
|
||||||
admin.categories.update(data, socket);
|
admin.categories.update(data, socket, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketAdmin.categories.search = function(socket, data, callback) {
|
SocketAdmin.categories.search = function(socket, data, callback) {
|
||||||
|
|||||||
Reference in New Issue
Block a user