【问题标题】:Backbone and Strophe muc plugin: Handle incoming eventsBackbone 和 Strophe muc 插件:处理传入事件
【发布时间】:2012-06-27 07:55:32
【问题描述】:

我收到了发送到房间的最后一条消息,这些消息也将显示在视图中,但是如果有新消息进入 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);
    },
 });

【问题讨论】:

    标签: backbone.js xmpp strophe


    【解决方案1】:

    有一些地方出了问题,这里有一些提示:

    你的Message 模型应该写成:

    var Message = Backbone.Model.extend({
      defaults: {
          body: "default message",
      }
    });
    

    不需要初始化,如果你传递属性,它们将被分配。

    您的收藏:

    var MessageList = Backbone.Collection.extend({
        model: Message,
    });
    

    我还将 onMessageReceived 放在 MessageList 之外:

     var onMessageReceived = function(body) {
         Messages.add({body: $(body).text()});
         return true;
     }           
    

    您的 MessageView 可以更简单,MUC 消息不会改变,它们只是被添加:

    var MessageView = Backbone.View.extend({
      tagName: "li",
    
      template: _.template($("#message-template").html()),
      render: function() {
          this.$el.html(this.template(this.model.toJSON()));
          return this;    
      }        
    });  
    

    希望这能让你继续前进……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多