I get the last messages sent to the room which will be also displayed in the View but if a new message comes in the Strophe event handler does not fire although I see incoming message stanza over the wire (xmlInput on the connection).
What could be the reason for that?
After connecting I create the MessageList and register the Strophe handler:
Messages = new MessageList();
XMPPConnection.addHandler(Messages.onMessageReceived, null, "message", "groupchat");
XMPPConnection.send($pres({to: "room@conference.server.local/user1"}).c("x", {xmlns: "http://jabber.org/protocol/muc"}));
window.App = new AppView();
And the MVC:
// ----------------- Message Model ----------------
var Message = Backbone.Model.extend({
body: "default message",
initialize: function(body) {
if (body) {
this.set({body: body});
}
}
});
// ----------------- Message Collection ----------------
var MessageList = Backbone.Collection.extend({
model: Message,
initialize: function(body) {
},
onMessageReceived: function(body) {
var message = new Message($(body).text());
Messages.add(message);
return true;
}
});
// ---------------- Message View -------------------
var MessageView = Backbone.View.extend({
tagName: "li",
template: _.template($("#message-template").html()),
events: {},
initialize: function() {
this.model.bind("change", this.render, this);
// this.model.bind("remove", this.remove, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
// ---------------- App View -------------------
var AppView = Backbone.View.extend({
el: $("#myapp"),
initialize: function() {
Messages.bind("add", this.addOneMessage, this);
this.main = $('#main');
},
addOneMessage: function(message) {
var view = new MessageView({model: message});
this.$("#message-list").append(view.render().el);
},
});
There are a few things going wrong, here are some tips:
Your
Messagemodel should be written as:there’s no need for the initialize, if you pass attributes they will be assigned.
Your collection:
I would also put the onMessageReceived outside the the MessageList:
Your MessageView can be simpler, MUC messages do not change, they just get added:
Hopefully this gives you enough to go on…