From cf42e43ff0f32ba24e940455b060ae278025942b Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sat, 18 Feb 2012 07:33:15 +0000 Subject: [PATCH] [#25,model][s]: introduce new Field Model and FieldList collection and attach to Dataset. * NB: no refactoring elsewhere yet to take account of this * model.test.js: tests for Field (first proper model tests!) --- src/model.js | 48 +++++++++++++++++++++++++++++++++++----------- test/index.html | 1 + test/model.test.js | 21 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 test/model.test.js 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);