So far, I have always found valuable help by studying existing answers on stackoverflow, but now I've hit a roadblock.
I'm currently working on an app that utilizes a directive to generate calendar month boxes.
<app2directive class="column_50" year="2016" month="November"></app2directive>
The directive code isolates the scope and uses a templateUrl file to display the calendar month.
App.directive('app2directive',function( YEARS, MONTHS, DAYS){
return {
restrict:"ACE",
scope:{},
replace:true,
templateUrl:"templates/cal_directive3.html",
controller: function ( $scope, $attrs, $injector, $log, YEARS, MONTHS, DAYS) { var factoryName = "datumFactory" ;
var factoryTonnen = "jsonsrc" ;
var factoryInstance = $injector.get( factoryName ) ;
var tonnenInstance = $injector.get( factoryTonnen ) ;
var wtf = $scope.jsondata.get().then( function( promise){
console.log( "jsondata", promise ) ;
//$scope.tonnen = promise ;
}, function( error){
console.log( "error", error ) ;
}) ;
});
Currently, I am using an $injector to inject a factory that performs a $http request to retrieve data from a JSON file containing information like holidays or other static details specific to the chosen year and month(s).
App.factory( 'jsonsrc', function( $http, $q ){
return {
get: function(){
var deferred = $q.defer() ;
$http.get( 'termine_2016.json' )
.success(function(data, status, headers, config) {
deferred.resolve( data ) ;
}).error(function(data, status, headers, config) {
console.log( "[jsonsrc] request did not work!" );
deferred.reject( data ) ;
}) ;
return deferred.promise ;
}
}
});
This results in the $http call being made 12 times for a full year page load.
My goal is to refactor the code so that I can ideally load the JSON data into the main controller and have the directive inherit from the parent scope.
Since the call returns a promise, the directive needs to wait for it to resolve before proceeding, but I am currently unsure how to approach this. Any guidance would be greatly appreciated!