Question

What is a good pattern for how to handle a subset of objects.

Say I've got a list of Lockers, inside each Locker is a set of Items. The UI is something akin to this - we're going to hope that the ASCII art makes in through.

Locker#1    |    Item #1 in Locker #2
Locker#2 ** |    Item #2 in Locker #2
Locker#3    |    Item #3 in Locker #2
            |    Item #4 in Locker #2
            |    Item #5 in Locker #2
            |    Item #6 in Locker #2
            |    Item #7 in Locker #2
            |    Item #8 in Locker #2
            |

Locker #2 is selected (active). What I've been doing is setting a callback when locker #2 is activated and then doing a ItemCollection.fetch({ data: { lid: LOCKER_ID }}) to fetch the subset of items that are ready for display. Which doesn't feel very backbone like.

What' the better approach -

  • Loading a subset of the models - if so is there a good example of how to do it?
  • Loading everything then doing Backbone filtering on the Collection?
  • Maybe an example of pagination that might be appropriate ?
Was it helpful?

Solution

I think everything depends on the amount of your data.

But I don't see any problem fetching the items Collection every time the filter changes. I really feel it is as Backbone way :). And this solution works for both cases: a lot of data, and few data.

As an optimization step you can cache the Collections and create new ones every time the filter changes. So you can reuse and already fetched one if the User click back to an already visited filter.

But still this optimization step is adding distance between your User and the fresh data. The User should reload the whole application to refresh his cached Collections. Or... add more code to reset Collections every X seconds... start to be a hard way.

So, in my opinion: re-fetch the Collection every time the User changes the filter sounds nice for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top