showcase: REST service cars: get remote values
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 26 Aug 2015 14:58:36 +0000 (16:58 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 26 Aug 2015 14:58:36 +0000 (16:58 +0200)
angularjs/showcase/src/showcase/app/rest/rest.constants.js
angularjs/showcase/src/showcase/app/rest/rest.controller.js
angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.html
angularjs/showcase/src/showcase/app/rest/rest.service.js

index a82540f..0a8591c 100644 (file)
@@ -1,7 +1,7 @@
 (function () {
   'use strict';
 
-  var prefix = 'http://localhost:8080/';
+  var prefix = '/';
 
   angular
     .module('app.rest')
index 66f6956..5e34c09 100644 (file)
@@ -10,6 +10,7 @@
    * @name app.rest.controller:Rest
    *
    * @requires $location
+   * @requires app.rest.cars
    *
    * <p>
    * <br>
    * Rest controller.
    */
   /* @ngInject */
-  function Rest($location) {
+  function Rest($location, cars) {
     var vm = this;
     vm.example = {
       text: 'try to send data',
       word: /^\s*\w*\s*$/,
       singleModel: 1
     };
+    vm.getCars = getCars;
+    vm.cars = undefined;
+
+    function getCars() {
+      vm.cars = cars.getAll();
+    }
   }
 
 })();
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js
new file mode 100644 (file)
index 0000000..62f1b3a
--- /dev/null
@@ -0,0 +1,40 @@
+describe('app.rest', function() {
+  'use strict';
+
+  var Rest;
+  var cars = {
+    getAll: function() {
+      return {};
+    }
+  };
+  var $q;
+
+  beforeEach(function() {
+    module('app.rest');
+
+    inject(function($controller, $location, _$q_) {
+      Rest = $controller('Rest', {
+        $location: $location,
+        cars: cars
+      });
+      $q = _$q_;
+    });
+  });
+
+  describe('Rest controller', function () {
+
+    it('should invoke GET all cars in service', function () {
+
+      spyOn(cars, 'getAll')
+        .and.callFake(function() {
+          var deferred = $q.defer();
+          return deferred.promise;
+        });
+
+      Rest.getCars();
+
+      expect(cars.getAll).toHaveBeenCalled();
+    });
+  });
+
+});
index c34fdb4..47d2136 100644 (file)
     <span>myForm.input.$error = {{myForm.input.$error}}</span><br/>
     <span>myForm.$valid = {{myForm.$valid}}</span><br/>
     <span>myForm.$error.required = {{!!myForm.$error.required}}</span><br/>
-    <button type="button" class="btn btn-primary" ng-model="vm.singleModel" ng-disabled="myForm.input.$invalid">
+    <button type="button" class="btn btn-primary" ng-disabled="myForm.input.$invalid">
       Single Toggle
     </button>
+    <ul >
+      <li ng-repeat="car in vm.cars"> {{ car.content }} </li>
+    </ul>
+    <button type="button" class="btn btn-primary" ng-disabled="myForm.input.$invalid"
+            ng-click="vm.getCars()">
+      Get cars
+    </button>
   </form>
-
-
-
-
 </div>
\ No newline at end of file
index 618eb82..ca5aa43 100644 (file)
@@ -41,7 +41,7 @@
         .catch(getAllFailed);
 
       function getAllCompleted(response) {
-        return response.data.results;
+        return response.data;
       }
 
       function getAllFailed(error) {