Skip to content

Commit

Permalink
[datopian#425] Fix Dataset.query() when called with Query model
Browse files Browse the repository at this point in the history
The problem is that Backbone >= 0.9.9 doesn't accept anymore updating models
with:

    model.set(otherModel);

That needs to be changed to:

    model.set(otherModel.attributes);

Dataset.query() accepts both regular JS objects and Query models, so we need to
check the parameter type to support both.
  • Loading branch information
vitorbaptista committed May 14, 2014
1 parent fa1e4d3 commit 2a9f092
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/model.js
Expand Up @@ -191,7 +191,11 @@ my.Dataset = Backbone.Model.extend({
this.trigger('query:start');

if (queryObj) {
this.queryState.set(queryObj, {silent: true});
var attributes = queryObj;
if (queryObj instanceof my.Query) {
attributes = queryObj.toJSON();
}
this.queryState.set(attributes, {silent: true});
}
var actualQuery = this.queryState.toJSON();

Expand Down
25 changes: 25 additions & 0 deletions test/model.test.js
Expand Up @@ -177,6 +177,31 @@ test('Dataset getFieldsSummary', function () {
});
});

test('query with Query model', function () {
var dataset = new recline.Model.Dataset({
records: [{country: 'UK'}, {country: 'DE'}]
});
var query = new recline.Model.Query();
query.addFilter({type: 'term', field: 'country', term: 'DE'});

dataset.query(query).done(function (results) {
deepEqual(results.length, 1);
deepEqual(results.models[0].toJSON(), {country: 'DE'});
});
});

test('query with plain object', function () {
var dataset = new recline.Model.Dataset({
records: [{country: 'UK'}, {country: 'DE'}]
});
var query = {q: 'DE'};

dataset.query(query).done(function (results) {
deepEqual(results.length, 1);
deepEqual(results.models[0].toJSON(), {country: 'DE'});
});
});

test('fetch without and with explicit fields', function () {
var dataset = new recline.Model.Dataset({
backend: 'csv',
Expand Down

0 comments on commit 2a9f092

Please sign in to comment.