[#68,view-grid,refactor][xs]: switch to new field / doc renderer and deriver.
* remove existing cellRenderer and switch to calling doc.getFieldValue(field)
This commit is contained in:
@@ -8,16 +8,12 @@ this.recline.View = this.recline.View || {};
|
|||||||
//
|
//
|
||||||
// Provides a tabular view on a Dataset.
|
// Provides a tabular view on a Dataset.
|
||||||
//
|
//
|
||||||
// Initialize it with a recline.Dataset object.
|
// Initialize it with a `recline.Model.Dataset`.
|
||||||
//
|
|
||||||
// Additional options passed in second arguments. Options:
|
|
||||||
//
|
|
||||||
// * cellRenderer: function used to render individual cells. See DataGridRow for more.
|
|
||||||
my.DataGrid = Backbone.View.extend({
|
my.DataGrid = Backbone.View.extend({
|
||||||
tagName: "div",
|
tagName: "div",
|
||||||
className: "recline-grid-container",
|
className: "recline-grid-container",
|
||||||
|
|
||||||
initialize: function(modelEtc, options) {
|
initialize: function(modelEtc) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.el = $(this.el);
|
this.el = $(this.el);
|
||||||
_.bindAll(this, 'render');
|
_.bindAll(this, 'render');
|
||||||
@@ -26,7 +22,6 @@ my.DataGrid = Backbone.View.extend({
|
|||||||
this.model.currentDocuments.bind('remove', this.render);
|
this.model.currentDocuments.bind('remove', this.render);
|
||||||
this.state = {};
|
this.state = {};
|
||||||
this.hiddenFields = [];
|
this.hiddenFields = [];
|
||||||
this.options = options;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
events: {
|
events: {
|
||||||
@@ -210,9 +205,7 @@ my.DataGrid = Backbone.View.extend({
|
|||||||
model: doc,
|
model: doc,
|
||||||
el: tr,
|
el: tr,
|
||||||
fields: self.fields
|
fields: self.fields
|
||||||
},
|
});
|
||||||
self.options
|
|
||||||
);
|
|
||||||
newView.render();
|
newView.render();
|
||||||
});
|
});
|
||||||
this.el.toggleClass('no-hidden', (self.hiddenFields.length == 0));
|
this.el.toggleClass('no-hidden', (self.hiddenFields.length == 0));
|
||||||
@@ -226,14 +219,6 @@ my.DataGrid = Backbone.View.extend({
|
|||||||
//
|
//
|
||||||
// In addition you *must* pass in a FieldList in the constructor options. This should be list of fields for the DataGrid.
|
// In addition you *must* pass in a FieldList in the constructor options. This should be list of fields for the DataGrid.
|
||||||
//
|
//
|
||||||
// Additional options can be passed in a second hash argument. Options:
|
|
||||||
//
|
|
||||||
// * cellRenderer: function to render cells. Signature: function(value,
|
|
||||||
// field, doc) where value is the value of this cell, field is
|
|
||||||
// corresponding field object and document is the document object. Note
|
|
||||||
// that implementing functions can ignore arguments (e.g.
|
|
||||||
// function(value) would be a valid cellRenderer function).
|
|
||||||
//
|
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// <pre>
|
// <pre>
|
||||||
@@ -241,22 +226,12 @@ my.DataGrid = Backbone.View.extend({
|
|||||||
// model: dataset-document,
|
// model: dataset-document,
|
||||||
// el: dom-element,
|
// el: dom-element,
|
||||||
// fields: mydatasets.fields // a FieldList object
|
// fields: mydatasets.fields // a FieldList object
|
||||||
// }, {
|
// });
|
||||||
// cellRenderer: my-cell-renderer-function
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// </pre>
|
// </pre>
|
||||||
my.DataGridRow = Backbone.View.extend({
|
my.DataGridRow = Backbone.View.extend({
|
||||||
initialize: function(initData, options) {
|
initialize: function(initData) {
|
||||||
_.bindAll(this, 'render');
|
_.bindAll(this, 'render');
|
||||||
this._fields = initData.fields;
|
this._fields = initData.fields;
|
||||||
if (options && options.cellRenderer) {
|
|
||||||
this._cellRenderer = options.cellRenderer;
|
|
||||||
} else {
|
|
||||||
this._cellRenderer = function(value) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.el = $(this.el);
|
this.el = $(this.el);
|
||||||
this.model.bind('change', this.render);
|
this.model.bind('change', this.render);
|
||||||
},
|
},
|
||||||
@@ -291,7 +266,7 @@ my.DataGridRow = Backbone.View.extend({
|
|||||||
var cellData = this._fields.map(function(field) {
|
var cellData = this._fields.map(function(field) {
|
||||||
return {
|
return {
|
||||||
field: field.id,
|
field: field.id,
|
||||||
value: self._cellRenderer(doc.get(field.id), field, doc)
|
value: doc.getFieldValue(field)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return { id: this.id, cells: cellData }
|
return { id: this.id, cells: cellData }
|
||||||
|
|||||||
@@ -20,20 +20,6 @@ test('new DataGridRow View', function () {
|
|||||||
var tds = $el.find('td');
|
var tds = $el.find('td');
|
||||||
equal(tds.length, 3);
|
equal(tds.length, 3);
|
||||||
equal($(tds[1]).attr('data-field'), 'a');
|
equal($(tds[1]).attr('data-field'), 'a');
|
||||||
|
|
||||||
var view = new recline.View.DataGridRow({
|
|
||||||
model: doc
|
|
||||||
, el: $el
|
|
||||||
, fields: new recline.Model.FieldList([{id: 'a'}, {id: 'b'}])
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cellRenderer: function(value, field) {
|
|
||||||
return '<span class="field-' + field.id + '">' + value + '</span>';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
view.render();
|
|
||||||
var tds = $el.find('td .data-table-cell-value');
|
|
||||||
equal($(tds[0]).html(), '<span class="field-a">1</span>', 'Checking cellRenderer works');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
})(this.jQuery);
|
})(this.jQuery);
|
||||||
|
|||||||
Reference in New Issue
Block a user