// Snippets
function remove_spaces(value) {
if (value == null || value == "") {
return "";
}
else {
return value.replace(/ /g, "");
}
}
// TODO Checar todas as funcoes de jquery
var app_url = "http://localhost:3000";
// Jquery
$(document).ready(function() {
var cache_estado, cache_cidade, cache_bairro, cache_segmento;
// TODO: Realizar mais testes
/* Atualiza as cidades de acordo com o estado */
$("#busca_estado").change(function() {
estado_selecionado = $("#busca_estado").attr("value");
if (estado_selecionado != cache_estado && remove_spaces(estado_selecionado) != "") {
$.getJSON(app_url + "/busca/listar_cidades/estado/" + estado_selecionado,
function(json) {
var cidades = '<option value="">Selecione uma cidade</option><option value="todas_cidades">-Todas-</option>';
$.each(json, function(i,valor){
cidades += '<option value="' + valor.anuncios.permalink + '">' + valor.anuncios.cidade_nome + '</option>';
});
$('#busca_cidade').html(cidades);
$('#busca_bairro').html('<option value="">Selecione uma cidade</option>');
$('#busca_segmento').html('<option value="">Selecione uma cidade</option>');
cache_estado = estado_selecionado;
cache_cidade = '';
cache_bairro = '';
});
}
});
// TODO: Realizar mais testes
/* Atualiza bairros de acordo com a cidade */
$('#busca_cidade').change(function() {
cidade_selecionada = $("#busca_cidade").attr("value");
if (cidade_selecionada != cache_cidade && remove_spaces(cidade_selecionada) != "") {
$.getJSON(app_url + "/busca/listar_bairros/estado/" + estado_selecionado + "/cidade/" + cidade_selecionada,
function(json) {
var bairros = '<option value="">Selecione um bairro</option><option value="todos_bairros">-Todas-</option>';
$.each(json, function(i,valor){
bairros += '<option value="' + valor.anuncios.bairro + '">' + valor.anuncios.bairro + '</option>';
});
$('#busca_bairro').html(bairros);
$('#busca_segmento').html('<option value="">Selecione um bairro</option>');
cache_cidade = cidade_selecionada;
});
}
});
$('#busca_bairro').change(function() {
bairro_selecionado = $("#busca_bairro").attr("value");
if (bairro_selecionado != cache_bairro && remove_spaces(bairro_selecionado) != "") {
$.getJSON(app_url + "/busca/listar_segmentos/estado/" + estado_selecionado + "/cidade/" + cidade_selecionada + "/bairro/" + bairro_selecionado,
function(json) {
var segmentos = '<option value="">Selecione um segmento</option><option value="todos_segmentos">-Todas-</option>';
$.each(json, function(i,valor){
segmentos += '<option value="' + valor.anuncios.permalink + '">' + valor.anuncios.nome + '</option>';
});
$('#busca_segmento').html(segmentos);
cache_bairro = bairro_selecionado;
});
}
});
$('#busca_botao').click(function() {
estado_url = $('#busca_estado').attr('value');
cidade_url = $('#busca_cidade').attr('value');
bairro_url = $('#busca_bairro').attr('value');
segmento_url = $('#busca_segmento').attr('value');
if (remove_spaces(estado_url) == "" || remove_spaces(cidade_url) == "" || remove_spaces(bairro_url) == "" || remove_spaces(segmento_url) == "") {
alert("Por favor, escolha uma cidade, um bairro e um segmento");
}
else {
window.location = app_url + "/busca/completa/" + estado_url + "/" + cidade_url + "/" + bairro_url + "/" + segmento_url + ".html"
}
})
});
Refactorings
No refactoring yet !
Tj Holowaychuk
March 23, 2009, March 23, 2009 17:53, permalink
Cant really help you since its not entirely english, but I can tell you just from looking at it there is certainly much which can be improved upon. For example:
# Put your misc function definitions inside of an anonymous function to limit its scope
(function(){
# I would name the param string, since thats what you really are accepting
# then throw an error, or return an empty string like below
function remove_spaces(string) {
if (!typeof string == 'string') return ''
return string.replace(/ /g, '')
}
print(remove_spaces('foo bar ')) # => foobar
})();
print(remove_spaces('foo bar ')) # => error .. remove_spaces is not defined
# OR
function remove_spaces(string) {
return typeof string == 'string' ?
string.replace(/ /g, ''):
''
}
diegorv.myopenid.com
March 23, 2009, March 23, 2009 20:51, permalink
English version, i need help to optimize that
/ Snippets
function remove_spaces(value) {
if (value == null || value == "") {
return "";
}
else {
return value.replace(/ /g, "");
}
}
// TODO Checar todas as funcoes de jquery
var app_url = "http://localhost:3000";
// Jquery
$(document).ready(function() {
var cache_state, cache_city, cache_zone, cache_segment;
$("#state_search").change(function() {
selected_state = $("#state_search").attr("value");
if (selected_state != cache_state && remove_spaces(selected_state) != "") {
$.getJSON(app_url + "/search/list_cities/state/" + selected_state,
function(json) {
var cities = '<option value="">Select the city</option><option value="all_cities">-All-</option>';
$.each(json, function(i,db){
cities += '<option value="' + db.data.permalink + '">' + db.data.city_name + '</option>';
});
$('#city_search').html(cities);
$('#zone_search').html('<option value="">Select the city</option>');
$('#segment_search').html('<option value="">Select the city</option>');
cache_state = selected_state;
cache_city = '';
cache_zone = '';
});
}
});
$('#city_search').change(function() {
selected_city = $("#city_search").attr("value");
if (selected_city != cache_city && remove_spaces(selected_city) != "") {
$.getJSON(app_url + "/search/list_zones/state/" + selected_state + "/city/" + selected_city,
function(json) {
var zones = '<option value="">Select the zone</option><option value="all_zones">-All-</option>';
$.each(json, function(i,db){
zones += '<option value="' + db.data.zone + '">' + db.data.zone + '</option>';
});
$('#zone_search').html(zones);
$('#segment_search').html('<option value="">Select the zone</option>');
cache_city = selected_city;
});
}
});
$('#zone_search').change(function() {
zone_selected = $("#zone_search").attr("value");
if (zone_selected != cache_zone && remove_spaces(zone_selected) != "") {
$.getJSON(app_url + "/search/list_segments/state/" + selected_state + "/city/" + selected_city + "/zone/" + zone_selected,
function(json) {
var segments = '<option value="">Select the segment</option><option value="all_segments">-All-</option>';
$.each(json, function(i,db){
segments += '<option value="' + db.data.permalink + '">' + db.data.name + '</option>';
});
$('#segment_search').html(segments);
cache_zone = zone_selected;
});
}
});
$('#busca_botao').click(function() {
state_url = $('#state_search').attr('value');
city_url = $('#city_search').attr('value');
zone_url = $('#zone_search').attr('value');
segment_url = $('#segment_search').attr('value');
if (remove_spaces(state_url) == "" || remove_spaces(city_url) == "" || remove_spaces(zone_url) == "" || remove_spaces(segment_url) == "") {
alert("Please select all datas");
}
else {
window.location = app_url + "/busca/completa/" + state_url + "/" + city_url + "/" + zone_url + "/" + segment_url + ".html"
}
})
});
transmisión en vivo de televisión
May 29, 2011, May 29, 2011 23:20, permalink
hola, Chicos, Muchas gracias por escribir esto, era increíblemente informativa y me dijo que una tonelada
Viajes sin visado a Rusia
May 30, 2011, May 30, 2011 10:32, permalink
Hola, Chicas! Aquest article va ser molt interessant, sobretot des que jo era la recerca d'idees sobre aquest tema el passat dijous.
lighting manufacturers in the UK
September 9, 2011, September 09, 2011 19:19, permalink
Estou aprendendo com você , no entanto , mas estou melhorando a mim mesmo. Eu realmente amo estudar cada parte que está escrito em seu blog.Preserve os contos vinda. Gostei !
I`m a beginner with jquery, it`s fine?!