Sha256: 8203f37f5b651bc8fd89ffc9d356d33037d1b86fd004d41da8da8cd6cb4ec9a3

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

'use strict';

function ApiController($scope, $http, $location) {
  $scope.goTo = function(path, fromRel) {
    $scope.fromRel = fromRel
    $location.path(path)
  }

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

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

  get('/api')

  // helpers

  function get(path) {
    $http.get(path).success(responseHandler)
  }

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

  function put() {
    $http.put($scope.collection.href, 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

    $location.path($scope.collection.href)
  }
}


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

Version data entries

1 entries across 1 versions & 1 rubygems

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