有沒有什麼方法可以檢測Backbone.Controller中的無效(或未定義)路由和觸發404頁面?如何檢測Backbone.Controller中的無效路由和觸發器功能
我在我的控制器中定義了這樣的路由,但它沒有工作。
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:
我用Backbone.Controller的構造以匹配當前散列片段和@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"
如果你已經解決了你自己的問題,然後回答你自己的問題。 – Raynos
建議是重寫您的問題,以便它只包含問題,然後提供您自己的答案。所以,請忽略問題中的答案。如果您沒有立即提供答案,您可能會發現某人有更好的方法來回答您的問題。 –
是的,你的建議是對的。感謝您分享! – tomodian