Skip to content

Instantly share code, notes, and snippets.

@girirajsharma
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girirajsharma/01ab8a35538fde599374 to your computer and use it in GitHub Desktop.
Save girirajsharma/01ab8a35538fde599374 to your computer and use it in GitHub Desktop.
module.factory('RealmEvents', function($resource) {
return $resource(authUrl + '/admin/realms/:id/events', {
id : '@realm'
});
});
//-------------------------------------------------------------app.js-----------------------------------------------------------
module.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
});
//-------------------------------------------------------(realm-events.html)----------------------------------------------------
<div class="form-group" data-ng-show="filter">
<label class="col-sm-2 control-label" for="filterEvents" class="control-label">Event Type</label>
<div class="col-sm-5">
<select ui-select2 data-ng-model="query.type[$index]" data-placeholder="Select Event Type(s)" multiple>
<option ng-repeat="event in filterEvents | unique: 'type'" value="{{event.type}}">{{event.type}}</option>
</select>
</div>
</div>
//-------------------------------------------------------Controller(realm.js)-----------------------------------------------------------
module.controller('RealmEventsCtrl', function($scope, RealmEvents, realm) {
$scope.realm = realm;
$scope.page = 0;
$scope.fquery = {
id : realm.realm,
max : 100,
first : 0
}
$scope.filterEvents = RealmEvents.query($scope.fquery);
$scope.query = {
id : realm.realm,
max : 5, // bound to ng-model query.max in UI
first : 0, // no ng-model in UI
type : [] // nound to ng-model query.type[$index] in UI
}
$scope.update = function() {
for (var i in $scope.query) {
if ($scope.query[i] == '') {
delete $scope.query[i];
}
}
$scope.events = RealmEvents.query($scope.query);
}
$scope.firstPage = function() {
$scope.query.first = 0;
$scope.update();
}
$scope.previousPage = function() {
$scope.query.first -= parseInt($scope.query.max);
if ($scope.query.first < 0) {
$scope.query.first = 0;
}
$scope.update();
}
$scope.nextPage = function() {
$scope.query.first += parseInt($scope.query.max);
$scope.update();
}
$scope.update();
});
//-----------------------------------------------------------------------------------------------------------------------
$scope.filterEvents = RealmEvents.query($scope.fquery);
Request URL:http://localhost:8081/auth/admin/realms/master/events?first=0&max=100
// This is OK and works perfectly and fetches required events
$scope.events = RealmEvents.query($scope.query);
Request URL:http://localhost:8081/auth/admin/realms/master/events?max=5&type={}
// Why there isn't "first" as URL query parameter ?
// Required URL: http://localhost:8081/auth/admin/realms/master/events?first=0&max=5&type=[]
On selecting LOGIN and REFRESH_TOKEN as Event Types in ui-Select2,
$scope.events = RealmEvents.query($scope.query);
Request URL:http://localhost:8081/auth/admin/realms/master/events?max=5&type={"undefined":["REFRESH_TOKEN","LOGIN"]}
// event type value(s) must be bound as an array and sent as query parameter, HOW ?
// Again, Why there isn't "first" as URL query parameter ?
// Required URL : http://localhost:8081/auth/admin/realms/master/events?first=0&max=5&type=["REFRESH_TOKEN","LOGIN"]
Because, "first" value is not sent as query param or else is unreferenced, it causes errors on clicking first, previous and next buttons.
On clicking the previous button:
Request URL:http://localhost:8081/auth/admin/realms/master/events?first=NaN&max=5&type={"undefined":[]}
// Required URL : http://localhost:8081/auth/admin/realms/master/events?first=0&max=5&type=[]
// In concise, i wish to send some values(int, string, array etc.) with URL as query parameters. Don't know how exactly arrays are sent with URL ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment