Jump to content

loading file from assets folder in js


Manol
 Share

Recommended Posts

How can I access the assets folder from a javascript file?

from:   /site/templates/js/myjs.js

to:      /assets/myfile.txt

Best regards.

You should be able to access it with /site/assets/myfile.txt

  • Like 1
Link to comment
Share on other sites


this is my .php under /templates


<?php include("./includes/head.inc"); ?>

    
<!-- Content start -->
<div data-role="content">
    
    <div id='proximity_list' class='css3-blink '>Buscando lugares de interes cercanos...</div>

    <div ng-controller='getLitLatLng'>
        <ul data-role='listview'>
            <li ng-repeat='lit in lits | orderBy:predicate:reverse'>
                <a href='{{ lit.url }}' >
                    <h2>{{ lit.title }}</h2>
                    <p>{{ lit.distance }} metros</p>
                </a> 
            </li>
        </ul>
    </div>            

<!-- ><div id='mymap' ></div> -->

</div> <!-- /content end -->    

<?php  include("./includes/foot.inc"); ?>

this my js under /templates/js

(I use angularjs, maybe that is the reason this is not working)

function getLitLatLng($scope, $http) {
	
	// check if user has geolocation
	if(navigator.geolocation) {
			
		// Posicion del dispositivo movil
		navigator.geolocation.getCurrentPosition(function(position) {
		 
				// get user position
				x0 =  position.coords.latitude;
				y0 =  position.coords.longitude;
					
				//create and populate list of lits	
				listalits($scope, $http);

			});//navigator
		}//if	
	
}

function listalits($scope, $http){
	
	/** gets lits information	 */
  $http.get('http://mydomain.com/site/assets/data/list-details.json')

		/** success file readed */
		.success(function(data) {
	
				// convert to object
		    $scope.lits = eval(data);

				// calcula distancias de usuario a lit
				for(var i in $scope.lits){
					metros = (Math.sqrt(Math.pow((x0-$scope.lits[i].lat),2) + Math.pow((y0-$scope.lits[i].lng),2)) * 100000 );
					$scope.lits[i].distance = metros;
				}
				
		/** file not readable */
		}).error(function(data, status) { alert(" json no leido desde el servidor"); });

	//order by distance
	$scope.predicate = 'distance';
	
}



			
		
Link to comment
Share on other sites

'/site/assets/list-details.json' doesn't get the results but 'http://mysite.com/site/assets/data/list-details.json' does.

Thank you anyway.

Of course it doesn't get the result because the first url is wrong... that's what Ryan mentioned, you have a /data/ folder inside assets? Which is missing in the first url

/site/assets/list-details.json

http://mysite.com/site/assets/data/list-details.json

So it should be then:

/site/assets/data/list-details.json

which will work fine.

To go further if you want to make it always have the correct url even if you move PW folder to a subfolder you would use the $config->urls->assets in front of data folder.

$filepath = $config->urls->assets . "data/list-details.json";

If you want to have it in your JScript as a variable you could use a json object to have access to it in js.

Somewhere in your pages html <head> before your angular js script:

<?php
$jsconfig = array('assets' => $config->urls->assets);
?>
<script>
    var config = <?php echo json_encode($jsconfig);?>;
</script>

Then use it in your angularjs like this:

$http.get( config.assets + 'data/list-details.json').success(....

PW is using this same technique in the admin.

Link to comment
Share on other sites

Thank you very much Soma, this is the bit I was missing:

$filepath = $config->urls->assets . "data/list-details.json";

Really appreciate your help, it's a pleasure to learn from you and many others in this forum.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...