JavaScript how to make synchronous call to json data from angularjs make synchronous call in javascript,make synchronous call asynchronous c#,

I am calling some data from a JSON file in AngularJS but due to the asynchronous call the code is moving to other step before receiving the data so that is causing an error. I used $http.get

$http.get('job.json').success(function (response) {
  $scope.big = response;      
});

Can you suggest some synchronous method to call json data which is

{
  "days": [{
    "dayname": "Sun,23 Aug 2015",
    "date": "2015-08-23",
    "hours": "hoursArray(array24)"
  }, {
    "dayname": "Mon,24 Aug 2015",
    "date": "2015-08-24",
     "hours": "hoursArray(array24)"
  }, {
    "dayname": "Tue,25 Aug 2015",
    "date": "2015-08-25",
    "hours":"hoursArray(array24)"
  }, {
    "dayname": "Wed,26 Aug 2015",
    "date": "2015-08-26",
    "hours": "hoursArray(array24)"
  }]
}

this is the jquery file i am using

(function($) {
	$.fn.schedule = function(options) {
		var methods = {
			init : function(ele, opt) {
				//methods.currentdate = methods.now.getFullYear() + "Engine Change" + methods.now.getMonth() + "Engine Change" + methods.now.getDate();
				methods.currentdate = methods.now.getFullYear() + "-" + methods.now.getMonth() + "-" + methods.now.getDate();
				// $("#scheduleAllDays > *").each(function(){
				// var item = $(this);
				// $("#scheduleAllDays").width($("#scheduleAllDays").width()+item.width());
				// });
				// $("#scheduleAllDays").width($("#scheduleAllDays").width());
				ele.find("[data-row]").each(function() {
					var drow = $(this), drowset = $("[data-row='" + drow.data("row") + "']");
					
					var maxheight = methods.elesMaxHeight(drowset);
					drowset.height(maxheight);
				});
				methods.allocateDurations(ele);
				$("#scheduleContentInner", ele).css("min-height", $(".schedule-drag-wrap", ele).innerHeight());
			},
			elesMaxHeight : function(ele) {
				var heights = $(ele).map(function() {
					return $(this).height();
				}).get();
				return Math.max.apply(null, heights);
			},
			allocateDurations : function(ele) {
				methods.flightdata = {
					routes : {}
				};
				ele.find("[data-flight-row]").each(function(i, ival) {
					var flight = $(this);
					methods.flightdata.routes["row" + i] = [];
					flight.find("[data-flight-record]").each(function() {
						var currentFlight = $(this), flightrecord = methods.makeStringToObject(currentFlight.data("flight-record"));
						flightrecord.element = currentFlight;
						methods.flightdata.routes["row" + i].push(flightrecord);
					});
				});
				methods.positionSet(ele);
			},
			positionSet : function(ele) {
				var dayelement = $("#scheduleAllDays > *", ele);
				
				var totaldaywidth = $("#scheduleAllDays").width() + 30;
				
				var totaldays = dayelement.size();
				
				var totalSeconds = (((totaldays * 24) * 60) * 60);
				var perSecondsWidth = Number(totaldaywidth / totalSeconds);
				var divider = $(".schedule-h-divider");
				dayelement.each(function(i, ival) {
					var dayele = $(this), dividerele = divider.eq(i);
					dividerele.css({
						top : $("#scheduleAllDays").height(),
						left : dayele.offset().left - 104
					});
				});
				for (var i in methods.flightdata.routes) {
					var iobj = methods.flightdata.routes[i];
					for (var j in iobj) {
						var jobj = iobj[j];
						var duration = jobj.duration, width = Number(methods.hmtosec(duration, ".") * perSecondsWidth);
						var parent = jobj.element.parent();
						jobj.element.css({
							// position : "relative",
							width : width + "px",
							overflow : "hidden",
							"white-space" : "nowrap"
						}).parent().css({
							// width : width+"px",
							// overflow : "hidden"
							// position:"absolute",
							left : (j==0)?0:parent.prev().position().left+parent.prev().width()
						});
					}
				}
				methods.dragInit(ele);
			},
			setCurrentTimeMarker : function(ele) {
				var marker = $(".schedule-current-time-marker");
				var markerpills = $(".schedule-time-marker-pills");
				var dayelement = $("#scheduleAllDays > *", ele);
				var totaldaywidth = $("#scheduleAllDays").width() + 30;
				var totaldays = dayelement.size();
				var totalSeconds = (((totaldays * 24) * 60) * 60);
				var perSecondsWidth = Number(totaldaywidth / totalSeconds);
				var currentdate = new Date();
				var format = currentdate.getFullYear() + "Engine Change" + currentdate.getMonth() + "Engine Change" + currentdate.getDate();
				var currentdateele = $("#scheduleAllDays").find("[data-date='" + format + "']");
				var days = (currentdateele.index()), seconds = ((days * 24) * 60) * 60;
				seconds = seconds + methods.hmtosec(currentdate.getHours() + "." + currentdate.getMinutes(), ".");
				marker.stop().animate({
					top : $("#scheduleAllDays",ele).height()-53,
					left : (seconds * perSecondsWidth) - (marker.width() / 2)
				}, 1000, "swing");
				markerpills.html(currentdate.getHours() + ":" + currentdate.getMinutes());
				methods.markermove = setInterval(function() {
					currentdate = new Date();
					marker.css({
						left : marker.position().left + perSecondsWidth
					}, "fast", "swing");
					markerpills.html(methods.makezerodigit(currentdate.getHours()) + " : " + methods.makezerodigit(currentdate.getMinutes()));
					// methods.schedulemove(ele,perSecondsWidth);
				}, 1000);
			},
			schedulemove : function(ele,seconds) {
				var dragwrap = ele.find(".schedule-drag-wrap");
				var routewidth = $(".schedule-route:eq(0)").width() + $(".schedule-route:eq(1)").width();
				var maxleft = -(dragwrap.width() - ($(window).width() - routewidth));
				if (Math.abs(dragwrap.position().left) < Math.abs(maxleft)) {
					dragwrap.css({
						left : (dragwrap.position().left - (dragwrap.width)) + "px"
					});
				}
			},
			makezerodigit : function(digit) {
				return (String(digit).match(/^[0-9]$/)) ? "0" + digit : digit;
			},
			dragInit : function(ele) {
				var currentdaycol = $("[data-date='" + methods.currentdate + "']");
				ele.find(".schedule-drag-wrap").css({
					left : -(currentdaycol.position().left - 50) + "px"
				}).animate({
					left : -currentdaycol.position().left - 0 + "px"
				}, 1000, "swing", function() {
					methods.drag(ele);
					methods.setCurrentTimeMarker(ele);
				});
			},
			drag : function(ele) {
				methods.move = null;
				$(".schedule-drag-wrap", ele).on("mousedown", function(e) {
					var dragele = $(this), position = dragele.position();
					methods.move = {
						x : e.pageX,
						y : e.pageY,
						left : position.left
					};
				}).on("mouseup mouseleave", function(e) {
					var dragele = $(this);
					if (methods.move) {
						methods.move = null;
						dragele.removeClass("userselect-none cursor-move");
					}
				}).on("mousemove", function(e) {
					var dragele = $(this), position = dragele.position(), movedx, drag = true;
					if (methods.move) {
						methods.curmove = {
							x : e.pageX,
							y : e.pageY
						};
						var routewidth = $(".schedule-route:eq(0)").width() + $(".schedule-route:eq(1)").width();
						var maxleft = -(dragele.width() - ($(window).width() - routewidth));
						var xcondition = (methods.move.x > methods.curmove.x);
						dragele.addClass("userselect-none cursor-move");
						if (position.left <= maxleft && xcondition) {
							drag = false;
							dragele.css({
								left : maxleft
							});
						}
						if (position.left > -10 && !xcondition) {
							drag = false;
							dragele.css({
								left : 0
							});
						}
						if (drag) {
							//if direction right to left
							movedx = methods.move.left + (methods.curmove.x - methods.move.x);
							dragele.css({
								left : movedx
							});
						}
					}
				});
			},
			now : new Date(),
			currentdate : "",
			hmtosec : function(hours, identy) {
				var s = (hours.match(/\./)) ? hours.split(identy) : [hours, 0], h = s[0], m = s[1];
				h = (Number(h)) ? (h * 60) * 60 : 0;
				m = (m == 0) ? 0 : m * 60;
			
				return Number(h + m);
				
			},
			makeStringToObject : function(string) {
				var loc_string = String(string).split("|");
				var output = {};
				for (var i in loc_string) {
					var keyvalue = loc_string[i].split("~");
					output[keyvalue[0]] = keyvalue[1];
				}
				return output;
			}
		};
		return this.each(function() {
			methods.init($(this), $.extend({}, $.fn.schedule.setting, options));
		});
	};
	$.fn.schedule.setting = {};
})(jQuery);
Answer:1

In service: var deferred = $q.defer(); $http.get('job.json') .success(function(response) { defer.resolve(response); }).error(function(error){ console.log(error); }) return deferred.promise;

In controller:

var p=<serviceCall>
p.then(function(s){
$scope.ans=s;
})

The $q is used for getting synchronous response.for more details:https://docs.angularjs.org/api/ng/service/$q

Answer:2

You can't do a synchronous HTTP request, since the reponse (in your case it is the JSON file) can not be loaded instantly. However, $http.get returns a promise which is resolved when the request completes. You should do everything you want to do as soon as the JSON file has loaded inside the then-block of the promise.

$http.get('job.json').then(function (response) {
  $scope.big = response;
  // Do anything else you need to after JSON has been loaded.     
});
Answer:3

Is it possible to take content of one file and include it into handlebars template using only power of handlebars? I just want to instert inline css into html. <html> <head> <...

Is it possible to take content of one file and include it into handlebars template using only power of handlebars? I just want to instert inline css into html. <html> <head> <...

  1. include file-content as part of markdown
  2. include file content in html
  3. include content file in nuget package
  4. markdown include file content
  5. php include file content
  6. jinja include file content
  7. javascript include file content
  8. jinja2 include file content
  9. doxygen include file content
  10. typescript include file content
  11. c++ include file content as string
  12. rst include file content
  13. thymeleaf include file content
  14. xslt include file content
  15. jekyll include file content
  16. helm include file content
  17. yaml include file content
  18. asciidoc include file content
  19. jsp include file content
  20. confluence include file content

