Czy istnieje metoda wykrywania nieprawidłowej (lub niezdefiniowanej) trasy i wywołania strony 404 w Backbone.Controller?Jak wykryć nieprawidłową funkcję trasy i wyzwalacza w Backbone.Controller
Mam zdefiniowane trasy w moim sterowniku w ten sposób, ale to nie zadziałało.
class MyController extends Backbone.Controller
routes:
"method_a": "methodA"
"method_b": "methodB"
"*undefined": "show404Error"
# when access to /#method_a
methodA: ->
console.log "this page exists"
# when access to /#method_b
methodB: ->
console.log "this also exists"
# when access to /#some_invalid_hash_fragment_for_malicious_attack
show404Error: ->
console.log "sorry, this page does not exist"
UPDATE:
kiedyś konstruktora Backbone.Controller dopasować bieżącego fragmentu mieszania i @routes.
class MyController extends Backbone.Controller
constructor: ->
super()
hash = window.location.hash.replace '#', ''
if hash
for k, v of @routes
if k is hash
return
@show404Error()
routes:
"method_a": "methodA"
"method_b": "methodB"
"*undefined": "show404Error"
# when access to /#method_a
methodA: ->
console.log "this page exists"
# when access to /#method_b
methodB: ->
console.log "this also exists"
# when access to /#some_invalid_hash_fragment_for_malicious_attack
show404Error: ->
console.log "sorry, this page does not exist"
Jeśli rozwiązałeś swój problem, odpowiedz na własne pytanie. – Raynos
Proponujemy przepisanie Twojego pytania tak, aby zawierało tylko pytanie, a następnie podaj własną odpowiedź. Pomiń więc odpowiedź w pytaniu. Może się okazać, że ktoś ma lepszy sposób na udzielenie odpowiedzi na twoje pytanie, jeśli nie podasz natychmiastowej odpowiedzi. –
Tak, Twoja sugestia jest właściwa. Dziękujemy za udostępnienie! – tomodian