更新骨干型号/浏览次数轮询请求 [英] Polling request for updating Backbone Models/Views

查看:138
本文介绍了更新骨干型号/浏览次数轮询请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个方法来更新网络应用与实施骨干

用例将是以下几点:结果
我有几个视图,每个视图,或者与此相关的观点也许模型/收藏,
需要做出不同的查询请求,在不同的时间服务器进行一些发现变化。

我想知道什么是最普遍的方式:

1)贯彻落实传统的轮询请求结果
2)实施长轮询请求结果
3)实施 HTML5网页插座


P.S:结果
1)服务器用PHP编写的。结果
2)现在我正在寻找一个解决方案,而无需使用HTML5 WebSockets的,因为可能与PHP不是那么简单了。


下面是一个使用传统的轮询请求

(1)我的简单code

(1)

  //为MyModel
VAR为MyModel = Backbone.View.extend({
    urlRoot:backendUrl
});// 我的看法
VAR MyView的= Backbone.View.extend({    初始化:功能(){
        this.model =新为MyModel();
        this.model.fetch();
        this.model.on('变',this.render);
        的setTimeout(函数(){
            this.model.fetch();
        },1000 * 60 * 2); //为了更新视图中的每个2分钟
    }
});



解决方案

实施IT模型中的投票处理,检查这个例子:

  //为MyModel
VAR为MyModel = Backbone.Model.extend({
  urlRoot:backendUrl',  //添加到您的模型:
  longPolling:假的,
  intervalMinutes:2,
  初始化:功能(){
    _.bindAll(本);
  },
  startLongPolling:功能(intervalMinutes){
    this.longPolling = TRUE;
    如果(intervalMinutes){
      this.intervalMinutes = intervalMinutes;
    }
    this.executeLongPolling();
  },
  stopLongPolling:功能(){
    this.longPolling = FALSE;
  },
  executeLongPolling:功能(){
    this.fetch({成功:this.onFetch});
  },
  onFetch:功能(){
    如果(this.longPolling){
      的setTimeout(this.executeLongPolling,1000 * 60 * this.intervalMinutes); //为了更新视图中的每个N分钟
    }
  }
});// 我的看法
VAR MyView的= Backbone.View.extend({    初始化:功能(){
        this.model =新为MyModel();
        this.model.startLongPolling();
        this.model.on('变',this.render);
    }
});

I need to find a way to update a web App implemented with backbone.

The use case will be the following:
I have several Views, and each View, or maybe model/collection related to this view, needs to make different polling request to the server at different time for discovering some change.

I am wondering what is the most general way to:

1) implement the Traditional Polling Request
2) implement the Long Polling Request
3) implement the HTML5 web socket


P.S.:
1) The server is written in PHP.
2) For now I am looking for a solution without using HTML5 WebSockets because maybe with PHP is not so simple.


Here's my simple code (1) using Traditional Polling Request.

(1)

// MyModel
var MyModel = Backbone.View.extend({
    urlRoot: 'backendUrl'
});

// MyView
var MyView = Backbone.View.extend({

    initialize: function () {
        this.model = new MyModel();
        this.model.fetch();
        this.model.on('change', this.render);
        setTimeout(function () {
            this.model.fetch();
        }, 1000 * 60 * 2); // in order to update the view each two minutes
    }
});


解决方案

Implement it in your Model the polling handler, check this example:

// MyModel
var MyModel = Backbone.Model.extend({
  urlRoot: 'backendUrl',

  //Add this to your model:
  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
    }
  }
});

// MyView
var MyView = Backbone.View.extend({

    initialize: function () {
        this.model = new MyModel();
        this.model.startLongPolling();
        this.model.on('change', this.render);
    }
});

这篇关于更新骨干型号/浏览次数轮询请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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