pauloabmiranda
2/24/2014 - 1:15 PM

Function to init all controllers inside html element

Function to init all controllers inside html element

var PB = PB || {};
PB.viewsToRoute = [];

PB.initControls = function($elem){
    var data, viewModel;

    $elem.find('[data-control]').each(function (index, elem) {
        data = $(elem).data();

        if(PB[data.control]){
            viewModel = new PB[data.control]( { el:elem, data:data } );
            
            //if data-route exists and is true adds to array of controllers to be called on route
            if(data.route) 
                PB.viewsToRoute.push( viewModel );
                
            viewModel = null;
        }else{
            console.log('data.control doesnt exist: PB.', data.control);
        }
    });
};