Last active
December 12, 2015 12:29
-
-
Save kevinchampion/4772419 to your computer and use it in GitHub Desktop.
Javascript namespacing pattern
This file contains 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
/** | |
* This pattern allows you to manually return the functions you want to be public, | |
* thus allowing for private functions. | |
*/ | |
var Mcard = (function() { | |
/** | |
* Helper to update form and results based on query as retrieved from a url | |
* change. | |
*/ | |
function updateContent() { | |
console.log('updateContent'); | |
updateForm(params); | |
} | |
/** | |
* Helper to update search form field values based on parameters. | |
* | |
* @param params | |
* Key value object containing the parameters and values. | |
*/ | |
function updateForm(params) { | |
console.log('updateForm', params); | |
} | |
return { | |
updateForm: updateForm, | |
updateContent: updateContent | |
} | |
})(); |
This file contains 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
/** | |
* This pattern allows you to define a tree structure for your object, | |
* enabling the possibility for more semantic code. | |
*/ | |
var Mcard = { | |
/** | |
* Helper to update form and results based on query as retrieved from a url | |
* change. | |
*/ | |
updateContent:function() { | |
console.log('updateContent'); | |
this.updateForm(params); // Does NOT work. | |
Mcard.updateForm(params); // Does work. | |
updateForms(params); // Does NOT work, but could work if updateContent was placed below updateForms. | |
}, | |
/** | |
* Helper to update search form field values based on parameters. | |
* | |
* @param params | |
* Key value object containing the parameters and values. | |
*/ | |
updateForm:function(params) { | |
console.log('updateForm', params); | |
}, | |
reveal : { | |
changeExpiration:function() { | |
$('.expiration-form').removeClass('hidden'); | |
}, | |
republish:function() { | |
$('.expiration-form, #edit-title-wrapper, #edit-offer-description-wrapper, fieldset.collapsible').removeClass('hidden'); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment