이 문제에 대해 많은 논란이있었습니다. 라우터를 공식적으로 컨트롤러라고 부르는 것이 혼란이라고 생각합니다. 작년 말 백본을 사용하기 시작했을 때 이미 변경이 있었지만 많은 사람들이 이미 라우터를 중심으로 컨트롤러를 구축하고 있다고 생각합니다. 나는 결코 그것에 동의하지 않았다. Backbone이 만들어지기 오래 전부터 Backbone과 비슷한 독점적 인 MVC 엔진을 구축 한 경험에 의존하여 - 라우터는 단순히 내역 관리자 구성 요소였습니다.
그래서 가장 좋은 몇 가지를, 라우터를 구현 고려하는 방법 결정에 특정 문제를 해결하기 위해 : 모든
- 첫째, 라우터는 응용 프로그램의 필수 구성 요소가 아닙니다. 라우터 없이도 앱의 다양한 페이지 나 화면으로 이동할 수 있습니다.
- 라우터는 주 기능이 기록을 관리한다는 점에서 컨트롤러가 아닙니다. 애플리케이션 비즈니스 로직을 라우터에 내장 할 수는 있지만, 라우터가 실제로하는 일의 물을 왜곡하는 것으로 항상 깨달았습니다.
- 라우터를 응용 프로그램의 구성 요소로 만들면 우려 사항을 효과적으로 분리 할 수 있으며 훨씬 더 효과적인 pub/sub 유형의 배열을 가질 수 있습니다.다음은
는 중재자, 술집/하위 패턴 다음 내 응용 프로그램에서 사용하는 라우터 모듈에 대한 코드입니다 : 내가 구성 요소를 인스턴스화 할 때, 나는 그것을 있도록지도를 통과 그런
/**
* The idea behind this component is simply to relegate Backbone.Router to
* doing what it does best: History Management. All it is responsible for
* is two things:
*
* 1. Update the URL if router.navigate is invoked
* 2. Trigger a routeChanged event if the URL was updated either by a bookmark or
* typed in by a user.
*/
define(function() {
return Backbone.Router.extend({
initialize : function (map) {
this._reversedMap = this.reverseModuleMap(map);
},
routes:{
'*actions':'notify'
},
notify:function (actions) {
var args = arguments;
this.trigger("routeChanged", {command:actions});
},
/**
* Override Backbone.Router.navigate. Default is to pass a router fragment, but for
* our uses, we'll translate the "route" into a module mapping so that the controller
* will know which module to display.
* @param param
* @param options
*/
navigate:function (param, options) {
//console.log('navigate', param);
if(!param.suppressNavigate && param.actionCommand) {
Backbone.Router.prototype.navigate.call(this, this._reversedMap[param.actionCommand]);
} else if(!param.actionCommand) {
Backbone.Router.prototype.navigate.call(this, param, options);
}
},
/**
* this function simply reverses the key and value of the "map"
* @param map
*/
reverseModuleMap:function (map) {
var newMap = {};
_.each(map, function (value, key) {
newMap[value] = key;
});
// reversed key and value
return newMap;
}
});
});
을 내 컨트롤러는 모듈로 이동합니다 알고 :
this._router = new Router({
_default: 'moduleA',
sites : 'moduleA',
apps : 'moduleB'
});
this._router.on('routeChanged', this.handleRouteChange, this);
나는이 발견 한 가장 중요한 것은이 코드 좋은 및 간단한 유지한다는 것입니다, 그리고 내 컨트롤러에서 비즈니스 로직에 집중할 수 있습니다.