覆盖在取模型Marionette.js(并响应推送更新) [英] Overriding fetch in Marionette.js model (and responding to push updates)

查看:111
本文介绍了覆盖在取模型Marionette.js(并响应推送更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题的两个部分。我们有一个Marionette.js的应用,其中,初始加载,我们需要订阅到Lightstreamer实例,并保持该实例/连接打开(什么并不重要Lightstreamer的不同之处在于它发送更新到网页)。随后的模型更新一般将不会被用户操作通过从Lightstreamer来更新触发。我茫然的东西就如何建筑师这个。

首先,它似乎是基于取()动作,使Web请求到模型的url属性并同步检索数据(大部分样本)上的用户操作的默认模式。对我们来说,获取本质上是订阅行动和初始数据(消息的集合)将被异步返回。我所看到的建议,使用jQuery推迟,但我不知道在哪里做的()函数去(在模型中?在模块中?)。

其次,好像我们会用某种方式wreqr为推送更新注册,但我也不能肯定是什么样子。

下面是一些伪code:

  MyApp.Lightstreamer = codeToGetTheLightstreamerWiredUp();MyApp.MyCollection = Backbone.Collection.extend({
  型号:MyApp.Message,
  初始化:功能(模型,期权){
    this.id = options.id;
  },
  取:功能(){
    MyApp.Lightstreamer.Do('subscribeUpdate'),{
      adapterName:AdapterName',
      的parameterValue:this.id,
      otherStuff:otherstuff',
      的onUpdate:callbackFunction参数
  }
});MyApp.module(MyApp的...){
  MyApp.Router = Marionette.AppRouter.extend(
    {
       appRoutes:{控制器/视图/:身份证','认购'}
    }
  );  VAR阿比= {
     订阅:函数(ID){
       VAR消息=新MyApp.MyMessages({ID:ID});
       变种推迟= $ .Deferred();
       messages.fetch({
         成功:deferred.resolve
       }
     }
  };  QaApp.addInitializer(函数(){
      VAR路由器=新QaApp.Router(
      {
          控制器:阿比
      });
  });
}


  1. 问题1:在什么地方callbackFunction参数和/或如何做
    在这种情况下,延迟工作

  2. 问题2:我怎么更新这个模型
    当推事件发生时有新的数据(例如,一个新的消息,
    添加到集合中)?

谢谢!


解决方案

骨干网的本地的实施可以提供一个模板,你跟着。在正常读取,骨干调用同步,传递以下成功的回调:

  options.success =功能(RESP){
    VaR方法= options.reset? '重置':'设置';
    集合【制法】(RESP,期权);
    如果(成功)的成功(收集,RESP,期权);
    collection.trigger('同步',收集,RESP,期权);
  };

基本上,当主干从服务器获取一个响应,它使用这个填充集合。既然你直接从服务器获取定期更新,你会想要做的事有很多像这样使用LightStreamer的的onUpdate 参数。无论是骨干调用设置重置重置将清除收集和听起来不可取的,所以你可以删除条件并调用设置。触发同步是可选的,但我会推荐它与木偶最大的兼容性。我不相信你需要担心的延期,除非有我失去了一些其他的要求。

下面是我的建议:

  MyApp.Lightstreamer = codeToGetTheLightstreamerWiredUp();MyApp.MyCollection = Backbone.Collection.extend({
  型号:MyApp.Message,
  初始化:功能(模型,期权){
    this.id = options.id;
    //不知道,如果你需要这个,但为了以防万一是LightStream改变你的背景
    _.bindAll(这一点,syncLightstreamResponse');
  },
  subscribeToLightStream:功能(){
    MyApp.Lightstreamer.Do('subscribeUpdate'),{
      adapterName:AdapterName',
      的parameterValue:this.id,
      otherStuff:otherstuff',
      的onUpdate:this.syncLightstreamResponse
  },
  syncLightstreamResponse:功能(RESP,/ *我在这里猜测;更新参数列表需要* /){
    collection.set(RESP,期权);
    collection.trigger('同步',收集,RESP,期权);
  }
});MyApp.module(MyApp的...){
  MyApp.Router = Marionette.AppRouter.extend(
    {
       appRoutes:{控制器/视图/:身份证','认购'}
    }
  );  VAR阿比= {
     订阅:函数(ID){
       VAR消息=新MyApp.MyMessages({ID:ID});
       //不需要在这里延迟;刚刚订阅
       messages.subscribeToLightStream();
     }
  };  QaApp.addInitializer(函数(){
      VAR路由器=新QaApp.Router({
          控制器:阿比
      });
  });
}

这听起来并不像你对我真的改变了行为与其说创造synching与服务器集合的替代机制;问题的关键是,我不认为你需要重写来实现自己的目标。我已经重新命名功能 subscribeToLightStream ,但如果选择覆盖它不应该的问题代替。

This is a two part question. We have a Marionette.js application wherein, on initial load, we need to "subscribe" to a Lightstreamer instance and keep that instance/connection open (not important what Lightstreamer is except that it sends updates down to the page). Subsequent model updates will generally be triggered not by user actions by by updates coming from Lightstreamer. I'm at something of a loss as to how to architect this.

Firstly, it seems like the default model fetch() action makes a web request to the model's url property and retrieves data synchronously (in most samples) based on a user action. For us, the fetch is essentially the "subscribe" action and the initial data (a collection of messages) will be returned asynchronously. I have seen suggestions to use jquery deferred, but I'm not sure where the done() function goes (in the model? in the module?).

Secondly, it seems like we'd use wreqr somehow to register for the push updates, but I'm also not sure what that would look like.

Here's some psuedo code:

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
      });
  });
}

  1. Question 1: where does the callbackFunction and/or how does deferred work in this case
  2. Question 2: how do I update this model when a push event happens that has new data (e.g., a new message to add to the collection)?

Thanks!

解决方案

Backbone's native fetch implementation can provide a template for you to follow. During a normal fetch, Backbone calls sync, passing in the following success callback:

  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);
  };

Basically, when Backbone gets a response from the server, it uses this to populate the collection. Since you're getting periodic updates directly from the server, you'll want to do something a lot like this using LightStreamer's onUpdate parameter. Backbone either calls set or reset. reset will clear the collection and that sounds undesirable so you can remove the conditional and just call set. Triggering sync is optional but I'd recommend it for maximum compatibility with Marionette. I don't believe you need to worry about the deferred, unless there's some other requirement I'm missing.

Here's what I suggest:

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
      });
  });
}

It doesn't sound to me like you're really changing the fetch behavior so much as creating an alternative mechanism for synching the collection with the server; the point being that I don't think you need to override fetch to accomplish your goal. I've renamed your function to subscribeToLightStream, but it shouldn't matter if choose to override fetch instead.

这篇关于覆盖在取模型Marionette.js(并响应推送更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