[#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!)
This commit is contained in:
parent
437dbfc80b
commit
cf42e43ff0
48
src/model.js
48
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
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
<script src="sinon-qunit/1.0.0/sinon-qunit.js"></script>
|
||||
|
||||
<script type="text/javascript" src="../src/model.js"></script>
|
||||
<script type="text/javascript" src="model.test.js"></script>
|
||||
<script type="text/javascript" src="../src/backend.js"></script>
|
||||
<script type="text/javascript" src="backend.test.js"></script>
|
||||
<script type="text/javascript" src="../src/view.js"></script>
|
||||
|
||||
21
test/model.test.js
Normal file
21
test/model.test.js
Normal file
@ -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);
|
||||
Loading…
x
Reference in New Issue
Block a user