[model,refator][s]: reintroduce summary on Record object deprecating recordSummary on Dataset.
* Reverses change in 1dadc1106bd2dbcad4580d3889e15ac44c865f80 * 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:
parent
8fbb0682b3
commit
d599f75e85
@ -2,23 +2,28 @@
|
||||
// must have div with class="ex-1"
|
||||
var $el = $('.ex-1');
|
||||
|
||||
// we will define this function display so we can reuse it below!
|
||||
function display(dataset) {
|
||||
// total number of records resulting from latest query
|
||||
$el.append('Total found: ' + dataset.recordCount + '<br />');
|
||||
$el.append('Total returned: ' + dataset.records.length);
|
||||
// total number of records resulting from latest query
|
||||
$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 the first record in the list - it returns an instance of the Record object
|
||||
var record = dataset.records.at(0);
|
||||
// get 2nd record in list (note collection indexes off 0!)
|
||||
// this is an instance of a Record object
|
||||
var record = dataset.records.at(1);
|
||||
|
||||
// Use the summary helper method which produces proper html
|
||||
// You could also do record.toJSON() to get a hash of the record data
|
||||
$el.append(dataset.recordSummary(record));
|
||||
}
|
||||
// if records have an id you can get by id too ...
|
||||
// var record = dataset.records.get(record-id);
|
||||
|
||||
// now display our existing dataset ...
|
||||
display(dataset);
|
||||
// To get record attribute we use 'get'
|
||||
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>');
|
||||
|
||||
|
||||
@ -6,5 +6,5 @@ dataset.fields.models[6] = new recline.Model.Field({
|
||||
type: 'geo_point'
|
||||
});
|
||||
var rec = dataset.records.at(0);
|
||||
$el.append(dataset.recordSummary(rec));
|
||||
$el.append(record.summary());
|
||||
|
||||
|
||||
41
src/model.js
41
src/model.js
@ -202,6 +202,7 @@ my.Dataset = Backbone.Model.extend({
|
||||
self.recordCount = queryResult.total;
|
||||
var docs = _.map(queryResult.hits, function(hit) {
|
||||
var _doc = new my.Record(hit);
|
||||
_doc.fields = self.fields;
|
||||
_doc.bind('change', function(doc) {
|
||||
self._changes.updates.push(doc.toJSON());
|
||||
});
|
||||
@ -254,18 +255,9 @@ my.Dataset = Backbone.Model.extend({
|
||||
return dfd.promise();
|
||||
},
|
||||
|
||||
// ### recordSummary
|
||||
//
|
||||
// Get a simple html summary of a Dataset record in form of key/value list
|
||||
// Deprecated (as of v0.5) - use record.summary()
|
||||
recordSummary: function(record) {
|
||||
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>: ' + record.getFieldValue(field) + '</div>';
|
||||
}
|
||||
});
|
||||
html += '</div>';
|
||||
return html;
|
||||
return record.summary();
|
||||
},
|
||||
|
||||
// ### _backendFromString(backendString)
|
||||
@ -329,14 +321,22 @@ my.Dataset.restore = function(state) {
|
||||
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({
|
||||
constructor: function Record() {
|
||||
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() {
|
||||
_.bindAll(this, 'getFieldValue');
|
||||
},
|
||||
@ -365,6 +365,21 @@ my.Record = Backbone.Model.extend({
|
||||
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
|
||||
// Instead, Dataset object that created this Record should take care of
|
||||
// handling these changes (discovery will occur via event notifications)
|
||||
|
||||
@ -102,7 +102,7 @@ my.Timeline = Backbone.View.extend({
|
||||
"startDate": start,
|
||||
"endDate": end,
|
||||
"headline": String(record.get('title') || ''),
|
||||
"text": record.get('description') || this.model.recordSummary(record)
|
||||
"text": record.get('description') || record.summary()
|
||||
};
|
||||
return tlEntry;
|
||||
} else {
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user