all files / src/ sputils.list.js

100% Statements 16/16
50% Branches 1/2
100% Functions 8/8
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                                                                                                                                                  
(function () {
  var listByNameGetItems = function (name, config) {
    var url = "/_api/Web/Lists/getByTitle('%NAME')/items/".replace('%NAME', name);
    return sputils.rest.get(url, config)
      .then(sputils.rest.unwrapResults);
  };
 
  var listByNamePostItems = function (name, data, config) {
    var url = "/_api/Web/Lists/getByTitle('%NAME')/items/".replace('%NAME', name);
    return sputils.rest.post(url, data, config)
      .then(sputils.rest.unwrapResults);
  };
 
  var listByNameGetItemById = function (name, itemId, config) {
    var url = "/_api/Web/Lists/getByTitle('%NAME')/items/getbyid(%ID)"
          .replace('%NAME', name)
          .replace('%ID', itemId);
    return sputils.rest.get(url, config)
      .then(sputils.rest.unwrapResults);
  };
 
  /**
   * exposes operations on a list qualified by name.
   * @function sputils.list.byName
   * @param {string} name a list name
   * @returns {object} an object exposing operations possible on the list.
   *
   * @example
   *
   * var list = sputils.list.byName('announcements');
   * list.getItems().then(...);
   */
  var byName = function (name) {
    return {
      /**
       * Returns the list items from the given list name.
       * @function sputils.list.byName(name).getItems
       * @memberof! sputils.list
       * @param {Optional<object>} config the http config
       * @returns {Promise<array>} the array containing the list items.
       *
       * @example
       *
       * sputils.list.byName('Announcements').getItems()
       *   .then(function (data) { console.log(data.d.results) });
       *
       */
      getItems: function (config) {
        return listByNameGetItems(name, config);
      },
 
      /**
       * Returns the list item with the specified id.
       * @function sputils.list.byName(name).getItemById
       * @memberof! sputils.list
       * @param {object} data the payload
       * @param {Optional<object>} config the http config
       * @returns {Promise<object>} List item
       *
       * @example
       *
       * sputils.list.byName('Announcements').getItemById(1)
       *   .then(function (data) { console.log(data) });
       */
      getItemById: function (id, config) {
        return listByNameGetItemById(name, id, config);
      },
 
      /**
       * Modifies list items in the given list.
       * @function sputils.list.byName(name).postItems
       * @memberof! sputils.list
       * @param {object} data the payload
       * @param {object} config the http config
       * @returns {Promise<object>}
       *
       * @example
       *
       * var data = {
       *   __metadata: { type: "SP.Data.AnnouncementsListItem"}
       *   Title: "listlessly POSTing",
       * };
       * sputils.list.byName("Announcements").postItems(data)
       *   .then(function (data) { console.log(data); });
       */
      postItems: function (data, config) {
        return listByNamePostItems(name, data, config);
      }
    };
  };
 
  /** @namespace */
  sputils.list = fjs.assign(sputils.list || {}, {
    byName: byName
  });
})();