Skip to content

Commit

Permalink
Añadido filter slugify
Browse files Browse the repository at this point in the history
Quita tildes, ñ y deja en minusculas.
  • Loading branch information
Jesús Mur Fontanals committed Dec 9, 2014
1 parent 5e6e842 commit c45714d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 34 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

### Issues a revisar.

* Crear filter de Angular para la normalización de los nombres (Archivo services.js).

* Resolver un error en el filtro del array .some (Archivo services.js).

### Próximas feautures a desarrollar.
Expand Down
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var Employee = require('./models/employees.js');

app.post('/api/add',function(req, res) {
var employee = new Employee({
first: req.body.first,
first: req.body.first,
last: req.body.last,
street: req.body.street,
city: req.body.city,
Expand All @@ -54,10 +54,10 @@ app.get('/api/all', function(req, res) {
Employee.find(function(err, employees) {
if (err) {
res.send(err);
};
}
res.json(employees);
})
})
});
});

var port = 3000;
app.listen(port, function (err) {
Expand Down
23 changes: 23 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(function () {
var app = angular.module('users', ['ngRoute', 'user.controllers', 'user.directives', 'user.services', 'user.filters']);

app.config(['$routeProvider',function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/user-list.html',
controller: 'UserListController'
})
.when('/:type', {
templateUrl: 'views/user-list.html',
controller: 'UserListController'
})
.when('/user/:first', {
templateUrl: 'views/user.html',
controller: 'UserController',
controllerAs: 'UserCtrl'
})
.otherwise({
redirectTo: '/'
})
}]);
})();
16 changes: 16 additions & 0 deletions public/js/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function () {
angular.module('user.filters', [])
// Revisar este filtro
.filter('slugify', function(){
return function(input){
if (!input) return "";
var tittles = "ÃÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛãàáäâèéëêìíïîòóöôùúüûÑñÇç";
var original = "AAAAAEEEEIIIIOOOOUUUUaaaaaeeeeiiiioooouuuunncc";
for (var i = 0; i < tittles.length; i++) {
input = input.replace(tittles.charAt(i), original.charAt(i)).toLowerCase();
};
console.log(input);
return input;
};
});
})();
54 changes: 27 additions & 27 deletions public/js/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,55 @@
* Description
*/
angular.module('user.services', [])
.factory('userService', ['$http', '$q', function($http, $q){
.factory('userService', ['$http', '$q', '$filter',function($http, $q, $filter){
var slugify = $filter('slugify');
var deferred = $q.defer();

function all () {
function all() {
$http.get('/api/all')
.success(function(data) {
deferred.resolve(data);
})
deferred.resolve(data);
});
return deferred.promise;
}

function byName(first) {

// Falta normalizar los datos
var first = slugify(first);
var deferred = $q.defer();
all().then(function(data) {
var results = data.filter(function(user) {
return user.first === first;
})

return slugify(user.first) === first;
});
debugger;
if (results.length > 0) {
deferred.resolve(results[0]);
} else {
deferred.reject();
};
})
}
});
return deferred.promise;
}

function byType(){
var deferred = $q.defer();
// function byType(){
// var deferred = $q.defer();

// Resolver error .some
all().then(function(data){
debugger;
var results = data.filter(function(user){
return user.type.some(function(t){
return t === type;
})
});
deferred.resolve(results);
})
return deferred.promise;
}
// // Resolver error .some
// all().then(function(data){
// debugger;
// var results = data.filter(function(user){
// return user.type.some(function(t){
// return t === type;
// });
// });
// deferred.resolve(results);
// });
// return deferred.promise;
// }

return {
all: all,
byName: byName,
byType: byType
byName: byName
//byType: byType
};
}]);
})();
2 changes: 1 addition & 1 deletion public/views/user-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2>Lista de usuarios</h2>
<img ng-src="{{user.image}}" height="90" width="90" class="img" alt="{{user.first}} {{user.last}}">
</div>
<div class="media-body">
<h3><a ng-href="/#user/{{user.first}}">{{user.first}} {{user.last}}</a></h3>
<h3><a ng-href="/#user/{{user.first | slugify}}">{{user.first}} {{user.last}}</a></h3>
<p>{{user.city}} ({{user.state}})</p>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/filters.js"></script>
<script src="js/services.js"></script>
<script src="http://localhost:35729/livereload.js"></script>
<title>Ng-Users</title>
Expand Down

0 comments on commit c45714d

Please sign in to comment.