Sha256: 1f600c1993f47ae358e447e7e5b3ec2c6cc27ffec602f5c51ce15fe123ac0e54

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

'use strict';

function ApiController($scope, $http, $location) {
  $scope.rootUrl = '/api'

  $scope.goTo = function(url, fromRel) {
    $scope.fromRel = fromRel
    get(url)
  }

  $scope.submit = function() {
    $scope.fromRel === 'edit-form' ? put() : post()
  }

  $scope.destroy = destroy

  $scope.$watch(function() {
    return $location.url();
  }, function(url, oldUrl) {
    if(url !== oldUrl)
      get(url)
  });

  get($scope.rootUrl)

  // helpers

  function get(url) {
    $scope.loading = true
    $http.get(url).success(function(data, status){
      responseHandler(data, status)
      $location.url(url)
    }).error(responseHandler)
  }

  function destroy(url) {
    $scope.loading = true
    $http.delete(url).
      success(responseHandler).
      error(responseHandler)
  }

  function post() {
    $scope.loading = true
    $http.post($scope.collection.href, formData()).
      success(responseHandler).
      error(responseHandler)
  }

  function put() {
    $scope.loading = true
    $http.put($location.url(), formData()).
      success(responseHandler).
      error(responseHandler)
  }

  function formData() {
    var data = {};
    $scope.collection.template.data.forEach(function(f){ data[f.name] = f.value })
    return data
  }

  function responseHandler(data, status) {
    $scope.collection = data.collection;
    $scope.raw = JSON.stringify(data, undefined, 2)
    $scope.status = status
    $scope.loading = false
  }
}

ApiController.$inject = ['$scope', '$http', '$location'];

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
collection-json-browser-0.0.9 app/assets/javascripts/collection_json_browser/angular/controllers/api_controller.js