Backbone.js 重写Marionette.js模型中的获取(并响应推送更新)
这是一个由两部分组成的问题。我们有一个Marionette.js应用程序,在初始加载时,我们需要“订阅”一个Lightstreamer实例并保持该实例/连接打开(Lightstreamer是什么并不重要,只是它将更新发送到页面)。后续型号更新通常不会由用户操作触发,而是由Lightstreamer的更新触发。我不知道如何设计这个 首先,似乎默认的model fetch()操作向模型的url属性发出web请求,并基于用户操作同步检索数据(在大多数示例中)。对于我们来说,fetch本质上是“subscribe”操作,初始数据(消息集合)将异步返回。我已经看到了使用jquery deferred的建议,但我不确定done()函数的位置(在模型中?在模块中?) 其次,我们似乎会以某种方式使用wreqr来注册推送更新,但我也不确定这会是什么样子 以下是一些psuedo代码:Backbone.js 重写Marionette.js模型中的获取(并响应推送更新),backbone.js,marionette,Backbone.js,Marionette,这是一个由两部分组成的问题。我们有一个Marionette.js应用程序,在初始加载时,我们需要“订阅”一个Lightstreamer实例并保持该实例/连接打开(Lightstreamer是什么并不重要,只是它将更新发送到页面)。后续型号更新通常不会由用户操作触发,而是由Lightstreamer的更新触发。我不知道如何设计这个 首先,似乎默认的model fetch()操作向模型的url属性发出web请求,并基于用户操作同步检索数据(在大多数示例中)。对于我们来说,fetch本质上是“subs
MyApp.Lightstreamer = codeToGetTheLightstreamerWiredUp();
MyApp.MyCollection = Backbone.Collection.extend({
model: MyApp.Message,
initialize: function(models, options) {
this.id = options.id;
},
fetch: function () {
MyApp.Lightstreamer.Do('subscribeUpdate'), {
adapterName: 'AdapterName',
parameterValue: this.id,
otherStuff: 'otherstuff',
onUpdate: callbackFunction
}
});
MyApp.module("MyApp"...) {
MyApp.Router = Marionette.AppRouter.extend(
{
appRoutes: { 'controller/view/:id', 'subscribe' }
}
);
var Api = {
subscribe: function(id) {
var messages = new MyApp.MyMessages({ id: id });
var deferred = $.Deferred();
messages.fetch({
success: deferred.resolve
}
}
};
QaApp.addInitializer(function () {
var router = new QaApp.Router(
{
controller: Api
});
});
}
谢谢 主干网的本机
fetch
实现可以提供一个模板供您遵循。在正常提取期间,主干调用sync
,传入以下成功回调:
options.success = function(resp) {
var method = options.reset ? 'reset' : 'set';
collection[method](resp, options);
if (success) success(collection, resp, options);
collection.trigger('sync', collection, resp, options);
};
基本上,当主干网从服务器获得响应时,它将使用该响应填充集合。由于您直接从服务器获得定期更新,因此需要使用LightStreamer的onUpdate
参数执行类似的操作。主干调用set
或reset
reset
将清除集合,这听起来不太合适,因此您可以删除条件,只需调用set
。触发sync
是可选的,但为了最大限度地兼容木偶,我建议使用它。我不认为你需要担心延期,除非我还缺少其他要求
以下是我的建议:
MyApp.Lightstreamer = codeToGetTheLightstreamerWiredUp();
MyApp.MyCollection = Backbone.Collection.extend({
model: MyApp.Message,
initialize: function(models, options) {
this.id = options.id;
// not sure if you need this but just in case LightStream changes the context on you
_.bindAll(this, 'syncLightstreamResponse');
},
subscribeToLightStream: function () {
MyApp.Lightstreamer.Do('subscribeUpdate'), {
adapterName: 'AdapterName',
parameterValue: this.id,
otherStuff: 'otherstuff',
onUpdate: this.syncLightstreamResponse
},
syncLightstreamResponse: function(resp, /* I'm guessing here; update param list as needed */) {
collection.set(resp, options);
collection.trigger('sync', collection, resp, options);
}
});
MyApp.module("MyApp"...) {
MyApp.Router = Marionette.AppRouter.extend(
{
appRoutes: { 'controller/view/:id', 'subscribe' }
}
);
var Api = {
subscribe: function(id) {
var messages = new MyApp.MyMessages({ id: id });
// No need for deferred here; just subscribe
messages.subscribeToLightStream();
}
};
QaApp.addInitializer(function () {
var router = new QaApp.Router({
controller: Api
});
});
}
在我看来,你并不是真的在改变
获取
的行为,而是在创建一种替代机制来将集合与服务器同步;关键是我认为您不需要覆盖fetch
来实现您的目标。我已将您的函数重命名为subscribeToLightStream
,但如果您选择覆盖fetch
,这应该无关紧要。看起来不错,谢谢!现在,如果我能正确启动我的路由,我可以实际测试它(-:很高兴提供帮助。如果你发布路由问题,我会看一看,看看我是否也能在那里提供帮助。几分钟前终于让路由工作了,这几乎可以工作了,但在SyncLightStreamResponse方法中,我得到了“集合未定义”。检查“此”显示“这”是Lightstreamer适配器对象。从编写Lightstreamer更新代码的人那里得到了一些帮助,解决了一些非常奇怪的范围问题,现在一切都好了。谢谢,Chris!太棒了-没问题!