具有Ember和Ember-Data的Socket.IO [英] Socket.IO with Ember and Ember-Data
问题描述
App.ApplicationRoute = Ember.Route.extend({
setupController:function (控制器,数据){
var socket = io.connect(),
self = this;
socket.on('apartment / new',function(apartment){
var apt = App.Apartment.createRecord(apartment);
self.controllerFor('apartments')。pushObject(apt);
});
}
});
这实际上将创建一个新的模型类,它将对象推送到控制器,并创建一个新的li但值不会呈现。
< ul class =list-view>
{{#each apartment in controller}}
< li>
{{#linkTo'apartment'apartment}}
< span class =date> {{date apartment.date}}< / span>
{{apartment.title}}
{{/ linkTo}}
< / li>
{{/ each}}
< / ul>
这与运行循环有关吗?如何强制渲染值?或者有更好的方法吗?
有一个非常简单的解决方案,我在一些我使用应用。您可以为套接字提供通用回调,并接受任何类型的数据
callback:function(message){
//这比只是`eval`
var type = Ember.get(Ember.lookup,message.type)更好
store.load(type,message.data);
}
或者这里是专门为您的用例量身打造的
socket.on('apartment / new',function(apartment){
store.load(App.Apartment,apartment);
});
使用 store.load
将加载记录数据直接进入身份图。还有 loadMany
用于加载多个记录。
I've been poking around and I can't find any up to date examples of ember (1.0.0-rc.1) and ember-data(revision 11) that also use socket.io. I've tried something like this.
App.ApplicationRoute = Ember.Route.extend({
setupController: function(controller, data) {
var socket = io.connect(),
self = this;
socket.on('apartment/new', function(apartment) {
var apt = App.Apartment.createRecord(apartment);
self.controllerFor('apartments').pushObject(apt);
});
}
});
This actually will create a new model class, it pushes the object to the controller, and creates a new li but the values do not render.
<ul class="list-view">
{{#each apartment in controller}}
<li>
{{#linkTo 'apartment' apartment }}
<span class="date">{{date apartment.date}}</span>
{{apartment.title}}
{{/linkTo}}
</li>
{{/each}}
</ul>
Does this have something to do with the run loop? How do force the values to render? Or is there a better approach to this?
There's a very simple solution to this which I'm using in some of my apps. You can either have a general purpose callback for the socket and accept any kind of data
callback: function(message) {
// this is better than just `eval`
var type = Ember.get(Ember.lookup, message.type);
store.load(type, message.data);
}
or here it is specifically tailored to your use case
socket.on('apartment/new', function(apartment) {
store.load(App.Apartment, apartment);
});
using store.load
will load the record data directly into the identity map. There's also loadMany
for loading multiple records.
这篇关于具有Ember和Ember-Data的Socket.IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!