Skip to content

Commit f58c0bc

Browse files
committed
more store
1 parent 39ec8e4 commit f58c0bc

File tree

6 files changed

+122
-27
lines changed

6 files changed

+122
-27
lines changed

README.md

+101-2
Original file line numberDiff line numberDiff line change
@@ -1234,10 +1234,109 @@ 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. Our Angular app is reducing to its bare bones.
1238-
See `tag-07-es6-components`.
1237+
And delete all files and code related to the original component.
1238+
Refer to `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`.
12411340

12421341

12431342

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

+1-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ export class SearchService {
55
this.store = {
66
searching: false,
77
searchParam: '',
8-
searchResults: [],
9-
currentDetail: null
8+
searchResults: []
109
};
1110
this.VuexStore = VuexStore;
1211
}
@@ -20,17 +19,10 @@ export class SearchService {
2019
this.store.searching = false;
2120
}
2221

23-
selectItem (id) {
24-
this.store.currentDetail = this.store.searchResults.find(function (r) {
25-
return r.id === id;
26-
});
27-
}
28-
2922
query (searchParam) {
3023
this.store.searching = true;
3124
this.store.searchParam = searchParam;
3225
this.store.searchResults = [];
33-
this.store.currentDetail = null;
3426
return this.executeQuery(searchParam).then(this.resolveQuery.bind(this));
3527
}
3628
};

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

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

1616
<script>
17-
import { searchService } from '@/ngVueBridgeCode/ngVueComponentsModule';
18-
1917
export default {
2018
name: "Detail",
21-
data() {
22-
return {
23-
currentDetail: null
24-
}
25-
},
19+
computed: {
20+
currentDetail () {
21+
return this.$store.getters['currentDetail'];
22+
}
23+
},
2624
methods: {
2725
getDetail (detailId) {
28-
searchService.selectItem(Number(detailId));
29-
this.currentDetail = searchService.store.currentDetail;
26+
this.$store.dispatch('selectItem', Number(detailId));
3027
},
3128
addResult () {
3229
this.$store.dispatch('addResult', {
@@ -38,9 +35,7 @@ export default {
3835
}
3936
},
4037
beforeRouteEnter (to, from, next) {
41-
next(component => {
42-
component.getDetail(to.params.itemId);
43-
});
38+
next(component => component.getDetail(to.params.itemId));
4439
},
4540
beforeRouteUpdate (to, from, next) {
4641
this.getDetail(to.params.itemId);

code/vueApp/src/vueCode/store.js

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

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

1213
actions: {
@@ -19,13 +20,17 @@ export default new Vuex.Store({
1920
];
2021

2122
setTimeout(() => {
23+
store.commit('setSelectedDetail', null);
2224
store.commit('setResults', results);
2325
resolve(results);
2426
}, 2000);
2527
});
2628
},
2729
addResult: (store, result) => {
2830
store.commit('addResult', result);
31+
},
32+
selectItem: (store, id) => {
33+
store.commit('setSelectedDetail', id);
2934
}
3035
},
3136

@@ -36,10 +41,14 @@ export default new Vuex.Store({
3641
addResult: (state, result) => {
3742
state.results = state.results.concat(result);
3843
VueAngularEventBus.$emit('result-added');
44+
},
45+
setSelectedDetail: (state, id) => {
46+
state.selectedDetailId = id;
3947
}
4048
},
4149

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

0 commit comments

Comments
 (0)