2012-06-12 4 views
1

간단한 라우터를 정의하고 인스턴스화했습니다.backbone.js 라우터가 URL 조각에 응답하지 않습니다.

문제점 : URL http://localhost/backbone1/#photos/5에 갈 때, 나는 자바 스크립트 콘솔 console.log()의 출력을 볼 것으로 예상하지만, 아무것도가 표시되지 않습니다. 내가 뭔가 놓친거야?

JS 코드

var GalleryRouter = Backbone.Router.extend({ 

    routes: { 
     "about": "showAbout", 
     "photos/:id": "getPhoto", 
     "search/:query": "searchPhotos", 
     "search/:query/p:page": "searchPhotos", 
     "photos/:id/download/*imagePath": "downloadPhoto", 
     "*other": "defaultRoute" 
    }, 

    showAbout: function() { 

    }, 

    getPhoto: function(id) { 
     console.log('You are trying to reach photo ' + id); 
    }, 

    searchPhotos: function(query, page) { 
     console.log('Page number: ' + page + ' of the results for ' + query); 
    }, 

    downloadPhoto: function(id, imagePath) { 

    }, 

    defaultRoute: function(other) { 
     console.log("Invalid. You attempted to reach: " + other); 
    } 



}); 

var myGalleryRouter = new GalleryRouter(); 

답변

0

가 누락되었습니다. 일반적으로 필수 데이터를로드하고 전역보기를 초기화 한 다음 라우터를 시작하려고합니다.

1

당신은 라우터 당신이 Backbone.history.start()를 호출 한 후 해시 변화를 URL로 듣기 시작하는 초기화 방법

var initialize = function() { 
      var myGalleryRouter = new GalleryRouter(); 
      Backbone.history.start(); 
     }; 
return { 
     initialize: initialize 
    }; 
관련 문제