我收到了发送到房间的最后一条消息,这些消息也将显示在视图中,但是如果有新消息进入 Strophe 事件处理程序,则尽管我通过线路看到传入的消息节(连接上的 xmlInput),但不会触发。
这可能是什么原因?
连接后,我创建 MessageList 并注册 Strophe 处理程序:
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();
和 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);
},
});