Skip to content

Instantly share code, notes, and snippets.

@justintoth
Created February 19, 2014 19:14
Show Gist options
  • Save justintoth/9099384 to your computer and use it in GitHub Desktop.
Save justintoth/9099384 to your computer and use it in GitHub Desktop.
function mapJobListViewModel(details) {
var self = this;
this.entity;
this.views;
this.jobFormViewModel;
this.init = function () {
$('#job-date-range').change(function () {
self.get();
});
};
this.get = function (entity) {
if (entity) {
if (!self.entity) {
self.entity = ko.observable(entity);
}
else {
self.entity(entity);
}
}
if (!self.jobFormViewModel) {
self.jobFormViewModel = new mapJobFormViewModel(self);
}
self.jobFormViewModel.entity = self.entity();
utils.block($('#job-list'), 'Loading results');
var dateRange = utils.getDateRange($('#job-date-range').val());
api.jobs.readAll({
data: {
id: self.entity().id,
startDate: utils.formatIsoDateTime(dateRange.startDate),
endDate: utils.formatIsoDateTime(dateRange.endDate)
},
success: function (jobs) {
jobs = _.map(jobs, function (job) {
job.prettyExpectedArrivalDate = utils.formatDateTime(job.expectedArrivalDate);
job.fullAddress = utils.formatAddress(job);
return job;
});
if (!self.views) {
self.views = ko.observableArray(jobs);
ko.applyBindings(self, $('#jobs-tab')[0]);
}
else {
self.views(jobs);
}
$('#job-list').unblock();
},
error: function (result) {
$('#job-list').unblock();
}
});
};
this.edit = function (job) {
self.jobFormViewModel.fill(job);
self.jobFormViewModel.show();
};
this.init();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment