N
N
Nikita Lubchich2014-08-14 12:21:12
JavaScript
Nikita Lubchich, 2014-08-14 12:21:12

Why can't I specify a collection for the view?

var Chat = Backbone.Model.extend({
messageList: new MessageList(),
messageListView: new MessageListView({collection: this.messageList}),
 
switchChannel: function(channel) {
this.messageList.fetch({url: 'channels/'+channel, reset: true})
}
 
});
 
 
// in other file
var MessageList = Backbone.Collection.extend({
model: Message,
})
 
var MessageListView = Backbone.View.extend({
initialize: function(){
console.log(this.collection)
this.listenTo(this.collection, 'reset', this.render);
},

console.log(this.collection) -> undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MCBumf, 2014-08-14
@Cybran

In this place:

var Chat = Backbone.Model.extend({
  messageList: new MessageList(),
  messageListView: new MessageListView({collection: this.messageList}),
...

this refers to window, not the Chat instance. The messageList and messageListView objects must be created when the model is initialized:
var Chat = Backbone.Model.extend({
  initialize: function(){
    this.messageList = new MessageList();
    this.messageListView = new MessageListView({collection: this.messageList});
}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question