Skip to content

Commit a6472cc

Browse files
committed
Revert "more store"
This reverts commit f58c0bc.
1 parent f58c0bc commit a6472cc

File tree

6 files changed

+27
-122
lines changed

6 files changed

+27
-122
lines changed

README.md

+2-101
Original file line numberDiff line numberDiff line change
@@ -1234,109 +1234,10 @@ import Search from '@/ngVueBridgeCode/components/Search/index.js';
12341234
ngVueComponentsModule.component('search', Search);
12351235
```
12361236

1237-
And delete all files and code related to the original component.
1238-
Refer to `tag-07-es6-components`.
1237+
And delete all files and code related to the original component. Our Angular app is reducing to its bare bones.
1238+
See `tag-07-es6-components`.
12391239

12401240

1241-
Bonus #2: free Vue components from Angular services
1242-
----
1243-
Now we have got a store we can move on and strip `searchService` of unnecessary parts.
1244-
For example, the `store.currentDetail` property and the `selectItem(id)` method are exclusively used by the `detail` Vue component. Let's move them from the Angular service to the Vuex store.
1245-
1246-
Comment (or delete) the following lines:
1247-
1248-
**ngVueBridgeCode/services/searchService.js**
1249-
```javascript
1250-
export class SearchService {
1251-
constructor($timeout, VuexStore) {
1252-
/* ... */
1253-
this.store = {
1254-
searching: false,
1255-
searchParam: '',
1256-
searchResults: []
1257-
//, currentDetail: null
1258-
};
1259-
/* ... */
1260-
}
1261-
1262-
/*
1263-
selectItem(id) {
1264-
this.store.currentDetail = this.store.searchResults.find(function (r) {
1265-
return r.id === id;
1266-
});
1267-
}
1268-
*/
1269-
1270-
query (searchParam) {
1271-
/* ... */
1272-
// this.store.currentDetail = null;
1273-
/* ... */
1274-
}
1275-
};
1276-
```
1277-
1278-
And modify the store:
1279-
1280-
**vueCode/store.js**
1281-
```javascript
1282-
export default new Vuex.Store({
1283-
state: {
1284-
/* ... */
1285-
selectedDetailId: null
1286-
},
1287-
actions: {
1288-
getResults: (store, searchParam) => {
1289-
return new Promise((resolve, reject) => {
1290-
/* ... */
1291-
1292-
setTimeout(() => {
1293-
store.commit('setSelectedDetail', null);
1294-
store.commit('setResults', results);
1295-
resolve(results);
1296-
}, 2000);
1297-
});
1298-
},
1299-
/* ... */
1300-
selectItem: (store, id) => {
1301-
store.commit('setSelectedDetail', id);
1302-
}
1303-
},
1304-
mutations: {
1305-
/* ... */
1306-
setSelectedDetail: (state, id) => {
1307-
state.selectedDetailId = id;
1308-
}
1309-
},
1310-
getters: {
1311-
/* ... */
1312-
currentDetail: state => state.results.find(r => r.id === state.selectedDetailId)
1313-
}
1314-
});
1315-
```
1316-
1317-
Lastly, rewrite the component to use the store in place of the service:
1318-
1319-
**vueCode/components/Detail/index.vue**
1320-
```javascript
1321-
export default {
1322-
/* ... */
1323-
computed: {
1324-
currentDetail () {
1325-
return this.$store.getters['currentDetail'];
1326-
}
1327-
},
1328-
methods: {
1329-
getDetail (detailId) {
1330-
this.$store.dispatch('selectItem', Number(detailId));
1331-
},
1332-
/* ... */
1333-
},
1334-
/* ... */
1335-
};
1336-
```
1337-
1338-
Sure, you can probably do the same with the `store.searchResults` property. Our Angular app is reducing to its bare bones.
1339-
Refer to `tag-08-more-store`.
13401241

13411242

13421243

code/vueApp/dist/js/appVueLib.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/vueApp/dist/js/appVueLib_NgVueBridge.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/vueApp/src/ngVueBridgeCode/services/searchService.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export class SearchService {
55
this.store = {
66
searching: false,
77
searchParam: '',
8-
searchResults: []
8+
searchResults: [],
9+
currentDetail: null
910
};
1011
this.VuexStore = VuexStore;
1112
}
@@ -19,10 +20,17 @@ export class SearchService {
1920
this.store.searching = false;
2021
}
2122

23+
selectItem (id) {
24+
this.store.currentDetail = this.store.searchResults.find(function (r) {
25+
return r.id === id;
26+
});
27+
}
28+
2229
query (searchParam) {
2330
this.store.searching = true;
2431
this.store.searchParam = searchParam;
2532
this.store.searchResults = [];
33+
this.store.currentDetail = null;
2634
return this.executeQuery(searchParam).then(this.resolveQuery.bind(this));
2735
}
2836
};

code/vueApp/src/vueCode/components/Detail/index.vue

+12-7
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414
</template>
1515

1616
<script>
17+
import { searchService } from '@/ngVueBridgeCode/ngVueComponentsModule';
18+
1719
export default {
1820
name: "Detail",
19-
computed: {
20-
currentDetail () {
21-
return this.$store.getters['currentDetail'];
22-
}
23-
},
21+
data() {
22+
return {
23+
currentDetail: null
24+
}
25+
},
2426
methods: {
2527
getDetail (detailId) {
26-
this.$store.dispatch('selectItem', Number(detailId));
28+
searchService.selectItem(Number(detailId));
29+
this.currentDetail = searchService.store.currentDetail;
2730
},
2831
addResult () {
2932
this.$store.dispatch('addResult', {
@@ -35,7 +38,9 @@ export default {
3538
}
3639
},
3740
beforeRouteEnter (to, from, next) {
38-
next(component => component.getDetail(to.params.itemId));
41+
next(component => {
42+
component.getDetail(to.params.itemId);
43+
});
3944
},
4045
beforeRouteUpdate (to, from, next) {
4146
this.getDetail(to.params.itemId);

code/vueApp/src/vueCode/store.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Vue.use(Vuex);
66

77
export default new Vuex.Store({
88
state: {
9-
results: [],
10-
selectedDetailId: null
9+
results: []
1110
},
1211

1312
actions: {
@@ -20,17 +19,13 @@ export default new Vuex.Store({
2019
];
2120

2221
setTimeout(() => {
23-
store.commit('setSelectedDetail', null);
2422
store.commit('setResults', results);
2523
resolve(results);
2624
}, 2000);
2725
});
2826
},
2927
addResult: (store, result) => {
3028
store.commit('addResult', result);
31-
},
32-
selectItem: (store, id) => {
33-
store.commit('setSelectedDetail', id);
3429
}
3530
},
3631

@@ -41,14 +36,10 @@ export default new Vuex.Store({
4136
addResult: (state, result) => {
4237
state.results = state.results.concat(result);
4338
VueAngularEventBus.$emit('result-added');
44-
},
45-
setSelectedDetail: (state, id) => {
46-
state.selectedDetailId = id;
4739
}
4840
},
4941

5042
getters: {
51-
resultsCount: state => state.results.length,
52-
currentDetail: state => state.results.find(r => r.id === state.selectedDetailId)
43+
resultsCount: state => state.results.length
5344
}
5445
});

0 commit comments

Comments
 (0)