사용자가 http://example.com/#/playlist
으로 이동하면 일부 노래를 나열하려고합니다.백본 직접 URL 액세스가 모음을 깨뜨림
처음에 http://example.com
으로 이동하여 <a href="#/playlist">Playlist</a>
링크를 클릭하면 (모든 노래가 나열됩니다) 작동합니다.
그러나 직접 http://example.com/#/playlist
으로 가면 아무 것도 표시되지 않습니다.
this.collection 속성을 기록하면 컬렉션이 항상 동일합니다 (링크를 사용하거나 직접 액세스하는 경우).
유일한 차이점은 아래의 렌더링 기능에서 this.collection.each(function (song) {});
이며 URL에 직접 액세스 할 때 루프되지 않습니다.
define(
[
'jQuery',
'Underscore',
'Backbone',
'collections/songlist',
'views/song'
],
function ($, _, Backbone, SonglistCollection, SongView)
{
var PlaylistView = Backbone.View.extend({
// properties
el: '#content',
tagName: 'ul',
collection: new SonglistCollection(),
/**
* Initialize
*/
initialize: function()
{
// load songs
this.collection.bind('reset', this.render(), this);
this.collection.fetch();
},
/**
* Render
*/
render: function()
{
this.$el.html('');
console.log(this.collection);
this.collection.each(function (song)
{
var songItem = new SongView({ model: song });
this.$el.append(songItem.el);
}, this);
}
});
return new PlaylistView;
}
);
이 문제는에서, 여기에 '각 루프'을 발생합니다 :
render: function()
{
this.$el.html('');
console.log(this.collection);
this.collection.each(function (song)
{
var songItem = new SongView({ model: song });
this.$el.append(songItem.el);
}, this);
}
UPDATE이 내 라우팅 코드
이
코드입니다 :define([
'jQuery',
'Underscore',
'Backbone',
'views/start',
'views/playlist'
],
function ($, _, Backbone, startView, playlistView)
{
var AppRouter = Backbone.Router.extend({
routes: {
'playlist': 'playlist',
// default
'*actions': 'start'
},
start: function()
{
// call render on the module we loaded via the dependency array
// views/items/list
startView.render();
},
playlist: function()
{
playlistView.render();
},
defaultAction: function (actions)
{
// no default action, let's just log what the url was
console.log('No route:', actions)
}
});
var initialize = function()
{
var app_router = new AppRouter;
Backbone.history.start();
}
return {
initialize: initialize
};
}
);
라우팅 코드를 추가 할 수 있습니까? – codemonkey
라우팅 코드를 사용하여 게시물을 수정했습니다. 미리 감사드립니다. – Voles
밑줄로 각 방법을 사용하고 있습니까? 나는 그것이'_.each (this.collection.models, function (sont) {// code}, this); ' – Jack