[#88,state][s]: (and finally) introduce a recline.View.DataExplorer.restore function that restores from a serialized state.

* remove getState in favour of just using direct access to state object.
This commit is contained in:
Rufus Pollock
2012-04-15 22:47:16 +01:00
parent 7743534eac
commit 2a93aeb2c1
2 changed files with 45 additions and 13 deletions

View File

@@ -318,10 +318,12 @@ my.DataExplorer = Backbone.View.extend({
var graphState = qs['view-graph'] || qs.graph; var graphState = qs['view-graph'] || qs.graph;
graphState = graphState ? JSON.parse(graphState) : {}; graphState = graphState ? JSON.parse(graphState) : {};
var stateData = _.extend({ var stateData = _.extend({
readOnly: false,
query: query, query: query,
'view-graph': graphState, 'view-graph': graphState,
currentView: null backend: this.model.backend.__type__,
dataset: this.model.toJSON(),
currentView: null,
readOnly: false
}, },
initialState); initialState);
this.state.set(stateData); this.state.set(stateData);
@@ -353,16 +355,34 @@ my.DataExplorer = Backbone.View.extend({
}); });
} }
}); });
},
// ### getState
//
// Get the current state of dataset and views (see discussion of state above)
getState: function() {
return this.state;
} }
}); });
// ## restore
//
// Restore a DataExplorer instance from a serialized state including the associated dataset
my.DataExplorer.restore = function(state) {
// hack-y - restoring a memory dataset does not mean much ...
var dataset = null;
if (state.backend === 'memory') {
dataset = recline.Backend.createDataset(
[{stub: 'this is a stub dataset because we do not restore memory datasets'}],
[],
state.dataset
);
} else {
dataset = new recline.Model.Dataset(
state.dataset,
state.backend
);
}
var explorer = new my.DataExplorer({
model: dataset,
state: state
});
return explorer;
}
my.QueryEditor = Backbone.View.extend({ my.QueryEditor = Backbone.View.extend({
className: 'recline-query-editor', className: 'recline-query-editor',
template: ' \ template: ' \

View File

@@ -23,11 +23,13 @@ test('getState', function () {
model: dataset, model: dataset,
el: $el el: $el
}); });
var state = explorer.getState(); var state = explorer.state;
ok(state.get('query')); ok(state.get('query'));
equal(state.get('readOnly'), false); equal(state.get('readOnly'), false);
equal(state.get('query').size, 100); equal(state.get('query').size, 100);
deepEqual(state.get('view-grid').hiddenFields, []); deepEqual(state.get('view-grid').hiddenFields, []);
equal(state.get('backend'), 'memory');
ok(state.get('dataset').id !== null);
$el.remove(); $el.remove();
}); });
@@ -42,8 +44,18 @@ test('initialize state', function () {
} }
} }
}); });
var state = explorer.getState(); ok(explorer.state.get('readOnly'));
ok(state.get('readOnly')); });
test('restore (from serialized state)', function() {
var dataset = Fixture.getDataset();
var explorer = new recline.View.DataExplorer({
model: dataset,
});
var state = explorer.state.toJSON();
var explorerNew = recline.View.DataExplorer.restore(state);
var out = explorerNew.state.toJSON();
equal(out.backend, state.backend);
}); });
})(this.jQuery); })(this.jQuery);