When a user goes to http://example.com/#/playlist
, I want to list some songs.
It works (all songs are listed) when you first go to http://example.com
and click on the link <a href="#/playlist">Playlist</a>
.
But if you directly go to http://example.com/#/playlist
, nothing is shown.
When I log the this.collection property, the collection is always the same (if you use the link, or directly access).
The only difference is that this.collection.each( function ( song ) {} );
in the render-function below, doesn't loop when directly accessing the URL.
This is the code:
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;
}
);
The problem occurs here, in the 'each-loop':
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
This is my routing code:
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
};
}
);