怎么办轮询Backbone.js的? [英] how to do polling in backbone.js?

查看:125
本文介绍了怎么办轮询Backbone.js的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的工作使用Backbone.js的一个paly2.0框架应用程序(用java)。在我的应用程序需要定期从数据库表中的数据(用于显示即将发生的事件列表,如果交叉旧的事件应该从名单中删除的用例)。我正在逐渐要显示的数据,但问题是打数据库regularly.For,我试图使用Backbone.js的投票理念为每这些链接的轮询与Backbone.js的 HTTP集合:// kilon。组织/博客/ 2012/02 /骨干网轮询/ 。但他们没有投票的最新从数据库集合。请建议我如何实现或任何其他的选择吗?
谢谢进阶

Hi i am working on a paly2.0 framework application(with java) using backbone.js. In my application i need to get the table data from the database regularly ( for the use case of displaying the upcoming events list and if the crossed the old event should be removed from the list ).I am getting the data to display ,but the issue is to hit the Database regularly.For that i tried use backbone.js polling concept as per these links Polling a Collection with Backbone.js , http://kilon.org/blog/2012/02/backbone-poller/ .But they not polling the latest collection from db. Kindly suggest me how to achieve that or any other alternatives ? Thanks in adv.

推荐答案

有没有与骨干做一个本地方法。但是,你可以实现长轮询请求中加入一些方法到您的收藏:

There is not a native way to do it with Backbone. But you could implement long polling requests adding some methods to your collection:

// MyCollection
var MyCollection = Backbone.Collection.extend({
  urlRoot: 'backendUrl',

  longPolling : false,
  intervalMinutes : 2,
  initialize : function(){
    _.bindAll(this);
  },
  startLongPolling : function(intervalMinutes){
    this.longPolling = true;
    if( intervalMinutes ){
      this.intervalMinutes = intervalMinutes;
    }
    this.executeLongPolling();
  },
  stopLongPolling : function(){
    this.longPolling = false;
  },
  executeLongPolling : function(){
    this.fetch({success : this.onFetch});
  },
  onFetch : function () {
    if( this.longPolling ){
      setTimeout(this.executeLongPolling, 1000 * 60 * this.intervalMinutes); // in order to update the view each N minutes
    }
  }
});

var collection = new MyCollection();
collection.startLongPolling();
collection.on('reset', function(){ console.log('Collection fetched'); });

这篇关于怎么办轮询Backbone.js的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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