-
-
Save tommcfarlin/3d708d486ef8cbcbdb5c6cf932b6e9c7 to your computer and use it in GitHub Desktop.
[WordPress, JavaScript] Creating jQuery Components in WordPress
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
$.get(acme_ajax_object.ajax_url, { | |
action: 'get_data', | |
security: acme_ajax_object.security | |
}, function(response) { | |
// More to come... | |
}); |
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
var $container = $('#component-container'), | |
i, l = 0; | |
$.get(acme_ajax_object.ajax_url, { | |
action: 'get_data', | |
security: acme_ajax_object.security | |
}, function( response ) { | |
for( i = 0, l = response.data.length; i < l; i++ ) { | |
$container.append( | |
_createComponent(response.data[i]) | |
); | |
} | |
}); |
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
/** | |
* Creates a component to be added from the DOM based on the incoming data. | |
* | |
* For the purposes of this function, we're assuming there are several properties | |
* on data of which we're already aware. | |
* | |
* @param data The object containing the properties we need for the component. | |
* | |
* @return The component created from the incoming data. | |
*/ | |
function _createComponent(data) { | |
var title = data.title, | |
imageUrl = data.imageUrl, | |
description = data.description, | |
$component = $('<div />'); | |
$component | |
.append( | |
$('<p />').text(title) | |
) | |
.append( | |
$('<img />').attr('src', imageUrl) | |
) | |
.append( | |
$('<p />').text(description) | |
); | |
return $component; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment