Caching collections in backbone.js?

前端 未结 6 935
暖寄归人
暖寄归人 2021-02-03 12:50

What would be the best way to ensure that my collection stays cached and thereby only gets fetched once?

Should I implement some sort of cache layer? Should I share the

6条回答
  •  无人共我
    2021-02-03 13:30

    A simple solution would be to make a global variable for your collection so that all your javascript code will use the same variable.

    Just fetch the collection when the page loads

    $(function() {
      myCollection = theCollection();
      myCollection.fetch();
    });
    

    Since var is not used to declared myCollection, then it becomes a global variable.

    Of course this is a simple implementation. There are more robust implementations, but depending on your needs, they might be overkill.

提交回复
热议问题