Skip to content
Browse files

Odstraneni PS zavislosti na angularu, prejmenovani sort na order

  • Loading branch information...
1 parent 04b5e95 commit fecd447304b9724705f2108eca647c7d63633fe0 @JakubMrozek committed Feb 6, 2013
View
2 apiary.apib
@@ -233,7 +233,7 @@ GET /products?url={url}
Seznam produktů v kategorii.
-GET /products?category={kategorie}&filter={filter}&limit={limit}&offset={offset}&sort={sort}
+GET /products?category={kategorie}&filter={filter}&limit={limit}&offset={offset}&order={order}
< 200
< Content-Type: application/json
{"count":120,
View
35 public/js/ParametricSearch.js
@@ -4,14 +4,14 @@
*
* Konfiguracni parametry:
* limit - maximalni pocet objektu na stranku
- * sortColumns - sloupce, podle kterych je mozne radit
+ * orderColumns - sloupce, podle kterych je mozne radit
*
* @param {Object} config Konfiguracni objekt.
*/
function ParametricSearch(config) {
this._limit = config.limit;
- this._sortColumns = config.sortColumns;
+ this._orderColumns = config.orderColumns;
}
@@ -36,7 +36,7 @@ ParametricSearch.prototype.getParams = function() {
*/
ParametricSearch.prototype.getParam = function(name, def) {
- if (angular.isUndefined(this._params[name])) {
+ if (this._isUndefined(this._params[name])) {
return def;
}
return this._params[name] || def;
@@ -61,7 +61,7 @@ ParametricSearch.prototype.getParam = function(name, def) {
ParametricSearch.prototype.getFilterParam = function(name, def) {
var filter = this.getParam('filter');
- if (angular.isUndefined(filter)) {
+ if (this._isUndefined(filter)) {
return def;
}
return filter[name] || def;
@@ -120,7 +120,7 @@ ParametricSearch.prototype.getLimit = function() {
ParametricSearch.prototype.getPage = function() {
var offset = this._params.offset;
- if (angular.isUndefined(offset)) return 1;
+ if (this._isUndefined(offset)) return 1;
var page = (offset / this.getLimit()) + 1;
if (page < 1) return 1;
if (offset % this.getLimit() !== 0) return 1;
@@ -147,12 +147,12 @@ ParametricSearch.prototype.getOffset = function() {
* @return {String}
*/
-ParametricSearch.prototype.getSort = function() {
- var key = this._sortColumns.indexOf(this._params.sort);
+ParametricSearch.prototype.getOrder = function() {
+ var key = this._orderColumns.indexOf(this._params.order);
if (~key) {
- return this._sortColumns[key];
+ return this._orderColumns[key];
} else {
- return this._sortColumns[0];
+ return this._orderColumns[0];
}
};
@@ -161,21 +161,30 @@ ParametricSearch.prototype.getSort = function() {
* Projde vsechny parametry v parametru filter a vytvori z nich objekt.
*
* Priklad URL:
- * /abc?sort=price&filter=aaa:2@bbb:7,3
+ * /abc?order=price&filter=aaa:2@bbb:7,3
*
- * ps.getParams() vrati {sort: price, filter: {aaa: ['2'], bbb: ['7', '3']}}
+ * ps.getParams() vrati {order: price, filter: {aaa: ['2'], bbb: ['7', '3']}}
*/
ParametricSearch.prototype._parseFilter = function() {
var params = {};
var filter = this._params.filter;
- if (angular.isString(filter)) {
+ if (this._isString(filter)) {
filter.split('@').forEach(function(rule){
var parts = rule.split(':');
if (parts.length !== 2) return;
params[parts[0]] = parts[1].split(',');
});
}
this._params.filter = params;
-
};
+
+
+ParametricSearch.prototype._isUndefined = function(val) {
+ return typeof val === 'undefined';
+};
+
+
+ParametricSearch.prototype._isString = function(val) {
+ return typeof val === 'string';
+};
View
8 public/js/zdrojak.controller.js
@@ -104,7 +104,7 @@ module.controller('PageCtrl', ['$scope', '$routeParams', 'api', function ($scope
module.controller('CategoryCtrl', ['$scope', '$routeParams', '$location', 'parametricSearch', 'api', function ($scope, $routeParams, $location, parametricSearch, api) {
var query = {};
- var ps = parametricSearch({limit: 10, sortColumns: ['price', '-price']});
+ var ps = parametricSearch({limit: 10, orderColumns: ['price', '-price']});
$scope.category = api.category.show({url: $routeParams.category}, function(){
$scope.category.params.forEach(function(param){
@@ -115,7 +115,7 @@ module.controller('CategoryCtrl', ['$scope', '$routeParams', '$location', 'param
});
});
$scope.price = ps.getFilterParamAsString('price', $scope.category.maxPrice);
- $scope.sort = ps.getSort();
+ $scope.order = ps.getOrder();
$scope.limit = ps.getLimit();
$scope.page = ps.getPage();
$scope.load(ps.getOffset(), false);
@@ -126,7 +126,7 @@ module.controller('CategoryCtrl', ['$scope', '$routeParams', '$location', 'param
reset = angular.isDefined(reset) ? reset : true;
$scope.load(offset, reset);
$location.search({
- filter: query.filter, sort: query.sort,
+ filter: query.filter, order: query.order,
offset: query.offset, limit: query.limit
});
};
@@ -135,7 +135,7 @@ module.controller('CategoryCtrl', ['$scope', '$routeParams', '$location', 'param
$scope.load = function(offset, reset) {
query.filter = $scope.serialize();
query.category = $routeParams.category;
- query.sort = $scope.sort;
+ query.order = $scope.order;
query.offset = offset || 0;
query.limit = $scope.limit;
$scope.results = api.product.index(query, function(){
View
12 public/partials/category.html
@@ -22,13 +22,13 @@
</ul>
</fieldset>
<div id="category-controls">
- <span ng-show="sort=='price'">
- <input ng-click="sort='price';filter()" type="button" class="btn btn-primary" value="Od nejlevnějšího">
- <input ng-click="sort='-price';filter()" type="button" class="btn" value="Od nejdražšího">
+ <span ng-show="order=='price'">
+ <input ng-click="order='price';filter()" type="button" class="btn btn-primary" value="Od nejlevnějšího">
+ <input ng-click="order='-price';filter()" type="button" class="btn" value="Od nejdražšího">
</span>
- <span ng-hide="sort=='price'">
- <input ng-click="sort='price';filter()" type="button" class="btn" value="Od nejlevnějšího">
- <input ng-click="sort='-price';filter()" type="button" class="btn btn-primary" value="Od nejdražšího">
+ <span ng-hide="order=='price'">
+ <input ng-click="order='price';filter()" type="button" class="btn" value="Od nejlevnějšího">
+ <input ng-click="order='-price';filter()" type="button" class="btn btn-primary" value="Od nejdražšího">
</span>
</div>
</div>
View
2 test/frontend/apiary.js
@@ -168,7 +168,7 @@ var apiary = [
{
"description": "Seznam produktů v kategorii.",
"method": "GET",
- "url": "/products?category={kategorie}&filter={filter}&limit={limit}&offset={offset}&sort={sort}",
+ "url": "/products?category={kategorie}&filter={filter}&limit={limit}&offset={offset}&order={order}",
"request": {
"headers": {},
"body": ""
View
25 test/frontend/unit/ParametricSearch.test.js
@@ -3,7 +3,7 @@ describe('ParametricSearch', function(){
var ps;
beforeEach(function(){
ps = new ParametricSearch({
- limit: 10, sortColumns: ['price', '-price']
+ limit: 10, orderColumns: ['price', '-price']
});
});
@@ -39,18 +39,18 @@ describe('ParametricSearch', function(){
describe('razeni', function(){
it('vrati sloupec, podle ktereho se ma strankovat', function(){
- ps.setParams({sort: '-price'});
- expect(ps.getSort()).toBe('-price');
+ ps.setParams({order: '-price'});
+ expect(ps.getOrder()).toBe('-price');
});
it('vrati vychozi sloupec strankovani, pokud neni zadny zadan', function(){
ps.setParams({});
- expect(ps.getSort()).toBe('price');
+ expect(ps.getOrder()).toBe('price');
});
it('vrati vychozi sloupec strankovani, pokud zadany ve vyctu neexistuje', function(){
- ps.setParams({sort: 'abc'});
- expect(ps.getSort()).toBe('price');
+ ps.setParams({order: 'abc'});
+ expect(ps.getOrder()).toBe('price');
});
});
@@ -71,4 +71,17 @@ describe('ParametricSearch', function(){
});
});
+ describe('pomocne metody', function(){
+ it('vrati true, pokud je hodnota typu undefined', function(){
+ var abc = {efg: true};
+ expect(ps._isUndefined(abc.abc)).toBeTruthy();
+ expect(ps._isUndefined(abc.efg)).toBeFalsy();
+ });
+ it('vrati true, pokud je hodnota typu retezec', function(){
+ expect(ps._isString('abeceda')).toBeTruthy();
+ expect(ps._isString(1)).toBeFalsy();
+ expect(ps._isString({})).toBeFalsy();
+ });
+ });
+
});

0 comments on commit fecd447

Please sign in to comment.
Something went wrong with that request. Please try again.