나는 당신이 틀린 것을 무시하고 있다고 생각합니다 : 그 navigate
은 당신이 생각하는대로 사용되지 않습니다.
우리가 Backbone.history.start
의 일부를 살펴 보자 : 당신은 백본에서 라우팅을 처리하는 모든 다양한 방법 checkUrl
보다는 navigate
통해 갈 것을 볼 수 있습니다
// Start the hash change handling, returning `true` if the current URL matches
// an existing route, and `false` otherwise.
start: function(options) {
//...
if (this._hasPushState) {
Backbone.$(window).on('popstate', this.checkUrl);
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
Backbone.$(window).on('hashchange', this.checkUrl);
} else if (this._wantsHashChange) {
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
}
. checkUrl
메서드는 약간 바쁜 작업을 수행하며 loadUrl
을 호출합니다. 하나 part of the busy work is this :
if (this.iframe) this.navigate(current);
그래서 navigate
가 호출됩니다 있지만 <iframe>
은 당신이 IE의 이전 버전을 사용하는 경우에만 발생 hashchange
및 popstate
이벤트 및 AFAIK을 시뮬레이션하는 데 사용되는 경우에만 사용할 수 있습니다.
코드를 통해 일반적인 경로로 돌아갑니다. 우리는 checkUrl
이 바쁜 일을하고 loadUrl
을 호출하는 것을 보았습니다. loadUrl
does this :
loadUrl: function(fragmentOverride) {
var fragment = this.fragment = this.getFragment(fragmentOverride);
var matched = _.any(this.handlers, function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;
}
});
return matched;
}
당신이 route
method in History
와 볼 경우 route
method in Route
당신이 handler.callback
이 라우터 중 하나에서 경로 핸들러를 호출하고 라우팅 이벤트를 트리거 것입니다 것을 볼 수 있습니다. 당신이 경로 처리기가 호출되기 전에 재 지정하려면,이 같은 뭔가 loadUrl
을 대체 할 수
navigate: function(fragment, options) {
Backbone.history.navigate(fragment, options);
return this;
}
: : 교체하고
navigate
방법은 거의에만 Router
's navigate
에 의해 사용되는
(function(History) {
var _loadUrl = History.prototype.loadUrl;
_.extend(History.prototype, {
loadUrl: function() {
var args = [].slice.apply(arguments);
args[0] = this.getFragment(args[0]);
// If args[0] is the fragment that you want to
// redirect then replace it here or do whatever
// needs to be done.
return _loadUrl.apply(this, args);
}
});
})(Backbone.History);
데모 : http://jsfiddle.net/ambiguous/e4KYK/
전반적으로 당신이 더 나은 것 같아요 off : 정상적인 경로 처리기에서 리디렉션을 처리합니다. 문제가되는 경로가 호출되면 라우터가 무엇이든간에 navigate
을 호출하면됩니다.
는
start
method 그래서 여기에 모든 것이 변경 될 수 있습니다 넘어
Backbone.History
이 문서화되어 있지 않습니다 있음을 유의하십시오.
Backbone.history.start()를 어딘가에 호출했다고 가정합니다. – WiredPrairie
예 - 저의 견해가 바뀌고 있습니다. 왜 이것이 작동하지 않는 지 전혀 알지 못합니다. – user606521
다른 것이 틀림 없습니다.방금 공개 한 백본 프로젝트에 코드를 연결했는데 탐색 한 순간 즉시 'adad'경고가 표시되었습니다. :) 나는 행동을 바꿀지도 모르는 내 애플 리케이션에 연결된 코드를 "네비게이션"하기위한 상용구 코드 앵커 태그를 가지고있다. – WiredPrairie