Created
March 28, 2013 23:14
-
-
Save imehr/5267592 to your computer and use it in GitHub Desktop.
javascript #module pattern
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
// http://stackoverflow.com/questions/12042260/how-to-use-the-javascript-module-pattern-in-a-real-example | |
var myApp = (function() { | |
var someElement = $("#foo"); //some element I know I'll use lots | |
var addMessage = function(message) { | |
$.ajax({ | |
url: '/test', | |
type: 'POST', | |
dataType: "json", | |
data: {'message' : message}, | |
success: function(data) { | |
... | |
}, | |
error: function() { | |
... | |
} | |
}); | |
}; | |
var inputClick = function(event) { | |
event.preventDefault(); | |
//depending on if you'll reuse these selectors throughout the app I might have these as variables | |
$('.loading').html('<img class="remove_loading" src="/graphics/loading.gif" />'); | |
var message = $(".wallmessage").val(); | |
if (message == ""){ | |
$("#messageempty").jmNotify(); | |
$('.remove_loading').remove(); | |
} else { | |
addMessage(message); | |
} | |
}; | |
var bindFunctions = function() { | |
$("input#share").on("click", inputClick) | |
}; | |
var init = function() { | |
bindFunctions(); | |
}; | |
return { | |
addMessage: addMessage | |
//anything else you want available | |
//through myApp.function() | |
//or expose variables here too | |
}; | |
})(); | |
//usage | |
myApp.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment