I have wrote some service in AngularJS
that use defer, can you check my code and tell me if I getting it right?
Service (I dont really like name: WatchableTeaser
, have you got another?):
.factory('Teaser', function ($resource, DateUtils) {
return $resource('api/teasers/:id', {}, {
'query': {method: 'GET', isArray: true},
'getByLangKey': {method: 'GET', url: 'api/teasers/langKey/:langKey', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
data = angular.fromJson(data);
return data;
}
},
'update': {method: 'PUT'}
});
})
.service('WatchableTeaser', function ($q, $cookies, $filter, Language, Teaser) {
var lastSeenTeaserVideo = Number($cookies.get('last_seen_teaser_video'));
var getTeasersByCurrentLang = function () {
var deferred = $q.defer();
Language.getCurrent().then(function (current) {
Teaser.getByLangKey({langKey: current}, function (result) {
deferred.resolve(result);
})
});
return deferred.promise;
};
var teasersPromise = getTeasersByCurrentLang();
this.saveLastSeenTeaser = function (teaser) {
var deferred = $q.defer();
if (angular.isNumber(teaser)) {
deferred.resolve(teaser);
} else if (angular.isObject(teaser)) {
teasersPromise.then(function (teasers) {
deferred.resolve(teasers.indexOf(teaser));
});
} else {
deferred.resolve(-1);
}
deferred.promise.then(function(indexOfTeaser) {
var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + 30);
$cookies.put("last_seen_teaser_video", indexOfTeaser, {
expires: expireDate
});
lastSeenTeaserVideo = indexOfTeaser;
});
};
if (isNaN(lastSeenTeaserVideo)) {
this.saveLastSeenTeaser();
}
this.getTeaserToWatch = function () {
var deferred = $q.defer();
var self = this;
teasersPromise.then(function (teasers) {
var teaserToWatchIndex = lastSeenTeaserVideo + 1;
if (teaserToWatchIndex < 0 || teaserToWatchIndex >= teasers.length) {
self.saveLastSeenTeaser();
teaserToWatchIndex = 0;
}
deferred.resolve(teasers[teaserToWatchIndex]);
});
return deferred.promise;
};
});
Controller:
.controller('MainController', function ($scope, $state, $cookies, Category, WatchableTeaser) {
$scope.categories = [];
$scope.categoryName = $state.params.categoryName;
$scope.teaser = {};
$scope.loadAll = function () {
Category.query(function (result) {
$scope.categories = result;
});
WatchableTeaser.getTeaserToWatch().then(function (teaser) {
$scope.teaser = teaser;
});
};
$scope.loadAll();
$scope.$on('$stateChangeSuccess', function () {
$scope.categoryName = $state.params.categoryName;
});
$scope.safeApply = function (fn) {
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if (fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
var player = undefined;
videojs("teaser_video").ready(function () {
player = this;
player.on("ended", function () {
WatchableTeaser.saveLastSeenTeaser($scope.teaser);
WatchableTeaser.getTeaserToWatch().then(function (teaser) {
$scope.safeApply(function () {
$scope.thumbnailUrl = teaser.thumbnailUrl;
})
});
});
});
$scope.exitFullscreen = function () {
if (angular.isDefined(player)) {
player.exitFullscreen();
}
};
$scope.watchAgain = function () {
if (angular.isDefined(player)) {
player.currentTime(0);
player.play();
}
};
$scope.showRecomendedVideo = function () {
if (angular.isDefined(player)) {
WatchableTeaser.getTeaserToWatch().then(function (teaser) {
$scope.teaser = teaser;
player.src($scope.teaser.videoUrl);
$scope.watchAgain();
});
}
};
});
The question is about implement and usage WatchableTeaser
. Thank for any comment.