Skip to content

Commit

Permalink
Agrega paso 11 de la aplicación
Browse files Browse the repository at this point in the history
- Reemplaza [$http] por [$resource].

- Crea un servicio personalizado `Telefono` que representa el cliente
`$resource`.
  • Loading branch information
Israel Guzman committed Aug 30, 2014
1 parent 88e63f6 commit 9a4e852
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
2 changes: 2 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
<link rel="stylesheet" href="css/app.css">
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="js/ap.js"></script>
<script src="js/controladores.js"></script>
<script src="js/filtros.js"></script>
<script src="js/servicios.js"></script>
</head>
<body>

Expand Down
3 changes: 2 additions & 1 deletion app/js/ap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
var phonecatAp = angular.module('phonecatAp', [
'ngRoute',
'phonecatControladores',
'phonecatFiltros'
'phonecatFiltros',
'phonecatServicios'
]);

phonecatAp.config(['$routeProvider',
Expand Down
17 changes: 7 additions & 10 deletions app/js/controladores.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

var phonecatControladores = angular.module('phonecatControladores', []);

phonecatControladores.controller('ListaTelefonosCtrl', ['$scope', '$http',
function($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.telefonos = data;
});
phonecatControladores.controller('ListaTelefonosCtrl', ['$scope', 'Telefono',
function($scope, Telefono) {
$scope.telefonos = Telefono.query();

$scope.ordenProp = 'age';
}]);

phonecatControladores.controller('DetallesTelefonoCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$http.get('phones/' + $routeParams.idTelefono + '.json').success(function(data) {
$scope.telefono = data;
$scope.urlImagenPrincipal = data.images[0];
phonecatControladores.controller('DetallesTelefonoCtrl', ['$scope', '$routeParams', 'Telefono',
function($scope, $routeParams, Telefono) {
$scope.telefono = Telefono.get({idTelefono: $routeParams.idTelefono}, function(telefono) {
$scope.urlImagenPrincipal = telefono.images[0];
});

$scope.setImagen = function(urlImagen) {
Expand Down
10 changes: 10 additions & 0 deletions app/js/servicios.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@

/* Servicios */

var phonecatServicios = angular.module('phonecatServicios', ['ngResource']);

phonecatServicios.factory('Telefono', ['$resource',
function($resource){
return $resource('phones/:idTelefono.json', {}, {
query: {method:'GET', params:{idTelefono:'phones'}, isArray:true}
});
}]);


1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function(config){
files : [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/js/**/*.js',
'test/unit/**/*.js'
Expand Down
63 changes: 33 additions & 30 deletions test/unit/controladoresSpec.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
'use strict';

/* jasmine specs for controllers go here */
describe('controladores PhoneCat', function() {

describe('controllers', function() {
var scope, ctrl, $httpBackend;
beforeEach(function(){
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});

beforeEach(module('phonecatAp'));
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/phones.json').
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
beforeEach(module('phonecatServicios'));

describe('ListaTelefonosCtrl', function(){
var scope, ctrl, $httpBackend;

beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/phones.json').
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);

scope = $rootScope.$new();
ctrl = $controller('ListaTelefonosCtrl', {$scope: scope});
}));
scope = $rootScope.$new();
ctrl = $controller('ListaTelefonosCtrl', {$scope: scope});
}));


it('debe crear el modelo "telefonos" con 2 teléfonos traidos de xhr', function() {
expect(scope.telefonos).toBeUndefined();
$httpBackend.flush();
it('debe crear el modelo "telefonos" con 2 telefonos de xhr', function() {
expect(scope.telefonos).toEqualData([]);
$httpBackend.flush();

expect(scope.telefonos).toEqual([{name: 'Nexus S'},
{name: 'Motorola DROID'}]);
});
expect(scope.telefonos).toEqualData(
[{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
});


it('debe setear el valor por defecto del modelo ordenProp', function() {
expect(scope.ordenProp).toBe('age');
it('debe setear el valor por defecto del modelo orderProp', function() {
expect(scope.ordenProp).toBe('age');
});
});


describe('DetallesTelefonoCtrl', function(){
var scope, $httpBackend, ctrl,
datosTelefonoXyz = function() {
xyzPhoneData = function() {
return {
name: 'telefono xyz',
images: ['image/url1.png', 'image/url2.png']
Expand All @@ -41,21 +54,11 @@ describe('controllers', function() {

beforeEach(inject(function(_$httpBackend_, $rootScope, $routeParams, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/xyz.json').respond(datosTelefonoXyz());
$httpBackend.expectGET('phones/xyz.json').respond(xyzPhoneData());

$routeParams.idTelefono = 'xyz';
$routeParams.phoneId = 'xyz';
scope = $rootScope.$new();
ctrl = $controller('DetallesTelefonoCtrl', {$scope: scope});
}));


it('should fetch phone detail', function() {
expect(scope.telefono).toBeUndefined();
$httpBackend.flush();

expect(scope.telefono).toEqual(datosTelefonoXyz());
});
});


});

0 comments on commit 9a4e852

Please sign in to comment.