Skip to content

Instantly share code, notes, and snippets.

@oojacoboo
Created May 28, 2013 03:02
Show Gist options
  • Select an option

  • Save oojacoboo/5660273 to your computer and use it in GitHub Desktop.

Select an option

Save oojacoboo/5660273 to your computer and use it in GitHub Desktop.
this.parseUnits = function() {
if(this.jsonResultsReady === false) return;
if(this.currentResult && this.nResults)
if(this.currentResult >= this.nResults) return;
//for pager:
var showStart = this.currentResult + 1,
showEnd = this.currentResult + itemsPerResponse;
var counter = 0,
unitList = "";
//query our json
var filterUnits = function(units) {
var json = _.clone(units),
searchPattern = null,
keyValueRegex = /(\w*:\w+)/g,
splitSearch = _.without(that.searchQuery.split(keyValueRegex), "");
function match(s) {
s = String(s); //must be string to use match
return (s) ? s.match(searchPattern) : false;
}
function containsMatch(object, filter) {
searchPattern = new RegExp('(' + RegExp.escape(filter) + ')', "ig");
return _.chain(object)
.crush()
.reject(_.isUndefined)
.any(match).value();
}
function hasKeyValueMatch(key, value) {
return _.filter(json, function(object) {
return (_.has(object, key) && (eval("object." + key) == value));
});
}
function filterJson(filterStr) {
return _.filter(json, function(object) {
return (containsMatch(object, filterStr) ||
_.contains(filterStr, object.tags //check units tag array
|| (filterStr == "Pending Tenants" && object.pendingTenantsCount > 0)
|| (filterStr == "Lacking Management Fee" && object.lackingManagementFee == 1)
|| filterStr.length === 0)
);
});
}
//loop over search items ["string", "key:value"]
_.each(splitSearch, function(keyValueOrSearch) {
keyValueOrSearch = keyValueOrSearch.trim();
if(keyValueOrSearch.match(keyValueRegex)) {
var keyValue = keyValueOrSearch.split(/:/);
json = hasKeyValueMatch(keyValue[0], keyValue[1]);
} else {
json = filterJson(keyValueOrSearch);
}
});
//custom filtering
json = _.filter(json, function(object) {
if(that.filterBy == "filter-available" && object.available !== 1)
return false;
if(that.filterBy == "filter-past-due" && object.pastDue !== 1)
return false;
if(that.filterBy == "filter-renewals" && object.renewals !== 1)
return false;
if(that.minBeds !== null && that.minBeds > object.bedrooms && object.bedrooms > 0)
return false;
if(that.minBaths !== null && that.minBaths > object.bathrooms && object.bathrooms > 0)
return false;
if(that.minSqFt !== null && that.minSqFt > object.sqft && object.sqft.length > 0)
return false;
if(that.priceMin !== null && that.priceMin > object.desiredRent && object.desiredRent > 0)
return false;
if(that.priceMax !== null && that.priceMax < object.desiredRent && object.desiredRent > 0)
return false;
//if nothing returned false above, it's a keeper
return true;
});
return json;
};
_.each(filterUnits(this.jsonResults.units), function(unit) {
//for pager:
counter++;
if(showStart > counter || showEnd < counter)
return;
that.currentResult++;
unitList += that.addUnitTenants(unit, false);
});
this.containerEl.append(unitList);
//reset currentResult if nothing regex
if(counter === 0)
this.currentResult = 0;
else if(counter === 1) {
$("#units-list > li").attr("id","selected"); // if only one result, select it
setupSelectChangeEvents($("#units-list > li")); //you have to add evt handler here since they will not click it
}
//for pager:
this.nResults = counter;
this.setupPageNav();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment