[#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:
38
src/view.js
38
src/view.js
@@ -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: ' \
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user