Rails+Backbone+Faye 消息传递,如何为所有订阅者实例化模型并从 DOM 中删除其元素?
我正在使用 Rails + Backbone + Faye 制作一个示例聊天应用程序。
我目前能够使用 Faye 的消息传递功能在创建事件上写入 DOM,尽管我实际上并未实例化骨干模型。 Ala Ryan Bates 的教程我只是在
create.js.erb
<% broadcast "/messages/new" do %>
$("#chats-table").append("<%= escape_javascript render :partial => "chat", :locals => { :chat => @chat } %>");
<% end %>
内部调用并用另一个 javascript 发布它:
faye.subscribe("/messages/new", function(data) {
eval(data);
});
我想稍微重构一下并利用骨干的模型。一个很好的用例是删除方法。
我的聊天模型绑定到一个点击事件,删除调用:
model.destroy();
this.remove();
Backbone 将调用删除方法并向 /entity/id 发出删除请求,
这也会调度rails' /views/delete.js.erb'。 在那里我调用了一个辅助方法,它使用 Ruby 代码发布消息。
<% broadcast "/messages/delete" do %>
<%= @chat.to_json.html_safe; %>
<% end %>
在
var faye = new Faye.Client('http://0.0.0.0:9292/faye');
faye.subscribe("/messages/delete", function(data) {
});
这里,我想知道是否可以以某种方式实例化已删除的骨干模型,以便我可以将该事件推送到每个人的屏幕上并将其从 DOM 中删除。基本上,我想调用 this.remove();在 faye 客户端内部而不是在聊天模型中。这可能吗?
I am using Rails + Backbone + Faye to make a sample chat application.
I'm currently able to use Faye's messaging capabilities to write to the DOM on a create event, though I'm not actually instantiating a backbone model. Ala Ryan Bates' tutorial I'm just calling inside of
create.js.erb
<% broadcast "/messages/new" do %>
$("#chats-table").append("<%= escape_javascript render :partial => "chat", :locals => { :chat => @chat } %>");
<% end %>
And publishing it in another javascript:
faye.subscribe("/messages/new", function(data) {
eval(data);
});
I'd like to refactor this a bit and leverage backbone's models. A good use case would be the delete method.
My chat model is bound to a click event, delete which calls:
model.destroy();
this.remove();
Backbone will call the delete method and put a delete request to /entity/id
That also dispatches rails' /views/delete.js.erb'.
In there I call a helper method which publishes a message with Ruby code.
<% broadcast "/messages/delete" do %>
<%= @chat.to_json.html_safe; %>
<% end %>
listener
var faye = new Faye.Client('http://0.0.0.0:9292/faye');
faye.subscribe("/messages/delete", function(data) {
});
Here, I was wondering if i could instantiate the deleted backbone model somehow so I could push that event onto everybody's screen and remove it from the DOM. Basically, I would like to call this.remove(); inside the faye client instead of in the chat model. Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您应该删除模型并让 UI 侦听事件并自行刷新。一旦 UI 反映了模型的变化,你就成功了。
这里的问题是 Backbone 集合/模型不是身份映射。因此,您在视图中处理的模型对象与您将实例化并从 faye 回调中删除的模型对象不同。如果您的消息集合是全局可访问的,那么我建议您从那里获取实例并将其删除。
Well, you should do remove on the model and let the UI listen for the event and refresh itself. Once the UI reflects the model changes you are golden.
The problem you have here is that Backbone collections/models are not an identity map. So the model object you are dealing with in the view is not the same you will instantiate and remove from the faye callback. If your messages collection is globally accessible, then I suggest you get the instance from there are remove it.