Search correctly passes context data to server

This commit is contained in:
Robin Ward 2013-05-24 12:21:53 -04:00
parent 69bb70fbd3
commit 962f0dd5f9
10 changed files with 37 additions and 12 deletions

View File

@ -12,13 +12,20 @@ Discourse.Search = {
@method forTerm
@param {String} term The term to search for
@param {String} typeFilter An optional filter to restrict the search by type
@param {Object} opts Options for searching
@param {String} opts.typeFilter Filter our results to one type only
@param {Ember.Object} opts.searchContext data to help searching within a context (say, a category or user)
@return {Promise} a promise that resolves the search results
**/
forTerm: function(term, typeFilter) {
return Discourse.ajax('/search', {
data: { term: term, type_filter: typeFilter }
});
forTerm: function(term, opts) {
if (!opts) opts = {};
// Only include the data we have
var data = { term: term }
if (opts.typeFilter) data.typeFilter = opts.typeFilter;
if (opts.searchContext) data.search_context = opts.searchContext;
return Discourse.ajax('/search', { data: data });
}
}

View File

@ -25,7 +25,12 @@ Discourse.SearchController = Em.ArrayController.extend(Discourse.Presence, {
var searchController = this;
this.set('count', 0);
return Discourse.Search.forTerm(term, typeFilter).then(function(results) {
var searcher = Discourse.Search.forTerm(term, {
typeFilter: typeFilter,
searchContext: searchController.get('searchContext')
});
return searcher.then(function(results) {
searchController.set('results', results);
if (results) {
searchController.set('noResults', results.length === 0);

View File

@ -20,6 +20,10 @@ Discourse.Category = Discourse.Model.extend({
this.set("groups", Em.A(this.groups));
},
searchContext: function() {
return ({ type: 'category', id: this.get('id') });
}.property('id'),
url: function() {
return Discourse.getURL("/category/") + (this.get('slug'));
}.property('name'),

View File

@ -13,10 +13,10 @@ Discourse.Topic = Discourse.Model.extend({
return this.get('participants').slice(0, 3);
}.property('participants'),
canConvertToRegular: (function() {
canConvertToRegular: function() {
var a = this.get('archetype');
return a !== 'regular' && a !== 'private_message';
}).property('archetype'),
}.property('archetype'),
convertArchetype: function(archetype) {
var a = this.get('archetype');
@ -29,6 +29,10 @@ Discourse.Topic = Discourse.Model.extend({
}
},
searchContext: function() {
return ({ type: 'topic', id: this.get('id') });
}.property('id'),
category: function() {
return Discourse.Category.list().findProperty('name', this.get('categoryName'));
}.property('categoryName'),

View File

@ -28,6 +28,10 @@ Discourse.User = Discourse.Model.extend({
return Discourse.Utilities.avatarUrl(this.get('username'), 'small', this.get('avatar_template'));
}).property('username'),
searchContext: function() {
return ({ type: 'user', id: this.get('username_lower') });
}.property('username_lower'),
/**
This user's website.

View File

@ -47,7 +47,7 @@ Discourse.ListCategoryRoute = Discourse.FilteredListRoute.extend({
this._super();
// Add a search context
this.controllerFor('search').set('searchContext', this.modelFor('listCategory'));
this.controllerFor('search').set('searchContext', this.modelFor('listCategory').get('searchContext'));
},
deactivate: function() {

View File

@ -25,7 +25,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
Discourse.set('transient.lastTopicIdViewed', parseInt(topic.get('id'), 10));
// Set the search context
this.controllerFor('search').set('searchContext', topic);
this.controllerFor('search').set('searchContext', topic.get('searchContext'));
},
deactivate: function() {

View File

@ -28,7 +28,7 @@ Discourse.UserRoute = Discourse.Route.extend({
});
// Add a search context
this.controllerFor('search').set('searchContext', user);
this.controllerFor('search').set('searchContext', user.get('searchContext'));
},
deactivate: function() {

View File

@ -26,7 +26,7 @@ Discourse.ChooseTopicView = Discourse.View.extend({
search: Discourse.debounce(function(title) {
var chooseTopicView = this;
Discourse.Search.forTerm(title, 'topic').then(function (facets) {
Discourse.Search.forTerm(title, {typeFilter: 'topic'}).then(function (facets) {
if (facets && facets[0] && facets[0].results) {
chooseTopicView.set('topics', facets[0].results);
} else {

View File

@ -55,6 +55,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({
// background colors are available as a pipe-separated string
backgroundColors: function() {
var categories = Discourse.Category.list();
return Discourse.SiteSettings.category_colors.split("|").map(function(i) { return i.toUpperCase(); }).concat(
categories.map(function(c) { return c.color.toUpperCase(); }) ).uniq();
}.property('Discourse.SiteSettings.category_colors'),