2012-12-03 3 views
0

저는 js 및 backbone.js를 처음 사용하고 있으므로 문서와 여기에서 광범위하게 검색했음을 알려드립니다. 사진 (결국 다른 것들)을 표시하는 간단한 사이트를 만들고 다른보기/템플릿과 다른 '페이지'를보기 위해 사진을 클릭 할 수 있기를 원합니다. 나는 이것이 작동하지 않는 것 같아요. 이제는 "Uncaught TypeError : 라우터의 photoById 메소드에서 '정의되지 않은'get '메서드를 호출 할 수 없습니다. 아래 코드 ... 감사합니다!backbone.js 라우팅 문제

// ------ main.js

var photoListTemplate = Handlebars.compile($('#photo-list-template').html()); 
var photoTemplate = Handlebars.compile($('#photo-template').html()); 
var navTemplate = Handlebars.compile($('#navigation-template').html()); 

var Photo = Backbone.Model.extend({ 
    defaults: function() { 
    return { 

    }; 
    }, 
    initialize: function(options) { 

    } 
}); 
var PhotoCollection = Backbone.Collection.extend({ 
    model: Photo 
}); 

var PhotoView = Backbone.View.extend({ 
    events: { 

    }, 

    //tagName: 'td', 
    className: 'photo', 
    template: photoTemplate, 

    initialize: function(options) { 
     _.bindAll(this, 'render'); 
     this.model.on('change', this.render); 
    }, 
    render: function() { 
     $(this.el).empty().append(this.template(this.model.toJSON())); 
     return this; 
    } 
}); 

var PhotoCollectionView = Backbone.View.extend({ 
    tagName: 'table', 
    className: 'photo-list', 
    template: photoListTemplate, 
    events: { 

    }, 
    initialize: function(options) { 
     this.collection.on('add remove', this.render, this); 
    }, 
    render: function() { 
     $(this.el).empty(); 
     this.collection.each(function(photo){ 
      $(this.el).append(new PhotoView({model: photo}).render().el); 
     }, this); 
     return this; 
    } 
}); 

var PhotoRouter = Backbone.Router.extend({ 
    routes: { 
     "": "list", 
     "photo/:id": "photoById" 
    }, 
    initialize: function(options) { 

    }, 

    allPhotos: function() { 
     this.photoList = new PhotoCollection(); 
     this.photoCollectionView = new PhotoCollectionView({collection: this.photoList}); 
     this.photList.fetch(); 
     $('#content').empty().append(this.photoCollectionView.render().el); 
    }, 

    photoById: function(id){ 
     this.photo = this.photoList.get(id); 
     this.photoView = new PhotoView({model: this.photo}); 
     $('#content').empty().append(this.photoView.render().el); 
    } 
}); 

var photos = null; 
$.getJSON('http://localhost:3000/photos.json', function(response){ 
    photos = new PhotoCollection(response); 
    $('#container').append(new PhotoCollectionView({collection: photos}).render().el); 
    var photoRouter = new PhotoRouter(); 
    Backbone.history.start(); 
}); 

/----- index.html을 거기

<!DOCTYPE html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title></title> 
    <meta name="description" content=""> 
    <meta name="viewport" content="width=device-width"> 

    <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> 

    <link rel="stylesheet" href="css/normalize.css"> 
    <link rel="stylesheet" href="css/bootstrap.css"> 

    <link rel="stylesheet" href="css/main.css"> 
    <script src="js/vendor/modernizr-2.6.2.min.js"></script> 
</head> 
<body> 
    <!--[if lt IE 7]> 
     <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p> 
    <![endif]--> 

    <!-- Add your site or application content here --> 
    <!-- <p>Replace Me! Replace Me!</p> --> 

    <div class='container' id='container'> 
    </div> 

    <script type="text/x-handlebars-template" id='navigation-template'> 
     <div class='navbar'> 
      <div class='navbar-inner'> 
       <a class='brand' href=#>ohayon</a> 
       <ul class='nav'> 

       </ul> 
      </div> 
     </div> 
    </script> 

    <script type="text/x-handlebars-template" id='photo-template'> 
     <div class='lead'> 
     <a href='#photo/{{id}}'>{{name}}</a> 
     <img src='http://localhost:3000{{image.url}}' height='200' width='200'> 
     </div> 
    </script> 

    <script type="text/x-handlebars-template" id='photo-list-template'> 
     <div class='lead'> 
     <a href='#photos/{{id}}'>{{name}}</a> 
     <img src='http://localhost:3000{{image.url}}'> 
     </div> 
    </script> 

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.2.min.js"><\/script>')</script> 
    <script src="js/plugins.js"></script> 

    <!-- If we want Boostrap components --> 
    <script src="js/bootstrap.js"></script> 

    <!-- Templating with Handlebars --> 
    <script src="js/handlebars.js"></script> 

    <!-- We Need Underscore.js --> 
    <script src="js/underscore.js"></script> 

    <!-- And Finally Backbone --> 
    <script src="js/backbone.js"></script> 

    <!-- Your Code goes here --> 
    <script src="js/main.js"></script> 

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. --> 
    <script> 
     var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']]; 
     (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0]; 
     g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js'; 
     s.parentNode.insertBefore(g,s)}(document,'script')); 
    </script> 
</body> 
</html> 

답변

2

당신이 코딩있어 방법, 아니 보증 this.photoListphotoById에서 사용하려고 할 때 생성됩니다. - 당신은 PhotoCollection을 다시 만들거나 fetch를 호출하지 않으려는, 이미 만들어지고 채워 이후

initialize: function(options) { 
    this.photoList = photos; 
}, 

는 또한 allPhotos에서 몇 줄을 제거 : 당신은 라우터의 init 함수에서 그것을 만들 수 있습니다 앱의 첫 단계. 다음과 같아야합니다

allPhotos: function() { 
    this.photoCollectionView = new PhotoCollectionView({collection: this.photoList}); 
    $('#content').empty().append(this.photoCollectionView.render().el); 
}, 

It's running here.

은 (내가 함께 테스트하기 위해 몇 가지 더미 데이터를 사용했다.) (, 가끔 containercontent 다른 사람을 사용하는 또 다른 문제.)