Forum Stats

  • 3,816,616 Users
  • 2,259,214 Discussions
  • 7,893,519 Comments

Discussions

ArrayDataProvider vs RESTDataProvider

Chad Boom
Chad Boom Member Posts: 135 Blue Ribbon

We are getting questions from our development team on whether they should use ArrayDataProvider or RESTDataProvider. When retreiving data from the backend and rendering into components such as oj-table's and oj-select-single components, both Data Providers seem to be valid approaches. Most of the examples in the Cookbook under oj-table use the ArrayDataProvider, but was curious on what you see as the biggest advantages/disadvantages of using one Data Provider over the other.

Best Answers

  • Duncan Mills-Oracle
    Duncan Mills-Oracle Member Posts: 4,077 Employee
    Answer ✓

    The key difference is when it comes to where the data is and where operations on it will take place. With a ArrayDataProvider the whole dataset is stored client side in an array and all operations such as filtering and sorting will take place on that array. So it's great for small datasets (and for demos for the same reason)

    With the REST data provider it does not encapsulate the whole dataset - it's more of a moving window on a potentially infinite set of data in your backend. With a large dataset you don't want to download the whole thing in order to search for a handful of records, it's much better to specifically only GET the results you need. Same with sorting and so on.

    You can of course mix and match as it makes sense based on the data and the operations on that data.

    Chad Boom
  • John 'JB' Brock-Oracle
    John 'JB' Brock-Oracle Posts: 2,776 Employee
    Answer ✓

    Just to throw one more wrinkle into the mix for you Chad, take a look at the MutableArrayDataProvider instead of ArrayDataProvider. This is a more modern version of the DataProvider that allows you to work with keys and has no dependency on Knockout. Updating the content is as simple as updating the .data() method.

    Chad Boom

Answers

  • Duncan Mills-Oracle
    Duncan Mills-Oracle Member Posts: 4,077 Employee
    Answer ✓

    The key difference is when it comes to where the data is and where operations on it will take place. With a ArrayDataProvider the whole dataset is stored client side in an array and all operations such as filtering and sorting will take place on that array. So it's great for small datasets (and for demos for the same reason)

    With the REST data provider it does not encapsulate the whole dataset - it's more of a moving window on a potentially infinite set of data in your backend. With a large dataset you don't want to download the whole thing in order to search for a handful of records, it's much better to specifically only GET the results you need. Same with sorting and so on.

    You can of course mix and match as it makes sense based on the data and the operations on that data.

    Chad Boom
  • John 'JB' Brock-Oracle
    John 'JB' Brock-Oracle Posts: 2,776 Employee
    Answer ✓

    Just to throw one more wrinkle into the mix for you Chad, take a look at the MutableArrayDataProvider instead of ArrayDataProvider. This is a more modern version of the DataProvider that allows you to work with keys and has no dependency on Knockout. Updating the content is as simple as updating the .data() method.

    Chad Boom
  • Chad Boom
    Chad Boom Member Posts: 135 Blue Ribbon

    Thank you both, that helps a lot!

  • Chad Boom
    Chad Boom Member Posts: 135 Blue Ribbon

    One last question regarding the MutableArrayDataProvider. If you don't have a need to perform any CRUD operations (i.e. select single lists or read only tables), would you recommend still using the ArrayDataProvider or should we start migrating all of these to MutableArrayDataProvider's? We are currently working on upgrading all of our JET applications to version 12 and looking for advice on which is the most sustainable approach going forward.

  • MutableArrayDataProvider is a drop-in replacement for ArrayDataProvider. I would move to it if you are already doing refactoring of your code anyway.

  • Chad Boom
    Chad Boom Member Posts: 135 Blue Ribbon

    Hi JB, we tried this with a quick search and replace from the ArrayDataProvider to the MutableArrayDataProvider and found one significant difference in functionality. In order for the UI to update appropriately, we are having to explicitly call set the .data method of the data provider. Previously, we would just populate an observableArray and the UI would update automatically. Using the new MutableArrayDataProvider would require us to manage updating the data provider each time we update the data being fed into it. Ideally, it would nice if we could populate something into the array mapped to the DP and it would update without having to do this ourselves.



  • John 'JB' Brock-Oracle
    John 'JB' Brock-Oracle Posts: 2,776 Employee
    edited Apr 13, 2022 6:56PM

    Sorry Chad, yes you are correct.

    You can make this automated by using a Knockout pureComputed like this

        this.browserDPMutable = new MutableArrayDataProvider(browsers(), {
          keyAttributes: "value",
        });
    
        this.dataProvider = ko.pureComputed(() =>{
          let newData = browsers;
          this.browserDPMutable.data = newData();
          return this.browserDPMutable;
        })
    

    where "browsers" is an observableArray that gets updated in some way.

    there is absolutely nothing wrong with staying on ArrayDataProvider. The MutableArrayDataProvider is just a way to move past Knockout in the future.