Last active
August 29, 2015 14:00
-
-
Save bahattincinic/11289745 to your computer and use it in GitHub Desktop.
Permission control with KnockoutJs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Permissions(){ | |
var self = this; | |
/* Permissions */ | |
self.permissions = ko.observableArray([]); | |
/* | |
* Set Permission | |
* @param{String} key | |
* @param{boolean} value | |
* @return{boolean} | |
* Requirements: Underscore Js | |
* Sample: self.set_permission('dashboard_statistics', true); --> bool | |
*/ | |
self.setPermission = function(key, value){ | |
var check = _.where(self.permissions(), {Key:key}); | |
if(check.length == 0 && typeof value == "boolean"){ | |
self.permissions.push({Key:key, Value:value}); | |
return true; | |
}else{ | |
return false; | |
} | |
}; | |
/* | |
* Check Permission | |
* @param{String} key | |
* @return{boolean} | |
* Requirements: Underscore Js | |
* Sample: self.has_permission('dashboard_statistics'); --> bool | |
*/ | |
self.hasPermission = function(key){ | |
var result = _.where(self.permissions(), {Key:key}); | |
if(result.length > 0){ | |
return result[0].Value; | |
}else{ | |
return false; | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Persons(){ | |
this.permissions = new Permissions() | |
} | |
var person = new Persons(); | |
person.permissions.setPermission('create_user', true); | |
person.permissions.setPermission('edit_user', false); | |
person.permissions.hasPermission('create_user') // true | |
person.permissions.hasPermission('edit_user') // false | |
// HTML: <span data-bind="visible: $root.permissions.hasPermission('create_user')">Create User </span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment