Ember.js EmberJS:在主控制器上使用SSE支持的模型直接在主细节应用程序中加载子URL
我试图开发这个单模块应用程序,在两条路线之间建立主-细节关系。主服务器应该有一个模型,该模型最初被加载,随后用一个服务器端事件入口点进行更新。到目前为止,我有以下代码:Ember.js EmberJS:在主控制器上使用SSE支持的模型直接在主细节应用程序中加载子URL,ember.js,frontend,master-detail,server-sent-events,Ember.js,Frontend,Master Detail,Server Sent Events,我试图开发这个单模块应用程序,在两条路线之间建立主-细节关系。主服务器应该有一个模型,该模型最初被加载,随后用一个服务器端事件入口点进行更新。到目前为止,我有以下代码: var App = Ember.Application.create({ rootElement: '#content' }); App.Router.map(function() { this.resource('receptores', {path: '/'}, function() { t
var App = Ember.Application.create({
rootElement: '#content'
});
App.Router.map(function() {
this.resource('receptores', {path: '/'}, function() {
this.resource('receptor', {path: ':user_id'});
});
});
App.ReceptoresController = Ember.Controller.extend({
init: function() {
var sse = new EventSource('/push');
var controller = this;
sse.addEventListener('update-hhs', function(e) {
controller.set('model', JSON.parse(e.data).receptores);
});
}
});
App.ReceptorRoute = Ember.Route.extend({
model: function(params) {
return this.modelFor('receptores').findBy('id', params.user_id);
}
});
加载“receptores”起作用,当前数据加载良好。单击链接以http://localhost:5003/#/skkkd
,例如,将加载“接受器”路线和详细信息数据(详细信息只是加载到主机上的相同数据的子集)
如果我尝试在详细url处重新加载或输入http://localhost:5003/#/skkkd
我将直接得到以下异常:
Error while loading route: TypeError: Cannot read property 'findBy' of undefined
at App.ReceptorRoute.Ember.Route.extend.model (http://localhost:5003/monitoreo/static/js/receptores.js:34:43)
at superWrapper [as model] (http://localhost:5003/static/js/ember-1.5.1.js:1292:16)
at Ember.Route.Ember.Object.extend.deserialize (http://localhost:5003/static/js/ember-1.5.1.js:36570:19)
at http://localhost:5003/static/js/ember-1.5.1.js:32972:57
at http://localhost:5003/static/js/ember-1.5.1.js:33464:19
at invokeResolver (http://localhost:5003/static/js/ember-1.5.1.js:9646:9)
at new Promise (http://localhost:5003/static/js/ember-1.5.1.js:9632:9)
at Router.async (http://localhost:5003/static/js/ember-1.5.1.js:33463:16)
at Object.HandlerInfo.runSharedModelHook (http://localhost:5003/static/js/ember-1.5.1.js:32971:16)
at Object.UnresolvedHandlerInfoByParam.getModel (http://localhost:5003/static/js/ember-1.5.1.js:33058:19)
我知道问题是没有为ReceptoresController调用init hook。我认为我应该在ReceptoresRoute或控制器上实现一个模型钩子,并通过jQuery.get()加载初始数据,只更新服务器端事件。但是我在哪里初始化SSE呢
编辑:
结果我误解了模型钩子在子路由上的工作原理。在我的例子中,我通常通过链接到的链接查看详细信息页面,因此Ember已经为receptor
提供了模型。在这种情况下,永远不会调用模型挂钩。只有在尝试直接访问详细信息页面时才会调用它,而这正是它失败的地方。所以我的问题仍然存在
我的整个问题归结为我应该将SSE初始化放在何处,以便两条路由都可以访问相同的模型,而不管加载了哪个。好的,这是我的解决方案。从这里的几个问题拼凑而成。即和。希望它能帮助别人
var App = Ember.Application.create({
rootElement: '#content'
});
App.Router.map(function() {
this.resource('receptores', {path: '/'}, function() {
this.resource('receptor', {path: ':user_id'});
});
});
App.ReceptoresRoute = Ember.Route.extend({
model: function() {
var deferredData = Ember.Deferred.create();
var data = [];
var sse = new EventSource('/push');
sse.addEventListener('update-hhs', function(e) {
var receptores = JSON.parse(e.data).receptores;
receptores.forEach(function(r) {
var item = data.findBy('id', r.id);
if (typeof(item) != 'undefined') {
data.replace(data.indexOf(item), 1, [r]);
} else {
data.pushObject(r);
data.sortBy('full_name');
}
});
deferredData.resolve(data);
});
return deferredData;
}
});
App.ReceptorRoute = Ember.Route.extend({
model: function(params) {
return this.modelFor('receptores').findBy('id', params.user_id);
}
});