Created
August 13, 2018 19:15
-
-
Save SalmonKing72/3ccf336576153715c8069f74944ef80c to your computer and use it in GitHub Desktop.
jQuery module snippets
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
// Using the module pattern for a jQuery feature | |
$( document ).ready(function() { | |
var feature = (function() { | |
var items = $( "#myFeature li" ); | |
var container = $( "<div class='container'></div>" ); | |
var currentItem = null; | |
var urlBase = "/foo.php?item="; | |
var createContainer = function() { | |
var item = $( this ); | |
var _container = container.clone().appendTo( item ); | |
item.data( "container", _container ); | |
}; | |
var buildUrl = function() { | |
return urlBase + currentItem.attr( "id" ); | |
}; | |
var showItem = function() { | |
currentItem = $( this ); | |
getContent( showContent ); | |
}; | |
var showItemByIndex = function( idx ) { | |
$.proxy( showItem, items.get( idx ) ); | |
}; | |
var getContent = function( callback ) { | |
currentItem.data( "container" ).load( buildUrl(), callback ); | |
}; | |
var showContent = function() { | |
currentItem.data( "container" ).show(); | |
hideContent(); | |
}; | |
var hideContent = function() { | |
currentItem.siblings().each(function() { | |
$( this ).data( "container" ).hide(); | |
}); | |
}; | |
items.each( createContainer ).click( showItem ); | |
return { | |
showItemByIndex: showItemByIndex | |
}; | |
})(); | |
feature.showItemByIndex( 0 ); | |
}); |
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
// Using an object literal for a jQuery feature | |
var myFeature = { | |
init: function( settings ) { | |
myFeature.config = { | |
items: $( "#myFeature li" ), | |
container: $( "<div class='container'></div>" ), | |
urlBase: "/foo.php?item=" | |
}; | |
// Allow overriding the default config | |
$.extend( myFeature.config, settings ); | |
myFeature.setup(); | |
}, | |
setup: function() { | |
myFeature.config.items | |
.each( myFeature.createContainer ) | |
.click( myFeature.showItem ); | |
}, | |
createContainer: function() { | |
var item = $( this ); | |
var container = myFeature.config.container | |
.clone() | |
.appendTo( item ); | |
item.data( "container", container ); | |
}, | |
buildUrl: function() { | |
return myFeature.config.urlBase + myFeature.currentItem.attr( "id" ); | |
}, | |
showItem: function() { | |
myFeature.currentItem = $( this ); | |
myFeature.getContent( myFeature.showContent ); | |
}, | |
getContent: function( callback ) { | |
var url = myFeature.buildUrl(); | |
myFeature.currentItem.data( "container" ).load( url, callback ); | |
}, | |
showContent: function() { | |
myFeature.currentItem.data( "container" ).show(); | |
myFeature.hideContent(); | |
}, | |
hideContent: function() { | |
myFeature.currentItem.siblings().each(function() { | |
$( this ).data( "container" ).hide(); | |
}); | |
} | |
}; | |
$( document ).ready( myFeature.init ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment