mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 00:41:16 +08:00
Ember support for parent categories
This commit is contained in:
@ -48,9 +48,18 @@ Discourse.Site.reopenClass(Discourse.Singleton, {
|
|||||||
var result = this._super(obj);
|
var result = this._super(obj);
|
||||||
|
|
||||||
if (result.categories) {
|
if (result.categories) {
|
||||||
|
var byId = {}
|
||||||
result.categories = _.map(result.categories, function(c) {
|
result.categories = _.map(result.categories, function(c) {
|
||||||
return Discourse.Category.create(c);
|
byId[c.id] = Discourse.Category.create(c);
|
||||||
|
return byId[c.id];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Associate the categories with their parents
|
||||||
|
result.categories.forEach(function (c) {
|
||||||
|
if (c.get('parent_category_id')) {
|
||||||
|
c.set('parentCategory', byId[c.get('parent_category_id')]);
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.trust_levels) {
|
if (result.trust_levels) {
|
||||||
|
@ -10,6 +10,7 @@ class BasicCategorySerializer < ApplicationSerializer
|
|||||||
:topic_url,
|
:topic_url,
|
||||||
:hotness,
|
:hotness,
|
||||||
:read_restricted,
|
:read_restricted,
|
||||||
:permission
|
:permission,
|
||||||
|
:parent_category_id
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -10,4 +10,5 @@ test('slugFor', function(){
|
|||||||
slugFor({id: 123, slug: ''}, "123-category", "It returns id-category for empty strings");
|
slugFor({id: 123, slug: ''}, "123-category", "It returns id-category for empty strings");
|
||||||
slugFor({id: 456}, "456-category", "It returns id-category for undefined slugs");
|
slugFor({id: 456}, "456-category", "It returns id-category for undefined slugs");
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -9,4 +9,28 @@ test('instance', function(){
|
|||||||
present(site.get('flagTypes'), "The instance has a list of flag types");
|
present(site.get('flagTypes'), "The instance has a list of flag types");
|
||||||
present(site.get('trustLevels'), "The instance has a list of trust levels");
|
present(site.get('trustLevels'), "The instance has a list of trust levels");
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test('create categories', function() {
|
||||||
|
|
||||||
|
var site = Discourse.Site.create({
|
||||||
|
categories: [{ id: 1234, name: 'Test'},
|
||||||
|
{ id: 3456, name: 'Test Subcategory', parent_category_id: 1234},
|
||||||
|
{ id: 3456, name: 'Invalid Subcategory', parent_category_id: 6666}]
|
||||||
|
});
|
||||||
|
|
||||||
|
var categories = site.get('categories');
|
||||||
|
|
||||||
|
present(categories, "The categories are present");
|
||||||
|
equal(categories.length, 3, "it loaded all three categories");
|
||||||
|
|
||||||
|
var parent = categories.findBy('id', 1234);
|
||||||
|
present(parent, "it loaded the parent category");
|
||||||
|
blank(parent.get('parentCategory'), 'it has no parent category');
|
||||||
|
|
||||||
|
var subcategory = categories.findBy('id', 3456);
|
||||||
|
present(subcategory, "it loaded the subcategory");
|
||||||
|
equal(subcategory.get('parentCategory'), parent, "it has associated the child with the parent");
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
Reference in New Issue
Block a user