I han surgido con esta solución ordenada. Iniciar por primera vez mediante la creación de un espacio de nombres para sus carpetas en el ámbito global:
// Binder for PJAX init functions
$.fn.bindPJAX = {}; // Create namespace
function bindPJAX(functionName) {
// Check function existence, then call it
return $().bindPJAX[functionName] && $().bindPJAX[functionName]();
}
se unen entonces una devolución de llamada a PJAX hace clic:
$(document).ready(function(){
if ($.support.pjax) {
$('a[data-pjax]').on('click', function(event) {
var container = '#main';
var emptyRoute = 'feed'; // The function that will be called on domain's root
// Store current href without domain
var link = event.currentTarget.href.replace(/^.*\/\/[^\/]+\//, '');
var target = link === "" ? emptyRoute : link;
// Bind href-specific asynchronous initialization
$(document).on('ready pjax:success', container, function() {
bindPJAX(target); // Call initializers
$(document).off('ready pjax:success', container); // Unbind initialization
});
// PJAX-load the new content
$.pjax.click(event, {container: $(container)});
})
}
});
Cuando esto está configurado, se puede proceder a extender el $.fn.bindPJAX
Objeto para agregar funciones que coincidan con su nombre de ruta. Por ejemplo, esto, en el ámbito global, se llamará cuando se accede a través de http://www.yourdomain.com/admin
PJAX:
$.extend($.fn.bindPJAX, {
admin: function(){
// Initialization script for /admin route
}
});
También debe considerar un repliegue SECO viable cuando PJAX falla o no se admite, como el lanzamiento de la función init correcta en primero pageload comprobando window.location
en javascript, o tal vez hardcoding en vistas de su aplicación.
Gracias por esto! – neersighted
Loool. me encanta :) – Zeck
¿Qué puedo hacer si el código de inicialización es diferente para cada página? – shreyj