Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import mongoose, {Schema} from 'mongoose';
  2.  
  3. const UserSchema = new Schema({
  4. name: String,
  5. password: String,
  6. role: String
  7. });
  8.  
  9. UserSchema.methods.canDoSomeBusinessLogic = function(){
  10. return this.name === 'Jeff';
  11. };
  12.  
  13. UserSchema.methods.isAdmin = function(){
  14. return this.role === 'admin';
  15. };
  16.  
  17. # JS Class definition - classes/user.js
  18. export default class User {
  19. constructor(data = {}){
  20. Object.assign(this,data);
  21. }
  22.  
  23. canDoSomeBusinessLogic(){
  24. return this.name === 'Jeff';
  25. };
  26.  
  27. isAdmin(){
  28. return this.role === 'admin';
  29. }
  30. }
  31.  
  32. # Server - api/controllers/user.js
  33. import UserClass from
  34. User.findById(1,function(err,user){
  35. let user = new UserClass(user.toJSON();
  36. });
  37.  
  38. # Client - reducers/User.js
  39. export default function authReducer(state = null, action) {
  40. switch (action.type) {
  41. case GET_USER:
  42. return new UserClass(action.response.data);
  43. }
  44. }
  45.  
  46. # Client - containers/Page.jsx
  47. import {connect} from 'react-redux';
  48.  
  49. @connect(state => ({user: state.user}))
  50. export default class Page extends React.Component {
  51. render(){
  52. if(this.props.user.isAdmin()){
  53. // Some admin
  54. }
  55. }
  56. }
  57.  
  58. # helpers/user.js
  59. export function isAdmin(user){
  60. return user.role === 'admin';
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement