Backbone - Updating models in collection with data polled from another API

Fraser

I have a Backbone collection that I'm populating from an API endpoint. This return data such as:

[
    {
        "gameId": 1234,
        "gameName": "Fun Game 1"
    },
    {
        "gameId": 1235,
        "gameName": "Fun Game 2"
    },
    {
        "gameId": 1236,
        "gameName": "Fun Game 3"
    },
    etc,
    etc,
    etc
]

The collection is very simple and is initialised in the router so it's accessible to the entire app:

var GamesCollection = Backbone.Collection.extend({
        model: GameModel,
        url: '/path/to/games/list',

        parse:function(response){
            return response;
        }
    });

I have another endpoint that returns a collection of data related to the original data. This data looks like:

[
    {
        "gameId": 1234,
        "numberOfPlayers": "1,000"
    },
    {
        "gameId": 1235,
        "numberOfPlayers": "Fun Game 2"
    },
    {
        "gameId": 9999,
        "numberOfPlayers": "Some other game that's not in the original list"
    }
]

Note that the number of players response may or may not contain data for every game in the original response and may or may not contain data for games that do not exist in the original games response.

I need to be poll the number of players endpoint every X minutes and update the models in the GamesCollection with the data from the response so I can show this in the view.

What is the best way to handle this?

nikoshr

Query your numberOfPlayers endpoint and then set the fetched data on your collection. You can customize how set works with add and remove options.

For example,

var GamesCollection = Backbone.Collection.extend({
    model: GameModel,
    url: '/path/to/games/list',

    parse: function(response){
        return response;
    },

    pollNumberOfPlayers: function() {
        var self = this;
        return Backbone.ajax('/numberOfPlayers/endpoint').then(function(resp) {
            self.set(resp, {add: false, remove: false, merge: true});
        });
    },

    startPolling: function() {
        var self = this, 
            timer = 10000; //10 seconds

        self.pollNumberOfPlayers().then(function() {
            setTimeout(function() {
                self.startPolling();
            }, timer);
        });
    }
});

Note that I assumed your GameModel objects have gameId as their idAttribute.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Make a Backbone collection of models with data from a JSON file

来自分类Dev

Querying from models with HasManyThrough relations - strongloop api

来自分类Dev

Remove items from an HTML collection based on another HTML collection

来自分类Dev

如何使用嵌套模板从$ data上下文对象获取Knockback Collection中的基础Backbone集合

来自分类Dev

Backbone Collection 'add' event not firing

来自分类Dev

Why getters and setters in Backbone's models?

来自分类Dev

Backbone.Collection获取的竞争条件

来自分类Dev

Backbone.js呈现Collection View问题

来自分类Dev

Backbone.Collection获取的竞争条件

来自分类Dev

Backbone collection.reset 函数返回数组

来自分类Dev

Updating model data from form value ruby on rails

来自分类Dev

backbone restful api server return

来自分类Dev

为什么我不能制作Backbone.Views的Backbone.Collection?

来自分类Dev

如何重写Backbone Collection的删除方法,然后调用父级

来自分类Dev

为什么我的Backbone Collection重置事件未触发?

来自分类Dev

从Backbone.js中的Collection获取构造模型

来自分类Dev

Backbone Collection在Firefox中添加了奇怪的行为

来自分类Dev

一个Collection侦听BackBone中多个模型的更改

来自分类Dev

如何重写Backbone Collection的删除方法,然后调用父级

来自分类Dev

Backbone.js collection.remove文档说明

来自分类Dev

为什么Backbone的collection create()方法不填充ID?

来自分类Dev

具有任意参数的Backbone Collection.create()

来自分类Dev

使用Backbone.Collection获取但值不包括

来自分类Dev

Fill Users table with data using percentages from another table

来自分类Dev

Removing duplicates from pandas data frame with condition based on another column

来自分类Dev

Play Scala激活器编译命令显示值userid不是play.api.data.Form [models.Changepas剑]的成员

来自分类Dev

Updating GUI from different thread

来自分类Dev

Backbone.js是否带有Google Maps API Events?

来自分类Dev

Backbone.js:在keydown事件上运行并显示api结果

Related 相关文章

  1. 1

    Make a Backbone collection of models with data from a JSON file

  2. 2

    Querying from models with HasManyThrough relations - strongloop api

  3. 3

    Remove items from an HTML collection based on another HTML collection

  4. 4

    如何使用嵌套模板从$ data上下文对象获取Knockback Collection中的基础Backbone集合

  5. 5

    Backbone Collection 'add' event not firing

  6. 6

    Why getters and setters in Backbone's models?

  7. 7

    Backbone.Collection获取的竞争条件

  8. 8

    Backbone.js呈现Collection View问题

  9. 9

    Backbone.Collection获取的竞争条件

  10. 10

    Backbone collection.reset 函数返回数组

  11. 11

    Updating model data from form value ruby on rails

  12. 12

    backbone restful api server return

  13. 13

    为什么我不能制作Backbone.Views的Backbone.Collection?

  14. 14

    如何重写Backbone Collection的删除方法,然后调用父级

  15. 15

    为什么我的Backbone Collection重置事件未触发?

  16. 16

    从Backbone.js中的Collection获取构造模型

  17. 17

    Backbone Collection在Firefox中添加了奇怪的行为

  18. 18

    一个Collection侦听BackBone中多个模型的更改

  19. 19

    如何重写Backbone Collection的删除方法,然后调用父级

  20. 20

    Backbone.js collection.remove文档说明

  21. 21

    为什么Backbone的collection create()方法不填充ID?

  22. 22

    具有任意参数的Backbone Collection.create()

  23. 23

    使用Backbone.Collection获取但值不包括

  24. 24

    Fill Users table with data using percentages from another table

  25. 25

    Removing duplicates from pandas data frame with condition based on another column

  26. 26

    Play Scala激活器编译命令显示值userid不是play.api.data.Form [models.Changepas剑]的成员

  27. 27

    Updating GUI from different thread

  28. 28

    Backbone.js是否带有Google Maps API Events?

  29. 29

    Backbone.js:在keydown事件上运行并显示api结果

热门标签

归档