jeudi 26 mars 2015

ng-click to call a function in services for logging ratings onto firebase


I am creating a website which logs user ratings to firebase, but I cannot accomplish the calling of a services.js function which is meant to log the user's rating on click of one of stars 1-5. Is there something wrong with my code, or something I am missing? Here is a snippet of the services.js, controllers.js, as well as the ratings div from the html. Any help is much appreciated!


From the services.js



angular.module('dev.services', [])
.factory('ratingsService', function() {
return {
//removed return envelope
//var obj = {};
/**
* @todo Add a check to determine whether or not the specified user has already rated the specified item.
*
* @param {String} userId
* @param {String} itemType
* @param {String} itemId
* @param {Number} rating
*/
rate: function(userId, itemType, itemId, rating) {
// @todo Construct a new promise that resolves when both Firebase operations have been performed.
$firebase( new Firebase(dvUrl + '/users/' + userId + '/ratings/' + itemType + '/' + itemId) ).$set(rating);
$firebase( new Firebase(dvUrl + '/' + itemType + '/' + itemId + '/ratings/' + userId) ).$set(rating);
console.log("SUCCESS!");
},
/**
* @param {Object} ratings
* @returns {Number} avgRating
*/
getAvgRating: function(ratings) {
var ratingsTotal = 0;
var ratingsCount = this.getRatingsCount(ratings);

if(ratingsCount === 0) {
return 0;
} else {
for(var rating in ratings) {
if( ratings.hasOwnProperty(rating) ) {
ratingsTotal += parseFloat(ratings[rating]);
}
}

return (ratingsTotal / ratingsCount).toFixed(1);
}
},
/**
* @param {Object} ratings
* @returns {Number} ratingsCount
*/
getRatingsCount: function(ratings) {
return ratings == null
? 0
: Object.keys(ratings).length;
}
//removed return envelope
//return obj;
}
})


From the controllers.js



angular.module('DeViine.controllers', [])
.controller('rateCtrl', ['$scope', 'ratingsService', 'dvUrl', function($scope, ratingsService) {
$scope.rate = function (item, rating) {
ratingsService.rate(userId, item.type, item.id, rating);
}
}]);


From the HTML



<div class="ratings">
<div ng-click="rate(itemId, 1)" class="rate rate1" title="1"></div>
<div ng-click="rate(itemId, 2)" class="rate rate2" title="2"></div>
<div ng-click="rate(itemId, 3)" class="rate rate3" title="3"></div>
<div ng-click="rate(itemId, 4)" class="rate rate4" title="4"></div>
<div ng-click="rate(itemId, 5)" class="rate rate5" title="5"></div>
</div>




Aucun commentaire:

Enregistrer un commentaire