Data

The bbData service provides methods to load and save data through web service endpoints.

bbData functions

  • load(loadObj) — Takes an object with data, resources, and text properties and returns a promise that contains the result of an HTTP GET request. An httpResults property contains the results from the underlying calls to Angular's $http service. The object includes metadata about the response such as the HTTP status code.
    • data — A URL or an object with multiple URLs to be requested. result.data contains the promise results. For example: bbData.load({data: '/foo/data'}) or bbData.load({data: {a: '/foo/data1', b: '/foo/data2'}}). Requests to the URLs are made with credentials.
    • resources — A URL or an object with multiple URLs to be requested. result.resources contains the promise results. Requests to the URLs are made without credentials.
    • text — A URL or an object with multiple URLs to be requested. result.text contains the promise results. Requests to the URLs are made without credentials, and the result is returned as a string instead of an object.
    • loadManager — An object with name and scope properties that creates a wait while it and its child load managers retrieve data.
  • query(url, queryParams) — Creates a URL with a query string based on the queryParam's properties. For example: bbData.query('/foo/search', {x: 'y', z: 123}); returns /foo/search?x=y&z=123.
  • post(url, data) — Within bbData.load, creates an HTTP POST request from a URL and data object. For example: bbData.load({data: bbData.post('/foo/post', postData)});.
  • save(saveObj) — A function that issues an HTTP POST request to store data on the remote server. It takes an argument with the url, data, and type properties.
    • url — The URL to send the request to.
    • data — The object to POST to the URL.
    • type — The HTTP verb to use with the request. (Default: POST)
  • cancel(promise) — Takes a promise that bbData.load or bbData.save returns and cancels the underlying HTTP request. The promise is then rejected.

Demo

Returned Foo data: {{dataCtrl.result.data.foo}}
Returned resource data: {{dataCtrl.result.resources.name}}
Returned resource dtatus code: {{dataCtrl.result.httpResults.resources.status}}

Markup

<div ng-controller="DataTestController as dataCtrl">
  <button type="button" class="btn btn-primary" ng-click="dataCtrl.getFoo()">Get Foo</button>
  <div>Returned Foo data: <strong>{{dataCtrl.result.data.foo}}</strong></div>
  <div>Returned resource data: <strong>{{dataCtrl.result.resources.name}}</strong></div>
  <div>Returned resource dtatus code: <strong>{{dataCtrl.result.httpResults.resources.status}}</strong></div>
</div>

JavaScript

/* global angular */
(function () {
    'use strict';

    function DataTestController(bbData) {
        var self = this;

        self.getFoo = function () {
            bbData.load(
                {
                    data: '/sampledata/foo.json',
                    resources: '/sampledata/resources.json'
                }
            ).then(function (result) {
                self.result = result;
            });

        };
    }

    DataTestController.$inject = ['bbData'];

    angular.module('stache')
        .controller('DataTestController', DataTestController);
}());