Skip to content

Commit

Permalink
Fixes #213.
Browse files Browse the repository at this point in the history
Stable Version 1.4.1.
  • Loading branch information
jmdobry committed Nov 12, 2014
1 parent 0e2c5e4 commit c6710fb
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 57 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
##### 1.4.1 - 11 November 2014

###### Backwards compatible bug fixes
- #213 - multiple orderBy does not work

##### 1.4.0 - 09 November 2014

###### Backwards compatible API changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Unlike Backbone and Ember Models, angular-data does not require the use of gette

Supporting relations, computed properties, model lifecycle control and a slew of other features, angular-data is the tool for giving your data the respect it deserves.

__Latest Release:__ [1.4.0](https://github.com/jmdobry/angular-data/releases/tag/1.4.0)
__Latest Release:__ [1.4.1](https://github.com/jmdobry/angular-data/releases/tag/1.4.1)

Angular-data is finally 1.0.!

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Jason Dobry",
"name": "angular-data",
"description": "Data store for Angular.js.",
"version": "1.4.0",
"version": "1.4.1",
"homepage": "http://angular-data.pseudobry.com/",
"repository": {
"type": "git",
Expand Down
66 changes: 40 additions & 26 deletions dist/angular-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4089,6 +4089,42 @@ function lifecycleNoop(resourceName, attrs, cb) {
cb(null, attrs);
}

function compare(DSUtils, orderBy, index, a, b) {
var def = orderBy[index];
var cA = a[def[0]], cB = b[def[0]];
if (DSUtils.isString(cA)) {
cA = DSUtils.upperCase(cA);
}
if (DSUtils.isString(cB)) {
cB = DSUtils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(DSUtils, orderBy, index + 1, a, b);
} else {
return 0;
}
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(DSUtils, orderBy, index + 1, a, b);
} else {
return 0;
}
}
}
}

function Defaults() {
}

Expand Down Expand Up @@ -4214,9 +4250,10 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o

// Apply 'orderBy'
if (orderBy) {
angular.forEach(orderBy, function (def) {
var index = 0;
angular.forEach(orderBy, function (def, i) {
if (_this.utils.isString(def)) {
def = [def, 'ASC'];
orderBy[i] = [def, 'ASC'];
} else if (!_this.utils.isArray(def)) {
throw new _this.errors.IllegalArgumentError('DS.filter(resourceName[, params][, options]): ' + JSON.stringify(def) + ': Must be a string or an array!', {
params: {
Expand All @@ -4228,30 +4265,7 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o
});
}
filtered = _this.utils.sort(filtered, function (a, b) {
var cA = a[def[0]], cB = b[def[0]];
if (_this.utils.isString(cA)) {
cA = _this.utils.upperCase(cA);
}
if (_this.utils.isString(cB)) {
cB = _this.utils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
return 0;
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
return 0;
}
}
return compare(_this.utils, orderBy, index, a, b);
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-data.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-data",
"description": "Data store for Angular.js.",
"version": "1.4.0",
"version": "1.4.1",
"homepage": "http://angular-data.pseudobry.com",
"repository": {
"type": "git",
Expand Down
66 changes: 40 additions & 26 deletions src/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ function lifecycleNoop(resourceName, attrs, cb) {
cb(null, attrs);
}

function compare(DSUtils, orderBy, index, a, b) {
var def = orderBy[index];
var cA = a[def[0]], cB = b[def[0]];
if (DSUtils.isString(cA)) {
cA = DSUtils.upperCase(cA);
}
if (DSUtils.isString(cB)) {
cB = DSUtils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(DSUtils, orderBy, index + 1, a, b);
} else {
return 0;
}
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(DSUtils, orderBy, index + 1, a, b);
} else {
return 0;
}
}
}
}

function Defaults() {
}

Expand Down Expand Up @@ -129,9 +165,10 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o

// Apply 'orderBy'
if (orderBy) {
angular.forEach(orderBy, function (def) {
var index = 0;
angular.forEach(orderBy, function (def, i) {
if (_this.utils.isString(def)) {
def = [def, 'ASC'];
orderBy[i] = [def, 'ASC'];
} else if (!_this.utils.isArray(def)) {
throw new _this.errors.IllegalArgumentError('DS.filter(resourceName[, params][, options]): ' + JSON.stringify(def) + ': Must be a string or an array!', {
params: {
Expand All @@ -143,30 +180,7 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o
});
}
filtered = _this.utils.sort(filtered, function (a, b) {
var cA = a[def[0]], cB = b[def[0]];
if (_this.utils.isString(cA)) {
cA = _this.utils.upperCase(cA);
}
if (_this.utils.isString(cB)) {
cB = _this.utils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
return 0;
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
return 0;
}
}
return compare(_this.utils, orderBy, index, a, b);
});
});
}
Expand Down
65 changes: 65 additions & 0 deletions test/integration/datastore/sync_methods/filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,69 @@ describe('DS.filter(resourceName[, params][, options])', function () {

assert.deepEqual(angular.toJson(DS.filter('Comment', params)), angular.toJson([p1, p2]), 'should keep p1 and p2');
});
it('should work with multiple orderBy', function () {
var items = [
{ id: 1, test: 1, test2: 1 },
{ id: 2, test: 2, test2: 2 },
{ id: 3, test: 3, test2: 3 },
{ id: 4, test: 1, test2: 4 },
{ id: 5, test: 2, test2: 5 },
{ id: 6, test: 3, test2: 6 },
{ id: 7, test: 1, test2: 1 },
{ id: 8, test: 2, test2: 2 },
{ id: 9, test: 3, test2: 3 },
{ id: 10, test: 1, test2: 4 },
{ id: 11, test: 2, test2: 5 },
{ id: 12, test: 3, test2: 6 }
];
var params = {};

Post.inject(items);

params.orderBy = [
['test', 'DESC'],
['test2', 'ASC'],
['id', 'ASC']
];

var posts = Post.filter(params);

assert.deepEqual(JSON.stringify(posts), JSON.stringify([
items[2],
items[8],
items[5],
items[11],
items[1],
items[7],
items[4],
items[10],
items[0],
items[6],
items[3],
items[9]
]));

params.orderBy = [
['test', 'DESC'],
['test2', 'ASC'],
['id', 'DESC']
];

posts = Post.filter(params);

assert.deepEqual(JSON.stringify(posts), JSON.stringify([
items[8],
items[2],
items[11],
items[5],
items[7],
items[1],
items[10],
items[4],
items[6],
items[0],
items[9],
items[3]
]));
});
});

0 comments on commit c6710fb

Please sign in to comment.