Skip to content

Instantly share code, notes, and snippets.

@eads
Created February 18, 2014 04:39
Show Gist options
  • Save eads/9064724 to your computer and use it in GitHub Desktop.
Save eads/9064724 to your computer and use it in GitHub Desktop.
var EventAPICollection = Backbone.Collection.extend({
timezoneRegex: /\+0000$/,
initialize: function(models, options) {
options = options || {};
this.params = options.params || {};
this.preprocess = options.preprocess || false;
this.indexed = (options.indexed != false) ? true : false;
this.start = moment();
this.end = this.start.clone().add('months', 2);
this.filtered = new Backbone.Collection([]);
this.rawData = [];
this.activeSearch = {
'type': null,
'value': null
};
// Index events with LunrJS
if (this.indexed) {
this.index = lunr(function() {
this.field("title", { boost: 10 });
this.field("description");
this.field("venue_name");
this.field("neighborhood_name");
this.ref("id");
});
}
},
// Process API data
parse: function(data) {
var replace = this.timezoneRegex,
dates = [],
venues = [],
neighborhoods = [],
i = 0, j = 0;
this.trigger("parse_start");
var list = data.results;
if (this.preprocess) {
list = this.preprocess(list)
}
var results = _.flatten(_.map(list, _.bind(function(row) {
this.rawData.push(row);
if (this.indexed) {
row.id = i;
this.index.add(row);
i += 1;
}
// Gather up venues and neighborhoods for typeahead
venues.push(row.venue_name);
neighborhoods.push(row.neighborhood_name);
// Unspool events
var events = _.map(row.event_dates, function(date) {
var datestring = date.event_date.replace(replace, "");
var newrow =_.extend({}, row, {
id: j,
datestring: datestring,
start: new Date(datestring),
moment: moment(datestring),
allDay: false,
});
newrow.timestamp = newrow.moment.unix();
newrow.day = newrow.moment.format('YYYY-MM-DD'),
dates.push(newrow.day);
j += 1;
return newrow;
});
return events;
}, this)));
this.dates = _.uniq(dates.sort(), true);
this.venues = _.uniq(venues.sort(), true);
this.neighborhoods = _.uniq(neighborhoods.sort(), true);
this.trigger("parse_end");
return results;
},
// Search data, set collection.filtered to reflect the result
search: function(type, value) {
this.trigger("search_start", [value]);
this.activeSearch = {
'type': type,
'value': value
};
if (this.indexed && type == "search") {
var filtered = _.map(this.index.search(value), _.bind(function(match) {
return this.rawData[parseInt(match.ref)];
}, this));
this.filtered = new EventAPICollection({ results: filtered },
{ indexed: false, parse: true });
}
else if (type != "search" && type && value) {
var type = type + "_name",
q = {};
q[type] = value;
var filtered = _.where(this.rawData, q);
this.filtered = new EventAPICollection({ results: filtered },
{ indexed: false, parse: true });
}
else {
this.filtered = this;
}
this.filtered.sort();
this.trigger("search", [value]);
},
// Define behavior of collection.sort()
comparator: function(event) {
return event.get("timestamp");
},
// Set up AJAX call
sync: function(method, model, options) {
var params = $.extend(true, {
type: 'GET',
dataType: 'json',
cache: true,
data: $.extend({
'date_from': this.start.format('YYYY-MM-DD'),
'date_to': this.end.format('YYYY-MM-DD'),
'sort-by': 'date',
'order-by': 'asc',
'count': 300,
'fields': 'event_dates,neighborhood_name,venue_name,description,url,price,title,state,address,short_description,city_name,mmx_url',
}, this.params),
url: this.url,
}, options);
return $.ajax(params);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment