Replace static content with JSON Request


#1

I have this code below that works perfectly and I want to replace the var events with a JSON request, I’ve tried tons of examples online and can’t figure it out. The remote example JSON url is http://appserver.falconinet.com/events.lasso and it spits out JSON and has the allow origin set to all.

    .factory('Events', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var events = [{
    id: 0,
    title: 'Swing Dance Party',
    subtitle: 'Lets Get Dancing!',
    when: 'Thursday, Feb 19, 2015 (6:30-9PM)',
    picture: 'http://goldsea.com/Text/images/8198.jpg',
    desc: 'Dance, dance, dance and enjoy mixed drinks, wine, or 40 beers on tap. Krista Mccart & Steve Davis will be doing a short 30 minute class for first time beginners at 6:30 and the dance starts at 7:00. The dance and lesson are free!!!'
  }, {
    id: 1,
    title: 'St. Patricks Day Party',
    subtitle: 'with Special Guest The Menders',
    when: 'Saturday, March 14th (9PM)',
    picture: 'img/menders.png',
    desc: 'Based out of Gastonia, NC, The Menders have been blending influences such as the Beatles, Jack White, The Doors, and Ryan Adams into a folk-laced garage rock sound. Since 2011, they\'ve been honing their craft around NC at venues such as Double Door Inn, The Visulite, The Milestone, Tremont Music Hall, and Snug Harbor. With an upcoming debut self-titled album, lyrics dealing with the complexities of life and death, 4 part harmonies, and energetic live performances, The Menders seek to offer their fans and listeners a music experience that is sure to leave a lasting impression.'
  }];

  return {
    all: function() {
      return events;
    },
    remove: function(event) {
      events.splice(events.indexOf(event), 1);
    },
    get: function(eventId) {
      for (var i = 0; i < events.length; i++) {
        if (events[i].id === parseInt(eventId)) {
          return events[i];
        }
      }
      return null;
    }
  }
})

#2

I’ve tried this, but I get an error…

.factory('Events', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var events = [];
    
    	return {
		getEvents: function(){
			return $http.get("http://appserver.falconinet.com/events.lasso").then(function(response){
				events = response;
				return events;
			});
		},
		getEvents: function(index){
			return events[index];
		}
	}

ERROR: TypeError: Events.all is not a function


#3
.factory('Events', ['$rootScope','$http', '$q' ,
    function($rootScope,$http, $q) {
     return {
        getData: function() {
            var defer = $q.defer();
            $http.get('http://appserver.falconinet.com/events.lasso', { cache: 'true'})
            .success(function(data) {
              defer.resolve(data);
            })
            .error(function(data) {
            //error
            }
            });

           return defer.promise;
             }

}])

This is what i used…! even i got it from somewhere…!! you can try !! i fetched data from php file though…


#4

tried that doesnt work at all…


#5

in what way you called the getData function in Controller ? just to be sure… check whether the service was added to conroller


#6

the code you tried before is like this…(your secnd code with http get)

.factory('Events', function() {

It should be

.factory('Events', function($http) {

since you using $http.get