[#88,refactor/state,api-change][s]: convert state on DataExplorer to proper ObjectState model and rename config to state in DataExplorer initialization arguments.

* Also add more tests (#45).
This commit is contained in:
Rufus Pollock
2012-04-15 13:44:01 +01:00
parent 6a352f9c44
commit 8f33235933
2 changed files with 58 additions and 33 deletions

View File

@@ -1,4 +1,16 @@
/*jshint multistr:true */ /*jshint multistr:true */
// # Core View Functionality plus Data Explorer
//
// ## Common view concepts
//
// ### State
//
// TODO
//
// ### Read-only
//
// TODO
this.recline = this.recline || {}; this.recline = this.recline || {};
this.recline.View = this.recline.View || {}; this.recline.View = this.recline.View || {};
@@ -11,19 +23,21 @@ this.recline.View = this.recline.View || {};
// var myExplorer = new model.recline.DataExplorer({ // var myExplorer = new model.recline.DataExplorer({
// model: {{recline.Model.Dataset instance}} // model: {{recline.Model.Dataset instance}}
// el: {{an existing dom element}} // el: {{an existing dom element}}
// views: {{page views}} // views: {{dataset views}}
// config: {{config options -- see below}} // state: {{state configuration -- see below}}
// }); // });
// </pre> // </pre>
// //
// ### Parameters // ### Parameters
// //
// **model**: (required) Dataset instance. // **model**: (required) recline.model.Dataset instance.
// //
// **el**: (required) DOM element. // **el**: (required) DOM element to bind to. NB: the element already
// being in the DOM is important for rendering of some subviews (e.g.
// FlotGraph).
// //
// **views**: (optional) the views (Grid, Graph etc) for DataExplorer to // **views**: (optional) the dataset views (Grid, Graph etc) for
// show. This is an array of view hashes. If not provided // DataExplorer to show. This is an array of view hashes. If not provided
// just initialize a DataGrid with id 'grid'. Example: // just initialize a DataGrid with id 'grid'. Example:
// //
// <pre> // <pre>
@@ -45,13 +59,10 @@ this.recline.View = this.recline.View || {};
// ]; // ];
// </pre> // </pre>
// //
// **config**: Config options like: // **state**: state config for this view. Options are:
// //
// * readOnly: true/false (default: false) value indicating whether to // * readOnly: true/false (default: false) value indicating whether to
// operate in read-only mode (hiding all editing options). // operate in read-only mode (hiding all editing options).
//
// NB: the element already being in the DOM is important for rendering of
// FlotGraph subview.
my.DataExplorer = Backbone.View.extend({ my.DataExplorer = Backbone.View.extend({
template: ' \ template: ' \
<div class="recline-data-explorer"> \ <div class="recline-data-explorer"> \
@@ -89,13 +100,6 @@ my.DataExplorer = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
var self = this; var self = this;
this.el = $(this.el); this.el = $(this.el);
this.config = _.extend({
readOnly: false
},
options.config);
if (this.config.readOnly) {
this.setReadOnly();
}
// Hash of 'page' views (i.e. those for whole page) keyed by page name // Hash of 'page' views (i.e. those for whole page) keyed by page name
if (options.views) { if (options.views) {
this.pageViews = options.views; this.pageViews = options.views;
@@ -108,9 +112,9 @@ my.DataExplorer = Backbone.View.extend({
}) })
}]; }];
} }
this.state = {}; this.state = new recline.Model.ObjectState();
this._setupState(); // these must be called after pageViews are created
// this must be called after pageViews are created this._setupState(options.state);
this.render(); this.render();
this.router = new Backbone.Router(); this.router = new Backbone.Router();
@@ -168,7 +172,6 @@ my.DataExplorer = Backbone.View.extend({
render: function() { render: function() {
var tmplData = this.model.toTemplateJSON(); var tmplData = this.model.toTemplateJSON();
tmplData.displayCount = this.config.displayCount;
tmplData.views = this.pageViews; tmplData.views = this.pageViews;
var template = $.mustache(this.template, tmplData); var template = $.mustache(this.template, tmplData);
$(this.el).html(template); $(this.el).html(template);
@@ -233,21 +236,30 @@ my.DataExplorer = Backbone.View.extend({
} }
}, },
_setupState: function() { _setupState: function(initialState) {
var self = this; var self = this;
this.state = { var stateData = _.extend({
dataset: null, readOnly: false,
query: this.model.queryState.toJSON(), query: self.model.queryState.toJSON(),
currentView: null currentView: null
}; },
initialState);
this.state.set(stateData);
if (this.state.get('readOnly')) {
this.setReadOnly();
}
this.model.queryState.bind('change', function() { this.model.queryState.bind('change', function() {
self.state.queryState = this.model.queryState.toJSON(); self.state.set({queryState: self.model.queryState.toJSON()});
}); });
_.each(this.pageViews, function(pageView) { _.each(this.pageViews, function(pageView) {
if (pageView.view.state && pageView.view.state.bind) { if (pageView.view.state && pageView.view.state.bind) {
self.state['view-' + pageView.id] = pageView.view.state.toJSON(); var update = {};
update['view-' + pageView.id] = pageView.view.state.toJSON();
self.state.set(update);
pageView.view.state.bind('change', function() { pageView.view.state.bind('change', function() {
self.state['view-' + pageView.id] = pageView.view.state.toJSON(); var update = {};
update['view-' + pageView.id] = pageView.view.state.toJSON();
self.state.set(update);
}); });
} }
}); });

View File

@@ -24,11 +24,24 @@ test('getState', function () {
el: $el el: $el
}); });
var state = explorer.getState(); var state = explorer.getState();
ok(state.query); ok(state.get('query'));
equal(state.query.size, 100); equal(state.get('readOnly'), false);
deepEqual(state['view-grid'].hiddenFields, []); equal(state.get('query').size, 100);
deepEqual(state.get('view-grid').hiddenFields, []);
$el.remove(); $el.remove();
}); });
test('initialize state', function () {
var dataset = Fixture.getDataset();
var explorer = new recline.View.DataExplorer({
model: dataset,
state: {
readOnly: true
}
});
var state = explorer.getState();
ok(state.get('readOnly'));
});
})(this.jQuery); })(this.jQuery);