diff --git a/src/model.js b/src/model.js
index 9185113b..69df8116 100644
--- a/src/model.js
+++ b/src/model.js
@@ -8,17 +8,10 @@ this.recline.Model = this.recline.Model || {};
//
// A model must have the following (Backbone) attributes:
//
-// * fields: (aka columns) is the array of fields (column) to display. Each
-// entry in fields is a hash having at a minimum:
-// * id: a unique identifer for this field- usually this should match the key in the documents hash
-// * label: the visible label used for this field
-// * type: the type of the data
-// * data_key: the key used in the documents (usually this will be the same as
-// id but having this allows us to set more than one column reading the same
-// field.
-//
-// Other than standard list of Backbone methods it has two important attributes:
-//
+// * fields: (aka columns) is a FieldList listing all the fields on this
+// Dataset (this can be set explicitly, or, on fetch() of Dataset
+// information from the backend, or as is perhaps most common on the first
+// query)
// * currentDocuments: a DocumentList containing the Documents we have currently loaded for viewing (you update currentDocuments by calling getRows)
// * docCount: total number of documents in this dataset (obtained on a fetch for this Dataset)
my.Dataset = Backbone.Model.extend({
@@ -29,6 +22,7 @@ my.Dataset = Backbone.Model.extend({
if (backend && backend.constructor == String) {
this.backend = my.backends[backend];
}
+ this.fields = new my.FieldList();
this.currentDocuments = new my.DocumentList();
this.docCount = null;
this.queryState = new my.Query();
@@ -84,6 +78,38 @@ my.DocumentList = Backbone.Collection.extend({
model: my.Document
});
+// ## A Field (aka Column) on a Dataset
+//
+// Following attributes as standard:
+//
+// * id: a unique identifer for this field- usually this should match the key in the documents hash
+// * label: the visible label used for this field
+// * type: the type of the data
+my.Field = Backbone.Model.extend({
+ defaults: {
+ id: null,
+ label: null,
+ type: 'String'
+ },
+ // In addition to normal backbone initialization via a Hash you can also
+ // just pass a single argument representing id to the ctor
+ initialize: function(data) {
+ console.log(data);
+ // if a hash not passed in the first argument is set as value for key 0
+ if ('0' in data) {
+ this.set({id: data['0']});
+ }
+ if (this.attributes.label == null) {
+ this.set({label: this.id});
+ }
+ }
+});
+
+my.FieldList = Backbone.Collection.extend({
+ model: my.Field
+});
+
+// ## A Query object storing Dataset Query state
my.Query = Backbone.Model.extend({
defaults: {
size: 100
diff --git a/test/index.html b/test/index.html
index d9c240b5..cd87c9af 100644
--- a/test/index.html
+++ b/test/index.html
@@ -17,6 +17,7 @@
+
diff --git a/test/model.test.js b/test/model.test.js
new file mode 100644
index 00000000..33941a58
--- /dev/null
+++ b/test/model.test.js
@@ -0,0 +1,21 @@
+(function ($) {
+module("Model");
+
+test('Field: basics', function () {
+ var field = new recline.Model.Field({
+ id: 'x'
+ });
+ equal(field.attributes.label, 'x', 'Field label should be set from id');
+
+ var field = new recline.Model.Field({
+ id: 'x',
+ label: 'My label'
+ });
+ equal(field.attributes.label, 'My label', 'Field label should be set from id but not if explicitly provided');
+
+ var field = new recline.Model.Field('x');
+ equal(field.id, 'x', 'Set of id from single argumentst to ctor');
+ equal(field.attributes.id, 'x', 'Set of id from single argumentst to ctor');
+});
+
+})(this.jQuery);