HM-31. Добавлен роутинг для страниц (#9)
This commit is contained in:
41
src/services/RouteService.js
Normal file
41
src/services/RouteService.js
Normal file
@ -0,0 +1,41 @@
|
||||
import EmitService from './EmitService';
|
||||
import {parse} from 'querystring';
|
||||
|
||||
const ROUTE_CHANGE = 'routeChange';
|
||||
|
||||
class RouteService extends EmitService {
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this.history = window.history;
|
||||
this._events[ROUTE_CHANGE] = [];
|
||||
}
|
||||
|
||||
getUrlData = () => {
|
||||
return {
|
||||
url: location.pathname,
|
||||
query: parse(location.search.slice(1)),
|
||||
};
|
||||
}
|
||||
|
||||
generateNext = () => {
|
||||
this.next(ROUTE_CHANGE, this.getUrlData());
|
||||
}
|
||||
|
||||
init = () => {
|
||||
this.generateNext();
|
||||
}
|
||||
|
||||
goTo = (url) => {
|
||||
this.history.pushState({}, '', url);
|
||||
this.generateNext();
|
||||
}
|
||||
|
||||
onChange = (listener) => {
|
||||
this.subscribe(ROUTE_CHANGE, listener);
|
||||
}
|
||||
}
|
||||
|
||||
const routeService = new RouteService();
|
||||
|
||||
export default routeService;
|
||||
Reference in New Issue
Block a user