Source: dataProxy/CircularLocal.js

  1. // Copyright 2011 Tart. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview tart.dataProxy.CircularLocal XHR data proxy.
  16. */
  17. goog.provide('tart.dataProxy.CircularLocal');
  18. goog.require('tart.dataProxy.Local');
  19. /**
  20. *
  21. * Base model to handle xhr requests
  22. *
  23. * @extends {tart.dataProxy.Local}
  24. * @constructor
  25. */
  26. tart.dataProxy.CircularLocal = function() {
  27. goog.base(this);
  28. };
  29. goog.inherits(tart.dataProxy.CircularLocal, tart.dataProxy.Local);
  30. /**
  31. * Fetch data from xhr and call a function with returned data
  32. * @param {Function=} callback function to call with returned data.
  33. */
  34. tart.dataProxy.CircularLocal.prototype.fetch = function(callback) {
  35. var fetchedData = this.getData();
  36. var pagerParam = this.params.get('paginationParams');
  37. if (!fetchedData || !pagerParam)
  38. callback.call(this);
  39. var offset = pagerParam.get('offset'),
  40. limit = pagerParam.get('limit'),
  41. length = fetchedData.length,
  42. tmp = [],
  43. pos;
  44. if (limit > length) limit = length;
  45. for (var i = offset, loopCount = offset + limit; i < loopCount; i++) {
  46. pos = (i % length + length) % length;
  47. tmp.push(fetchedData[pos]);
  48. }
  49. fetchedData = tmp;
  50. callback.call(this, fetchedData);
  51. };