Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am sending data to a Class at parse.com, I would like to run this function and update the $scope without having to reload the view.

To create a Programme running the function below works fine, however it sometimes does not update the view following creating a new programme and it requires a page refresh (when the whole function is called as seen at the bottom - getProgrammes();

getProgrammes = function() {
$ionicLoading.show();

var programmesArray = [];
var QueryProgramme = Parse.Object.extend("Programme");
var query = new Parse.Query(QueryProgramme);

query.equalTo("userId", userId);
query.find({
success: function(results) {
  for(var i=0; i < results.length; i++) {
    var object = results[i];

    var programmeData = { title : object.get('programmeTitle'), 
                          id : object.id,
                          exercises : object.get('exerciseData')
                        };

    programmesArray.push(programmeData);
  }

  $scope.programmes = programmesArray;
  $scope.$apply();
  $ionicLoading.hide();
   },
   error: function(error) {
   alert('You do not have any Programmes - please create one');
   }
 })
};

getProgrammes();

I think I may be hacking this by using $scope.apply() - therefore making it unreliable. Assistance on the correct function to use in order to automatically update $scope would be great.

share|improve this question
    
You shouldn't need to call $scope.$apply(). You should just be able to set the property on $scope. Perhaps provide a js fiddle demonstrating the problem – Wayne Ellery Jan 18 at 0:15
up vote 3 down vote accepted

I've been using a library found here:

https://github.com/brandid/parse-angular-patch

Essentially I just reference the parse-angular.js file, and then include the dependency in my app, e.g.:

angular.module('app', [
  'parse-angular',
]);

Then I just use Parse queries anytime I like and they participate correctly with $scope and $apply. The library just does some simple wrapping on the Parse methods to make the async calls obey the rules of angular.

share|improve this answer
    
I've looked at this previously and found I couldn't follow the documentation on it (my lack of experience) however I will look again. – Taylorsuk Jan 18 at 8:16
    
The documentation is overly complicated, that's why I explained it with two simple steps, just add the JS file and add the references to your app declaration, nothing else is needed for basic functionality – Timothy Walters Jan 18 at 8:30
    
I'm being a real dumbass or something, where is the JS file I need? Reference to components/ in index.js but is that some bower magic? – Taylorsuk Jan 18 at 8:47
    
Nope still can't figure out what I should use as the JS file? – Taylorsuk Jan 18 at 12:42
1  
Sorry, the simple version I use is found here: github.com/brandid/parse-angular-patch – Timothy Walters Jan 21 at 1:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.