My query differs from others with a similar title. I am attempting to transform a function into a factory in order to share data across controllers, but unlike my other factories, this one is not functioning as expected.
I have successfully implemented other factories within the same services.js file. When inspecting them in the console, they both display the same data, leading me to believe there may be an issue with the return
statement.
This code operates correctly when placed directly in the controller:
Controller
getProgrammeData = function(activeProgrammeID) {
var programmeData = [];
var GetProgrammeData = Parse.Object.extend("Programme");
var query = new Parse.Query(GetProgrammeData);
query.equalTo("objectId", activeProgrammeID);
query.first({
success: function(object) {
$scope.programmes = {
programmeTitle : object.get('programmeTitle'),
id : object.id,
exerciseData : object.get('exerciseData')
};
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
However, this does not work as intended:
services.js
.factory('parseQueryFactory', function($http) {
return {
getProgrammeData:function(activeProgrammeID) {
var programmeData = [];
var programmes = {};
var GetProgrammeData = Parse.Object.extend("Programme");
var query = new Parse.Query(GetProgrammeData);
query.equalTo("objectId", activeProgrammeID);
query.first({
success: function(object) {
programmes = {
programmeTitle : object.get('programmeTitle'),
id : object.id,
exerciseData : object.get('exerciseData')
};
console.log(programmes)
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
})
return programmes
}
}
});
controllers.js
$scope.programmes = parseQueryFactory.getProgrammeData(activeProgrammeID);
Additionally, I would like to explore caching these results. How can I accomplish this?