I'm trying to wrap Snap.svg as a service of my application. The code below works, but is this the right way to go about it?
var svgService = angular.module("svgService", []);
svgService.factory("svgService", [
function () {
if (Snap) {
return Snap;
}
}
]);
AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.
Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.
Your if
check would throw an ReferenceError: Snap is not defined
error anyway if the Snap
doesn't exist.
You could replace the condition statement like this to be safe:
if (typeof Snap !== 'undefined') {
or if the Snap
is in window
, just reference it directly:
if (window.Snap) {
and even better, at least when it come to unit testing, using a built-in $window
service as it allow you to mock it out.
svgService.factory("svgService", function ($window) {
// the if check is unnecessary since an undefined
// is returned anyway when the Snap doesn't exist.
return $window.Snap;
});
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With