Created
July 17, 2015 06:19
-
-
Save anonymous/8e307fc3cc0a10f5ca8e to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/erewat
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="http://code.jquery.com/jquery-latest.js"></script> | |
| <meta charset=utf-8 /> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <div class="members-wrapper"></div> | |
| <script> | |
| </script> | |
| <script id="jsbin-javascript"> | |
| // The data store: | |
| var usersData = [ | |
| {firstName:"tommy", lastName:"MalCom", email:"test@test.com", id:102}, | |
| {firstName:"Peter", lastName:"brecHt", email:"test2@test2.com", id:103}, | |
| {firstName:"RoHan", lastName:"sahu", email:"test3@test3.com", id:104} | |
| ]; | |
| function titleCaseName(str) | |
| { | |
| return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); | |
| } | |
| // Our object with the chainable methods | |
| var userController = { | |
| currentUser:"", | |
| findUser:function (userEmail) { | |
| var arrayLength = usersData.length, i; | |
| for (i = arrayLength - 1; i >= 0; i--) { | |
| if (usersData[i].email === userEmail) { | |
| this.currentUser = usersData[i]; | |
| break; | |
| } | |
| } | |
| return this; | |
| }, | |
| formatName:function () { | |
| if (this.currentUser) { | |
| this.currentUser.fullName = titleCaseName (this.currentUser.firstName) + " " + titleCaseName (this.currentUser.lastName); | |
| } | |
| return this; | |
| }, | |
| createLayout:function () { | |
| if (this.currentUser) { | |
| this.currentUser.viewData = "<h2>Member: " + this.currentUser.fullName + "</h2>" + "<p>ID: " + this.currentUser.id + "</p>" + "<p>Email: " + this.currentUser.email + "</p>"; | |
| } | |
| return this; | |
| }, | |
| displayUser:function () { | |
| if (!this.currentUser) return; | |
| $(".members-wrapper").append(this.currentUser.viewData); | |
| } | |
| }; | |
| // Now, use the chaninable methods with expressiveness: | |
| userController.findUser("test2@test2.com").formatName().createLayout().displayUser(); | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript"> | |
| // The data store: | |
| var usersData = [ | |
| {firstName:"tommy", lastName:"MalCom", email:"test@test.com", id:102}, | |
| {firstName:"Peter", lastName:"brecHt", email:"test2@test2.com", id:103}, | |
| {firstName:"RoHan", lastName:"sahu", email:"test3@test3.com", id:104} | |
| ]; | |
| function titleCaseName(str) | |
| { | |
| return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); | |
| } | |
| // Our object with the chainable methods | |
| var userController = { | |
| currentUser:"", | |
| findUser:function (userEmail) { | |
| var arrayLength = usersData.length, i; | |
| for (i = arrayLength - 1; i >= 0; i--) { | |
| if (usersData[i].email === userEmail) { | |
| this.currentUser = usersData[i]; | |
| break; | |
| } | |
| } | |
| return this; | |
| }, | |
| formatName:function () { | |
| if (this.currentUser) { | |
| this.currentUser.fullName = titleCaseName (this.currentUser.firstName) + " " + titleCaseName (this.currentUser.lastName); | |
| } | |
| return this; | |
| }, | |
| createLayout:function () { | |
| if (this.currentUser) { | |
| this.currentUser.viewData = "<h2>Member: " + this.currentUser.fullName + "</h2>" + "<p>ID: " + this.currentUser.id + "</p>" + "<p>Email: " + this.currentUser.email + "</p>"; | |
| } | |
| return this; | |
| }, | |
| displayUser:function () { | |
| if (!this.currentUser) return; | |
| $(".members-wrapper").append(this.currentUser.viewData); | |
| } | |
| }; | |
| // Now, use the chaninable methods with expressiveness: | |
| userController.findUser("test2@test2.com").formatName().createLayout().displayUser(); | |
| </script></body> | |
| </html> |
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
| // The data store: | |
| var usersData = [ | |
| {firstName:"tommy", lastName:"MalCom", email:"test@test.com", id:102}, | |
| {firstName:"Peter", lastName:"brecHt", email:"test2@test2.com", id:103}, | |
| {firstName:"RoHan", lastName:"sahu", email:"test3@test3.com", id:104} | |
| ]; | |
| function titleCaseName(str) | |
| { | |
| return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); | |
| } | |
| // Our object with the chainable methods | |
| var userController = { | |
| currentUser:"", | |
| findUser:function (userEmail) { | |
| var arrayLength = usersData.length, i; | |
| for (i = arrayLength - 1; i >= 0; i--) { | |
| if (usersData[i].email === userEmail) { | |
| this.currentUser = usersData[i]; | |
| break; | |
| } | |
| } | |
| return this; | |
| }, | |
| formatName:function () { | |
| if (this.currentUser) { | |
| this.currentUser.fullName = titleCaseName (this.currentUser.firstName) + " " + titleCaseName (this.currentUser.lastName); | |
| } | |
| return this; | |
| }, | |
| createLayout:function () { | |
| if (this.currentUser) { | |
| this.currentUser.viewData = "<h2>Member: " + this.currentUser.fullName + "</h2>" + "<p>ID: " + this.currentUser.id + "</p>" + "<p>Email: " + this.currentUser.email + "</p>"; | |
| } | |
| return this; | |
| }, | |
| displayUser:function () { | |
| if (!this.currentUser) return; | |
| $(".members-wrapper").append(this.currentUser.viewData); | |
| } | |
| }; | |
| // Now, use the chaninable methods with expressiveness: | |
| userController.findUser("test2@test2.com").formatName().createLayout().displayUser(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment