views:

33

answers:

3

Hey all,

In the below javascript, "this" refers to Car object and search_id refers to the input text field with an id of "search_input". So basically the user types in text in the field and a search occurs based on the input. Now I understand that the val() method is grabbing the user input string from the input field. However, I am not sure what the colon in the split() method is doing. I always thought the split() method just puts a comma delimiter when you pass in an empty string into it. And then it appears that the splitted variable holds an array of strings broken down from the input. However, why would we be passing in the first broken down string in the string array (splitted[0]) and the second string (splitted[1]) and then passing that into the variable string_to_scope? Basically it is in the process of building a search. And it's these three lines I'm not sure what's going on:

var splitted = jQuery(this.search_id).val().split(": ");
if (splitted[0] && splitted[1]){
   if (string_to_scope[splitted[0]]) ret[string_to_scope[splitted[0]]] = splitted[1];

Here's more context:

Car.prototype.filter_func=function(){
var ret={};
var string_to_scope = {
    'Year': 'year_num_eq',
    'Make': 'make_name_eq',
    'Description': 'description_eq',
    'Expiry': 'expires_on_eq'
};
var search_value = jQuery(this.search_id).val();

if(search_value != null && search_value.length > 0){
    var splitted = jQuery(this.search_id).val().split(": ");
    if (splitted[0] && splitted[1]){
        if (string_to_scope[splitted[0]]) ret[string_to_scope[splitted[0]]] = splitted[1];
    }
}
return ret;
 };

Thanks for any response.

A: 

The idea is to allow someone to enter a search that looks like "Make: Toyota". That is to say, to make a single search box accommodate searches across multiple fields (where you specify which field). A more typical approach would be to have a drop-down for search type that is separate from the search term; this is trying to combine them into one box.

The "split" method takes a string that contains a delimiter and turns it into an array that contains everything before, between, or after the delimiter. In this case it's turning

"Make: Toyota" into ["Make","Toyota"].

The first piece (the search type) becomes the key into the scope hash, and the second piece becomes the search term.

JacobM
I'm not fulling understanding how jQuery(this.search_id).val() returns 'Make: Toyota'. The only thing the user will do is type 'Toyota' in the input field if they want to search by make. And doesn't val() just get the string of user input (which is 'Toyota')?
JohnMerlino
It doesn't look like that's what this code has in mind; the lookup is using two values from the user to create a query like{"make_name_eq":"Toyota"}, using the string_to_scope object to build that query based on a search type entered by the user. The code you've posted is definitely expecting two values separated by a colon. As written, the user is expected to type "Make: Toyota" in the input box. (Note the part where it splits on the colon and then has an if that checks that the resulting array has at least two parts. If the user just types "Toyota" this code won't do anything at all.)
JacobM
A: 

Split does just like it sounds. Splits a string by the input and returns an array. So that is what is happening with the split. jQuery(this.search_id).val().split(": ");

Then they are checking if there are values set for both the first index and the second. if (splitted[0] && splitted[1])

If that is true then they are checking if the first value matches the name of a property in the string_to_scope object. You can access object properties by index similar to an array. if (string_to_scope[splitted[0]])

If there is a property by that name then they are returning a new object ret with a property of the first split value that equals the second split value. ret[string_to_scope[splitted[0]]] = splitted[1];

spinon
+1  A: 
// 'Year: 1998' -> ['Year', '1998'];
var splitted = jQuery(this.search_id).val().split(": ");

// if there were two parts 
// (the year is not missing)
if (splitted[0] && splitted[1]){

  // if the key exists in string_to_scope object
  // -> ok because string_to_scope['Year'] exists
  if (string_to_scope[splitted[0]]) 

   // ret[ string_to_scope['Year'] ]
   // -> ret['year_num_eq'] = '1998';
   ret[ string_to_scope[splitted[0] ] = splitted[1];
galambalazs
I'm not fulling understanding how jQuery(this.search_id).val() returns 'Year: 1998'. The only thing the user will do is type '1998' in the input field if they want to search by year. And doesn't val() just get the string of user input (which is '1998')?
JohnMerlino
exactly. I don't know why the code does things. I'm only telling you what it is doing...
galambalazs