[model,refator][s]: reintroduce summary on Record object deprecating recordSummary on Dataset.

* Reverses change in 1dadc1106b
* Record now has fields attribute passed down from Dataset. This is needed in order to support summary method and also makes sense -- as pointed out by @zephod
* Update examples
This commit is contained in:
Rufus Pollock
2012-07-09 01:28:01 +01:00
parent 8fbb0682b3
commit d599f75e85
5 changed files with 66 additions and 30 deletions

View File

@@ -2,23 +2,28 @@
// must have div with class="ex-1" // must have div with class="ex-1"
var $el = $('.ex-1'); var $el = $('.ex-1');
// we will define this function display so we can reuse it below! // total number of records resulting from latest query
function display(dataset) { $el.append('Total found: ' + dataset.recordCount + '<br />');
// total number of records resulting from latest query $el.append('Total returned: ' + dataset.records.length);
$el.append('Total found: ' + dataset.recordCount + '<br />');
$el.append('Total returned: ' + dataset.records.length);
$el.append('<hr />'); $el.append('<hr />');
// dataset.records is a Backbone Collection of Records that resulted from latest query (hence "current") // get 2nd record in list (note collection indexes off 0!)
// Get the first record in the list - it returns an instance of the Record object // this is an instance of a Record object
var record = dataset.records.at(0); var record = dataset.records.at(1);
// Use the summary helper method which produces proper html // if records have an id you can get by id too ...
// You could also do record.toJSON() to get a hash of the record data // var record = dataset.records.get(record-id);
$el.append(dataset.recordSummary(record));
}
// now display our existing dataset ... // To get record attribute we use 'get'
display(dataset); var recdate = record.get('date');
$el.append('Date is: ' + recdate);
$el.append('<hr />');
// We can also convert the Record back to simple JS object
var simple = record.toJSON();
$el.append('<h4>Record as simple object</h4>');
$el.append('<pre>' + JSON.stringify(simple, null, 2) + '</pre>');

View File

@@ -6,5 +6,5 @@ dataset.fields.models[6] = new recline.Model.Field({
type: 'geo_point' type: 'geo_point'
}); });
var rec = dataset.records.at(0); var rec = dataset.records.at(0);
$el.append(dataset.recordSummary(rec)); $el.append(record.summary());

View File

@@ -202,6 +202,7 @@ my.Dataset = Backbone.Model.extend({
self.recordCount = queryResult.total; self.recordCount = queryResult.total;
var docs = _.map(queryResult.hits, function(hit) { var docs = _.map(queryResult.hits, function(hit) {
var _doc = new my.Record(hit); var _doc = new my.Record(hit);
_doc.fields = self.fields;
_doc.bind('change', function(doc) { _doc.bind('change', function(doc) {
self._changes.updates.push(doc.toJSON()); self._changes.updates.push(doc.toJSON());
}); });
@@ -254,18 +255,9 @@ my.Dataset = Backbone.Model.extend({
return dfd.promise(); return dfd.promise();
}, },
// ### recordSummary // Deprecated (as of v0.5) - use record.summary()
//
// Get a simple html summary of a Dataset record in form of key/value list
recordSummary: function(record) { recordSummary: function(record) {
var html = '<div class="recline-record-summary">'; return record.summary();
this.fields.each(function(field) {
if (field.id != 'id') {
html += '<div class="' + field.id + '"><strong>' + field.get('label') + '</strong>: ' + record.getFieldValue(field) + '</div>';
}
});
html += '</div>';
return html;
}, },
// ### _backendFromString(backendString) // ### _backendFromString(backendString)
@@ -329,14 +321,22 @@ my.Dataset.restore = function(state) {
return dataset; return dataset;
}; };
// ## <a id="record">A Record (aka Row)</a> // ## <a id="record">A Record</a>
// //
// A single entry or row in the dataset // A single record (or row) in the dataset
my.Record = Backbone.Model.extend({ my.Record = Backbone.Model.extend({
constructor: function Record() { constructor: function Record() {
Backbone.Model.prototype.constructor.apply(this, arguments); Backbone.Model.prototype.constructor.apply(this, arguments);
}, },
// ### initialize
//
// Create a Record
//
// You usually will not do this directly but will have records created by
// Dataset e.g. in query method
//
// Certain methods require presence of a fields attribute (identical to that on Dataset)
initialize: function() { initialize: function() {
_.bindAll(this, 'getFieldValue'); _.bindAll(this, 'getFieldValue');
}, },
@@ -365,6 +365,21 @@ my.Record = Backbone.Model.extend({
return val; return val;
}, },
// ### summary
//
// Get a simple html summary of this record in form of key/value list
summary: function(record) {
var self = this;
var html = '<div class="recline-record-summary">';
this.fields.each(function(field) {
if (field.id != 'id') {
html += '<div class="' + field.id + '"><strong>' + field.get('label') + '</strong>: ' + self.getFieldValue(field) + '</div>';
}
});
html += '</div>';
return html;
},
// Override Backbone save, fetch and destroy so they do nothing // Override Backbone save, fetch and destroy so they do nothing
// Instead, Dataset object that created this Record should take care of // Instead, Dataset object that created this Record should take care of
// handling these changes (discovery will occur via event notifications) // handling these changes (discovery will occur via event notifications)

View File

@@ -102,7 +102,7 @@ my.Timeline = Backbone.View.extend({
"startDate": start, "startDate": start,
"endDate": end, "endDate": end,
"headline": String(record.get('title') || ''), "headline": String(record.get('title') || ''),
"text": record.get('description') || this.model.recordSummary(record) "text": record.get('description') || record.summary()
}; };
return tlEntry; return tlEntry;
} else { } else {

View File

@@ -247,6 +247,22 @@ test('_normalizeRecordsAndFields', function () {
}); });
}); });
// =================================
// Record
module("Model Record");
test('summary', function () {
var dataset = new recline.Model.Dataset({
records: [ {a: 1, b: 2} ]
});
var record = dataset.records.at(0);
var out = record.summary();
var exp = '<div class="recline-record-summary"><div class="a"><strong>a</strong>: 1</div><div class="b"><strong>b</strong>: 2</div></div>'
equal(out, exp);
});
// ================================= // =================================
// Query // Query