[#129,refactor][l]: ([s] in effort) rename Document to Record - fixes #129.

This commit is contained in:
Rufus Pollock
2012-05-31 21:04:31 +01:00
parent e7aa60c7d2
commit c1c1881660
19 changed files with 160 additions and 160 deletions

View File

@@ -29,14 +29,14 @@ this.recline.Backend.Base = function() {
// ### sync
//
// An implementation of Backbone.sync that will be used to override
// Backbone.sync on operations for Datasets and Documents which are using this backend.
// Backbone.sync on operations for Datasets and Records which are using this backend.
//
// For read-only implementations you will need only to implement read method
// for Dataset models (and even this can be a null operation). The read method
// should return relevant metadata for the Dataset. We do not require read support
// for Documents because they are loaded in bulk by the query method.
// for Records because they are loaded in bulk by the query method.
//
// For backends supporting write operations you must implement update and delete support for Document objects.
// For backends supporting write operations you must implement update and delete support for Record objects.
//
// All code paths should return an object conforming to the jquery promise API.
this.sync = function(method, model, options) {
@@ -44,8 +44,8 @@ this.recline.Backend.Base = function() {
// ### query
//
// Query the backend for documents returning them in bulk. This method will
// be used by the Dataset.query method to search the backend for documents,
// Query the backend for records returning them in bulk. This method will
// be used by the Dataset.query method to search the backend for records,
// retrieving the results in bulk.
//
// @param {recline.model.Dataset} model: Dataset model.
@@ -71,11 +71,11 @@ this.recline.Backend.Base = function() {
// <pre>
// {
// total: // (required) total number of results (can be null)
// hits: [ // (required) one entry for each result document
// hits: [ // (required) one entry for each result record
// {
// _score: // (optional) match score for document
// _type: // (optional) document type
// _source: // (required) document/row object
// _score: // (optional) match score for record
// _type: // (optional) record type
// _source: // (required) record/row object
// }
// ],
// facets: { // (optional)

View File

@@ -39,7 +39,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// ### get
//
// Get document corresponding to specified id
// Get record corresponding to specified id
//
// @return promise compatible deferred object.
this.get = function(id) {
@@ -52,7 +52,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// ### upsert
//
// create / update a document to ElasticSearch backend
// create / update a record to ElasticSearch backend
//
// @param {Object} doc an object to insert to the index.
// @return deferred supporting promise API
@@ -72,7 +72,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// ### delete
//
// Delete a document from the ElasticSearch backend.
// Delete a record from the ElasticSearch backend.
//
// @param {Object} id id of object to delete
// @return deferred supporting promise API
@@ -154,7 +154,7 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
// Backbone sync implementation for this backend.
//
// URL of ElasticSearch endpoint to use must be specified on the dataset
// (and on a Document via its dataset attribute) by the dataset having a
// (and on a Record via its dataset attribute) by the dataset having a
// url attribute.
this.sync = function(method, model, options) {
if (model.__type__ == 'Dataset') {
@@ -180,15 +180,15 @@ this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
dfd.reject(arguments);
});
return dfd.promise();
} else if (model.__type__ == 'Document') {
} else if (model.__type__ == 'Record') {
return es.get(model.dataset.id);
}
} else if (method === 'update') {
if (model.__type__ == 'Document') {
if (model.__type__ == 'Record') {
return es.upsert(model.toJSON());
}
} else if (method === 'delete') {
if (model.__type__ == 'Document') {
if (model.__type__ == 'Record') {
return es.delete(model.id);
}
}

View File

@@ -7,7 +7,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
//
// Convenience function to create a simple 'in-memory' dataset in one step.
//
// @param data: list of hashes for each document/row in the data ({key:
// @param data: list of hashes for each record/row in the data ({key:
// value, key: value})
// @param fields: (optional) list of field hashes (each hash defining a hash
// as per recline.Model.Field). If fields not specified they will be taken
@@ -76,7 +76,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
results = results.slice(start, start+numRows);
return {
total: total,
documents: results,
records: results,
facets: facets
};
};
@@ -118,7 +118,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
return results;
};
this.computeFacets = function(documents, queryObj) {
this.computeFacets = function(records, queryObj) {
var facetResults = {};
if (!queryObj.facets) {
return facetResults;
@@ -129,7 +129,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
facetResults[facetId].termsall = {};
});
// faceting
_.each(documents, function(doc) {
_.each(records, function(doc) {
_.each(queryObj.facets, function(query, facetId) {
var fieldId = query.terms.field;
var val = doc[fieldId];
@@ -172,13 +172,13 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
}
return dfd.promise();
} else if (method === 'update') {
if (model.__type__ == 'Document') {
if (model.__type__ == 'Record') {
model.dataset._dataCache.update(model.toJSON());
dfd.resolve(model);
}
return dfd.promise();
} else if (method === 'delete') {
if (model.__type__ == 'Document') {
if (model.__type__ == 'Record') {
model.dataset._dataCache.delete(model.toJSON());
dfd.resolve(model);
}
@@ -191,7 +191,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
this.query = function(model, queryObj) {
var dfd = $.Deferred();
var results = model._dataCache.query(queryObj);
var hits = _.map(results.documents, function(row) {
var hits = _.map(results.records, function(row) {
return { _source: row };
});
var out = {

View File

@@ -12,11 +12,11 @@ this.recline.Model = this.recline.Model || {};
// fields on this Dataset (this can be set explicitly, or, will be set by
// Dataset.fetch() or Dataset.query()
//
// @property {DocumentList} currentDocuments: a `DocumentList` containing the
// Documents we have currently loaded for viewing (updated by calling query
// @property {RecordList} currentRecords: a `RecordList` containing the
// Records we have currently loaded for viewing (updated by calling query
// method)
//
// @property {number} docCount: total number of documents in this dataset
// @property {number} docCount: total number of records in this dataset
//
// @property {Backend} backend: the Backend (instance) for this Dataset.
//
@@ -48,7 +48,7 @@ my.Dataset = Backbone.Model.extend({
this.backend = this._backendFromString(backend);
}
this.fields = new my.FieldList();
this.currentDocuments = new my.DocumentList();
this.currentRecords = new my.RecordList();
this.facets = new my.FacetList();
this.docCount = null;
this.queryState = new my.Query();
@@ -58,12 +58,12 @@ my.Dataset = Backbone.Model.extend({
// ### query
//
// AJAX method with promise API to get documents from the backend.
// AJAX method with promise API to get records from the backend.
//
// It will query based on current query state (given by this.queryState)
// updated by queryObj (if provided).
//
// Resulting DocumentList are used to reset this.currentDocuments and are
// Resulting RecordList are used to reset this.currentRecords and are
// also returned.
query: function(queryObj) {
var self = this;
@@ -73,12 +73,12 @@ my.Dataset = Backbone.Model.extend({
this.backend.query(this, actualQuery).done(function(queryResult) {
self.docCount = queryResult.total;
var docs = _.map(queryResult.hits, function(hit) {
var _doc = new my.Document(hit._source);
var _doc = new my.Record(hit._source);
_doc.backend = self.backend;
_doc.dataset = self;
return _doc;
});
self.currentDocuments.reset(docs);
self.currentRecords.reset(docs);
if (queryResult.facets) {
var facets = _.map(queryResult.facets, function(facetResult, facetId) {
facetResult.id = facetId;
@@ -87,7 +87,7 @@ my.Dataset = Backbone.Model.extend({
self.facets.reset(facets);
}
self.trigger('query:done');
dfd.resolve(self.currentDocuments);
dfd.resolve(self.currentRecords);
})
.fail(function(arguments) {
self.trigger('query:fail', arguments);
@@ -176,11 +176,11 @@ my.Dataset.restore = function(state) {
return dataset;
};
// ## <a id="document">A Document (aka Row)</a>
// ## <a id="record">A Record (aka Row)</a>
//
// A single entry or row in the dataset
my.Document = Backbone.Model.extend({
__type__: 'Document',
my.Record = Backbone.Model.extend({
__type__: 'Record',
initialize: function() {
_.bindAll(this, 'getFieldValue');
},
@@ -188,7 +188,7 @@ my.Document = Backbone.Model.extend({
// ### getFieldValue
//
// For the provided Field get the corresponding rendered computed data value
// for this document.
// for this record.
getFieldValue: function(field) {
var val = this.get(field.id);
if (field.deriver) {
@@ -211,17 +211,17 @@ my.Document = Backbone.Model.extend({
}
});
// ## A Backbone collection of Documents
my.DocumentList = Backbone.Collection.extend({
__type__: 'DocumentList',
model: my.Document
// ## A Backbone collection of Records
my.RecordList = Backbone.Collection.extend({
__type__: 'RecordList',
model: my.Record
});
// ## <a id="field">A Field (aka Column) on a Dataset</a>
//
// Following (Backbone) attributes as standard:
//
// * id: a unique identifer for this field- usually this should match the key in the documents hash
// * id: a unique identifer for this field- usually this should match the key in the records hash
// * label: (optional: defaults to id) the visible label used for this field
// * type: (optional: defaults to string) the type of the data in this field. Should be a string as per type names defined by ElasticSearch - see Types list on <http://www.elasticsearch.org/guide/reference/mapping/>
// * format: (optional) used to indicate how the data should be formatted. For example:
@@ -234,13 +234,13 @@ my.DocumentList = Backbone.Collection.extend({
//
// @property {Function} renderer: a function to render the data for this field.
// Signature: function(value, field, doc) where value is the value of this
// cell, field is corresponding field object and document is the document
// cell, field is corresponding field object and record is the record
// object. Note that implementing functions can ignore arguments (e.g.
// function(value) would be a valid formatter function).
//
// @property {Function} deriver: a function to derive/compute the value of data
// in this field as a function of this field's value (if any) and the current
// document, its signature and behaviour is the same as for renderer. Use of
// record, its signature and behaviour is the same as for renderer. Use of
// this function allows you to define an entirely new value for data in this
// field. This provides support for a) 'derived/computed' fields: i.e. fields
// whose data are functions of the data in other fields b) transforming the
@@ -461,7 +461,7 @@ my.Query = Backbone.Model.extend({
// "_type" : "terms",
// // total number of tokens in the facet
// "total": 5,
// // @property {number} number of documents which have no value for the field
// // @property {number} number of records which have no value for the field
// "missing" : 0,
// // number of facet values not included in the returned facets
// "other": 0,

View File

@@ -97,8 +97,8 @@ my.Graph = Backbone.View.extend({
this.model.bind('change', this.render);
this.model.fields.bind('reset', this.render);
this.model.fields.bind('add', this.render);
this.model.currentDocuments.bind('add', this.redraw);
this.model.currentDocuments.bind('reset', this.redraw);
this.model.currentRecords.bind('add', this.redraw);
this.model.currentRecords.bind('reset', this.redraw);
// because we cannot redraw when hidden we may need when becoming visible
this.bind('view:show', function() {
if (this.needToRedraw) {
@@ -181,7 +181,7 @@ my.Graph = Backbone.View.extend({
// Uncaught Invalid dimensions for plot, width = 0, height = 0
// * There is no data for the plot -- either same error or may have issues later with errors like 'non-existent node-value'
var areWeVisible = !jQuery.expr.filters.hidden(this.el[0]);
if ((!areWeVisible || this.model.currentDocuments.length === 0)) {
if ((!areWeVisible || this.model.currentRecords.length === 0)) {
this.needToRedraw = true;
return;
}
@@ -209,8 +209,8 @@ my.Graph = Backbone.View.extend({
// However, that is non-trivial to work out from a dataset (datasets may
// have no field type info). Thus at present we only do this for bars.
var tickFormatter = function (val) {
if (self.model.currentDocuments.models[val]) {
var out = self.model.currentDocuments.models[val].get(self.state.attributes.group);
if (self.model.currentRecords.models[val]) {
var out = self.model.currentRecords.models[val].get(self.state.attributes.group);
// if the value was in fact a number we want that not the
if (typeof(out) == 'number') {
return val;
@@ -266,7 +266,7 @@ my.Graph = Backbone.View.extend({
tickLength: 1,
tickFormatter: tickFormatter,
min: -0.5,
max: self.model.currentDocuments.length - 0.5
max: self.model.currentRecords.length - 0.5
}
}
};
@@ -304,8 +304,8 @@ my.Graph = Backbone.View.extend({
y = _tmp;
}
// convert back from 'index' value on x-axis (e.g. in cases where non-number values)
if (self.model.currentDocuments.models[x]) {
x = self.model.currentDocuments.models[x].get(self.state.attributes.group);
if (self.model.currentRecords.models[x]) {
x = self.model.currentRecords.models[x].get(self.state.attributes.group);
} else {
x = x.toFixed(2);
}
@@ -339,7 +339,7 @@ my.Graph = Backbone.View.extend({
var series = [];
_.each(this.state.attributes.series, function(field) {
var points = [];
_.each(self.model.currentDocuments.models, function(doc, index) {
_.each(self.model.currentRecords.models, function(doc, index) {
var xfield = self.model.fields.get(self.state.attributes.group);
var x = doc.getFieldValue(xfield);
// time series

View File

@@ -17,9 +17,9 @@ my.Grid = Backbone.View.extend({
var self = this;
this.el = $(this.el);
_.bindAll(this, 'render', 'onHorizontalScroll');
this.model.currentDocuments.bind('add', this.render);
this.model.currentDocuments.bind('reset', this.render);
this.model.currentDocuments.bind('remove', this.render);
this.model.currentRecords.bind('add', this.render);
this.model.currentRecords.bind('reset', this.render);
this.model.currentRecords.bind('remove', this.render);
this.tempState = {};
var state = _.extend({
hiddenFields: []
@@ -77,13 +77,13 @@ my.Grid = Backbone.View.extend({
showColumn: function() { self.showColumn(e); },
deleteRow: function() {
var self = this;
var doc = _.find(self.model.currentDocuments.models, function(doc) {
var doc = _.find(self.model.currentRecords.models, function(doc) {
// important this is == as the currentRow will be string (as comes
// from DOM) while id may be int
return doc.id == self.tempState.currentRow;
});
doc.destroy().then(function() {
self.model.currentDocuments.remove(doc);
self.model.currentRecords.remove(doc);
self.trigger('recline:flash', {message: "Row deleted successfully"});
}).fail(function(err) {
self.trigger('recline:flash', {message: "Errorz! " + err});
@@ -213,7 +213,7 @@ my.Grid = Backbone.View.extend({
});
var htmls = Mustache.render(this.template, this.toTemplateJSON());
this.el.html(htmls);
this.model.currentDocuments.forEach(function(doc) {
this.model.currentRecords.forEach(function(doc) {
var tr = $('<tr />');
self.el.find('tbody').append(tr);
var newView = new my.GridRow({
@@ -246,7 +246,7 @@ my.Grid = Backbone.View.extend({
}
});
// ## GridRow View for rendering an individual document.
// ## GridRow View for rendering an individual record.
//
// Since we want this to update in place it is up to creator to provider the element to attach to.
//
@@ -256,7 +256,7 @@ my.Grid = Backbone.View.extend({
//
// <pre>
// var row = new GridRow({
// model: dataset-document,
// model: dataset-record,
// el: dom-element,
// fields: mydatasets.fields // a FieldList object
// });

View File

@@ -7,7 +7,7 @@ this.recline.View = this.recline.View || {};
// ## Map view for a Dataset using Leaflet mapping library.
//
// This view allows to plot gereferenced documents on a map. The location
// This view allows to plot gereferenced records on a map. The location
// information can be provided either via a field with
// [GeoJSON](http://geojson.org) objects or two fields with latitude and
// longitude coordinates.
@@ -115,14 +115,14 @@ my.Map = Backbone.View.extend({
self.render()
});
// Listen to changes in the documents
this.model.currentDocuments.bind('add', function(doc){self.redraw('add',doc)});
this.model.currentDocuments.bind('change', function(doc){
// Listen to changes in the records
this.model.currentRecords.bind('add', function(doc){self.redraw('add',doc)});
this.model.currentRecords.bind('change', function(doc){
self.redraw('remove',doc);
self.redraw('add',doc);
});
this.model.currentDocuments.bind('remove', function(doc){self.redraw('remove',doc)});
this.model.currentDocuments.bind('reset', function(){self.redraw('reset')});
this.model.currentRecords.bind('remove', function(doc){self.redraw('remove',doc)});
this.model.currentRecords.bind('reset', function(){self.redraw('reset')});
this.bind('view:show',function(){
// If the div was hidden, Leaflet needs to recalculate some sizes
@@ -184,9 +184,9 @@ my.Map = Backbone.View.extend({
// Actions can be:
//
// * reset: Clear all features
// * add: Add one or n features (documents)
// * remove: Remove one or n features (documents)
// * refresh: Clear existing features and add all current documents
// * add: Add one or n features (records)
// * remove: Remove one or n features (records)
// * refresh: Clear existing features and add all current records
redraw: function(action, doc){
var self = this;
action = action || 'refresh';
@@ -201,7 +201,7 @@ my.Map = Backbone.View.extend({
if (this.geomReady && this.mapReady){
if (action == 'reset' || action == 'refresh'){
this.features.clearLayers();
this._add(this.model.currentDocuments.models);
this._add(this.model.currentRecords.models);
} else if (action == 'add' && doc){
this._add(doc);
} else if (action == 'remove' && doc){
@@ -266,11 +266,11 @@ my.Map = Backbone.View.extend({
// Private: Add one or n features to the map
//
// For each document passed, a GeoJSON geometry will be extracted and added
// For each record passed, a GeoJSON geometry will be extracted and added
// to the features layer. If an exception is thrown, the process will be
// stopped and an error notification shown.
//
// Each feature will have a popup associated with all the document fields.
// Each feature will have a popup associated with all the record fields.
//
_add: function(docs){
var self = this;
@@ -281,7 +281,7 @@ my.Map = Backbone.View.extend({
var wrongSoFar = 0;
_.every(docs,function(doc){
count += 1;
var feature = self._getGeometryFromDocument(doc);
var feature = self._getGeometryFromRecord(doc);
if (typeof feature === 'undefined' || feature === null){
// Empty field
return true;
@@ -338,9 +338,9 @@ my.Map = Backbone.View.extend({
},
// Private: Return a GeoJSON geomtry extracted from the document fields
// Private: Return a GeoJSON geomtry extracted from the record fields
//
_getGeometryFromDocument: function(doc){
_getGeometryFromRecord: function(doc){
if (this.geomReady){
if (this.state.get('geomField')){
var value = doc.get(this.state.get('geomField'));

View File

@@ -19,9 +19,9 @@ my.SlickGrid = Backbone.View.extend({
var self = this;
this.el = $(this.el);
_.bindAll(this, 'render');
this.model.currentDocuments.bind('add', this.render);
this.model.currentDocuments.bind('reset', this.render);
this.model.currentDocuments.bind('remove', this.render);
this.model.currentRecords.bind('add', this.render);
this.model.currentRecords.bind('reset', this.render);
this.model.currentRecords.bind('remove', this.render);
var state = _.extend({
hiddenColumns: [],
@@ -110,7 +110,7 @@ my.SlickGrid = Backbone.View.extend({
var data = [];
this.model.currentDocuments.each(function(doc){
this.model.currentRecords.each(function(doc){
var row = {};
self.model.fields.each(function(field){
row[field.id] = doc.getFieldValue(field);

View File

@@ -31,7 +31,7 @@ my.Timeline = Backbone.View.extend({
this.model.fields.bind('reset', function() {
self._setupTemporalField();
});
this.model.currentDocuments.bind('all', function() {
this.model.currentRecords.bind('all', function() {
self.reloadData();
});
var stateData = _.extend({
@@ -78,11 +78,11 @@ my.Timeline = Backbone.View.extend({
]
}
};
this.model.currentDocuments.each(function(doc) {
this.model.currentRecords.each(function(doc) {
var start = doc.get(self.state.get('startField'));
if (start) {
var end = moment(doc.get(self.state.get('endField')));
end = end ? end.toDate() : null;
var end = doc.get(self.state.get('endField'));
end = end ? moment(end).toDate() : null;
var tlEntry = {
"startDate": moment(start).toDate(),
"endDate": end,

View File

@@ -98,7 +98,7 @@ my.ColumnTransform = Backbone.View.extend({
}
this.el.modal('hide');
this.trigger('recline:flash', {message: "Updating all visible docs. This could take a while...", persist: true, loader: true});
var docs = self.model.currentDocuments.map(function(doc) {
var docs = self.model.currentRecords.map(function(doc) {
return doc.toJSON();
});
// TODO: notify about failed docs?
@@ -107,14 +107,14 @@ my.ColumnTransform = Backbone.View.extend({
function onCompletedUpdate() {
totalToUpdate += -1;
if (totalToUpdate === 0) {
self.trigger('recline:flash', {message: toUpdate.length + " documents updated successfully"});
self.trigger('recline:flash', {message: toUpdate.length + " records updated successfully"});
alert('WARNING: We have only updated the docs in this view. (Updating of all docs not yet implemented!)');
self.remove();
}
}
// TODO: Very inefficient as we search through all docs every time!
_.each(toUpdate, function(editedDoc) {
var realDoc = self.model.currentDocuments.get(editedDoc.id);
var realDoc = self.model.currentRecords.get(editedDoc.id);
realDoc.set(editedDoc);
realDoc.save().then(onCompletedUpdate).fail(onCompletedUpdate);
});
@@ -158,7 +158,7 @@ my.ColumnTransform = Backbone.View.extend({
var editFunc = costco.evalFunction(e.target.value);
if (!editFunc.errorMessage) {
errors.text('No syntax error.');
var docs = self.model.currentDocuments.map(function(doc) {
var docs = self.model.currentRecords.map(function(doc) {
return doc.toJSON();
});
var previewData = costco.previewTransform(docs, editFunc, self.state.currentColumn);