I'm new to working with React Native and I am wondering what the best way is to create a conditional component that will show when the user data has the field, and hides when it does not. Using ...

I'm new to working with React Native and I am wondering what the best way is to create a conditional component that will show when the user data has the field, and hides when it does not. Using ...

  1. conditional components react
  2. conditional rendering components react

I have these two scripts, one sorts divs by a value in a div class (price) and the other shows or hides divs based on class name with buttons. (box a, box b etc, inside a "parent" class.) The problem ...

I have these two scripts, one sorts divs by a value in a div class (price) and the other shows or hides divs based on class name with buttons. (box a, box b etc, inside a "parent" class.) The problem ...

  1. hide show divs
  2. hide show divs jquery
  3. show hide divs javascript
  4. show hide divs on click
  5. show hide divs react
  6. show/hide div angularjs
  7. hide show onclick jquery
  8. hide show onclick javascript
  9. hide show onclick
  10. hide show multiple divs jquery
  11. hide show multiple divs javascript
  12. hide show multiple divs
  13. hide and show divs html
  14. hide and show divs bootstrap
  15. css hide show divs
  16. show hide onclick css
  17. onclick hide show div
  18. onclick hide show div jquery
  19. show hide multiple divs css
  20. css javascript hide show divs

For some reason I have to do this for(let i=0;i<=6;i++){ price.push({ min_price: `this.state.special_${i}_min`, max_price: `this.state.special_${i}_max` }); } But it's not ...

For some reason I have to do this for(let i=0;i<=6;i++){ price.push({ min_price: `this.state.special_${i}_min`, max_price: `this.state.special_${i}_max` }); } But it's not ...