Skip to content

Commit e6e0138

Browse files
committed
using vuex
1 parent 90c6c8a commit e6e0138

File tree

12 files changed

+533
-59
lines changed

12 files changed

+533
-59
lines changed

README.md

Lines changed: 406 additions & 42 deletions
Large diffs are not rendered by default.

code/angularApp/components/search.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@
66
'<div class="app-Search">' +
77
' <input type="text" placeholder="search" ng-model="$ctrl.searchInput" />' +
88
' <button ng-disabled="!$ctrl.searchInput" ng-click="$ctrl.search($ctrl.searchInput)">Search</button>' +
9+
' (results count: {{$ctrl.resultsCount()}})' +
910
'</div>',
10-
controller: ['searchService', function (searchService) {
11+
controller: ['$scope', 'searchService', 'VuexStore', 'utilities', 'VueAngularEventBus', function ($scope, searchService, VuexStore, utilities, VueAngularEventBus) {
12+
var render = utilities.safeApply.bind($scope);
1113

1214
angular.extend(this, {
1315
searchInput: '',
1416
search: function (searchParam) {
15-
searchService.query(searchParam);
17+
searchService.query(searchParam).then(render);
18+
},
19+
resultsCount: function () {
20+
return VuexStore.getters['resultsCount'];
1621
},
1722
$onInit: function () {
1823
console.log('Component "search" ready');
24+
VueAngularEventBus.$on('result-added', render);
25+
},
26+
$onDestroy() {
27+
VueAngularEventBus.$off('result-added', render);
1928
}
2029
});
2130

code/vueApp/dist/js/appVueLib.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/vueApp/dist/js/appVueLib_VendorsDependencies.js

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/vueApp/src/ngVueBridgeCode/ngVueComponentsModule.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import 'ngVue';
44
import 'ngVue/build/plugins.js';
55
import VueAppContainer from '@/vueCode/components/VueAppContainer';
66
import { SearchService } from '@/ngVueBridgeCode/services/searchService';
7+
import { Utilities } from '@/ngVueBridgeCode/services/utilities';
78
import { router } from '@/vueCode/router';
9+
import store from '@/vueCode/store';
10+
import { VueAngularEventBus } from '@/ngVueBridgeCode/utilities/VueAngularEventBus.js';
811

912

1013
const ngVueComponentsModule = angular.module('ngVueComponents', ['ngVue', 'ngVue.plugins']);
@@ -20,16 +23,28 @@ ngVueComponentsModule.directive('vueAppContainer',
2023
// global vue plugins
2124
ngVueComponentsModule.config(($ngVueProvider) => {
2225
$ngVueProvider.setRootVueInstanceProps({ router: router });
26+
$ngVueProvider.setRootVueInstanceProps({ store: store });
2327
});
2428

2529

2630

2731
// services
2832
ngVueComponentsModule.service('searchService', SearchService);
33+
ngVueComponentsModule.service('utilities', Utilities);
2934
export let searchService;
3035
ngVueComponentsModule.run($injector => {
3136
searchService = $injector.get('searchService');
3237
});
3338

3439

40+
41+
// enable access to Vuex from angular code
42+
ngVueComponentsModule.factory('VuexStore', [() => store]);
43+
44+
45+
46+
// create an angular factory for vue-angular communication event bus
47+
ngVueComponentsModule.factory('VueAngularEventBus', [() => VueAngularEventBus]);
48+
49+
3550
export default ngVueComponentsModule;

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
export class SearchService {
22
/** @ngInject */
3-
constructor($timeout) {
3+
constructor($timeout, VuexStore) {
44
this.$timeout = $timeout;
55
this.store = {
66
searching: false,
77
searchParam: '',
88
searchResults: [],
99
currentDetail: null
1010
};
11+
this.VuexStore = VuexStore;
1112
}
1213

13-
executeQuery (searchParam) {
14-
return this.$timeout(function () {
15-
// mock data
16-
return [
17-
{ id: 1, name: 'Result One', value: "We're all mad here. I'm mad. You're mad.", more: "What do you hear? Nothin' but the rain, sir. Grab your gun and bring the cat in." },
18-
{ id: 2, name: 'Result Two', value: 'Are you Alive? Yes. Prove it.', more: "Sometimes I've believed as many as six impossible things before breakfast." },
19-
{ id: 3, name: 'Result Three', value: 'Do you have any idea why a raven is like a writing desk?', more: "She’s right, Gaius. The end times are approaching. Humanity’s final chapters are about to be written. And you - you will be its author." }
20-
];
21-
}, 2000);
14+
executeQuery(searchParam) {
15+
return this.VuexStore.dispatch('getResults', searchParam);
2216
}
2317

2418
resolveQuery(results) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import SafeApply from '@/ngVueBridgeCode/utilities/SafeApply';
2+
3+
export class Utilities {
4+
/** @ngInject */
5+
constructor() {
6+
this.safeApply = SafeApply;
7+
}
8+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function (fn) {
2+
var phase = this.$root.$$phase;
3+
if (phase == '$apply' || phase == '$digest') {
4+
if (fn && (typeof (fn) === 'function')) {
5+
fn();
6+
}
7+
} else {
8+
this.$apply(fn);
9+
}
10+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Event bus to share events between Vue e Angular
3+
*
4+
* = Use in Vue =
5+
* import { VueAngularEventBus } from './vueAngularEventBus.js';
6+
* VueAngularEventBus.$emit('custom-event-name', params);
7+
*
8+
* = Use in Angular =
9+
* You can use it exactly as in Vue (see above) or you can wrap it into an angular factory and take advantage of dependency injection:
10+
* import { VueAngularEventBus } from './vueAngularEventBus.js';
11+
* ngVueComponentsModule.factory('VueAngularEventBus', [() => VueAngularEventBus]);
12+
*/
13+
14+
import Vue from 'vue';
15+
export const VueAngularEventBus = new Vue();

0 commit comments

Comments
 (0)