Skip to content

Commit ecfa77a

Browse files
committed
Auto merge of #2475 - Turbo87:search, r=locks
Simplify search field handling code This PR simplifies the code that handles the values of the two search boxes in the header. It mainly gets rid of a query param `alias()`, an unnecessary `oneWay()` property and two `controllerFor()` lookups. It also removes the `search` service and adds a new `header` service instead. r? @locks
2 parents 4eb4e2e + 884c1d3 commit ecfa77a

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

app/controllers/application.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import Controller from '@ember/controller';
2-
import { oneWay } from '@ember/object/computed';
32
import { on } from '@ember/object/evented';
43
import { inject as service } from '@ember/service';
54

65
import { EKMixin, keyDown, keyPress } from 'ember-keyboard';
76

87
export default Controller.extend(EKMixin, {
8+
header: service(),
99
flashMessages: service(),
10-
search: service(),
11-
searchQuery: oneWay('search.q'),
1210
session: service(),
1311

1412
keyboardActivated: true,
@@ -28,7 +26,7 @@ export default Controller.extend(EKMixin, {
2826
search() {
2927
this.transitionToRoute('search', {
3028
queryParams: {
31-
q: this.searchQuery,
29+
q: this.header.searchValue,
3230
page: 1,
3331
},
3432
});

app/controllers/search.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import Controller from '@ember/controller';
22
import { computed } from '@ember/object';
3-
import { alias, bool, readOnly } from '@ember/object/computed';
4-
import { inject as service } from '@ember/service';
3+
import { bool, readOnly } from '@ember/object/computed';
54

65
import { task } from 'ember-concurrency';
76

87
import PaginationMixin from '../mixins/pagination';
98

109
export default Controller.extend(PaginationMixin, {
11-
search: service(),
1210
queryParams: ['all_keywords', 'page', 'per_page', 'q', 'sort'],
13-
q: alias('search.q'),
11+
q: null,
1412
page: '1',
1513
per_page: 10,
1614

app/routes/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export default Route.extend({
1717
},
1818

1919
setupController(controller) {
20-
this.controllerFor('application').set('searchQuery', null);
21-
2220
if (!controller.dataTask.hasData) {
2321
let promise = controller.dataTask.perform();
2422
if (this.fastboot.isFastBoot) {

app/routes/search.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import Route from '@ember/routing/route';
2+
import { inject as service } from '@ember/service';
23

34
export default Route.extend({
5+
header: service(),
6+
47
queryParams: {
58
all_keywords: { refreshModel: true },
69
page: { refreshModel: true },
@@ -15,7 +18,12 @@ export default Route.extend({
1518
},
1619

1720
setupController(controller, params) {
18-
this.controllerFor('application').set('searchQuery', params.q);
21+
this.header.set('searchValue', params.q);
1922
controller.dataTask.perform(params);
2023
},
24+
25+
deactivate() {
26+
this._super(...arguments);
27+
this.header.set('searchValue', null);
28+
},
2129
});
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Service from '@ember/service';
22

33
export default class SearchService extends Service {
4-
q = null;
4+
// the value of the search input fields in the header
5+
searchValue = null;
56
}

app/templates/application.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
name="q"
1616
id="cargo-desktop-search"
1717
placeholder="Click or press 'S' to search..."
18-
value={{this.searchQuery}}
19-
oninput={{action (mut this.searchQuery) value="target.value"}}
18+
value={{this.header.searchValue}}
19+
oninput={{action (mut this.header.searchValue) value="target.value"}}
2020
autocorrect="off"
2121
autocapitalize="off"
2222
autofocus="autofocus"
@@ -103,8 +103,8 @@
103103
name="q"
104104
id="cargo-mobile-search"
105105
placeholder="Search"
106-
value={{this.searchQuery}}
107-
oninput={{action (mut this.searchQuery) value="target.value"}}
106+
value={{this.header.searchValue}}
107+
oninput={{action (mut this.header.searchValue) value="target.value"}}
108108
autocorrect="off"
109109
required
110110
>

0 commit comments

Comments
 (0)