This is a short write up on how to perform ReplaceConditionalWithPolymorphism for directives in angular
Recently I had to add some functionality to a menu-bar that handles navigation between different screens. It is implemented as a directive. The problem was the same directive was instantiated by several controllers, and from some controllers it needs an instance of something, lets call it Book, and from one other controller it needs an instance something else that we’ll call Page.
I’m going to use the syntax of javascript classes to explain the problem, as it is rather clear and not specific to a framework. At the end we’ll look at the angular version.
class MenuBar {
constructor(book, page) {
// either of book or page are undefined
}
goToView(viewItem) {
if (onBookView()) { // i.e. page == undefined
handleRedirectionFromBookViewTo(viewItem)
} else { // on page view
handleRedirectionFromPageViewTo(viewItem)
}
}
}
It is awkward and subject to error to allow either book or page to be null/undefined as it is difficult for another developer to know what is a valid way of instantiating it. Can both be undefined? If I define page, to I have to provide book? As far as testing goes, we have little confidence that it is instantiated with the right arguments in every view, so we're kind of forced to test all functionality in every view to be sure we don’t get a runtime failure. A combinatorial problem.
In addition much of the cyclomatic complexity of this class is unnecessary. Basically one set of branches are used when we have a book and a completely different one when we have a page. This is a clear cut for polymorphism :
class MenuBarForBook {
constructor(book) {...}
goToView(viewItem) {
handleRedirectionFromBookViewTo(viewItem)
}
}
class MenuBarForPage {
constructor(page) {...}
goToView(viewItem) {
handleRedirectionFromPageViewTo(viewItem)
}
}
With this design it is unlikely that a constructor won't be called with the right arguments. The combinatorial problem is solved and we've lowered the testing burden.
So how can we do this in angular. Easy; create two different directives that expose the same functions and use the same template.
angular.module('menuBar', [])
.directive('menuBarBook', [
function () {
return {
templateUrl: 'src/menu/MenuBar.html',
scope: {
book: ‘=‘ // constructor argument
},
link: function ($scope) {
$scope.goToView = function () { // function goToView()
handleRedirectionFromBookViewTo(viewItem)
}
}
}
}])
.directive('menuBarPage', [
function () {
return {
templateUrl: 'src/menu/MenuBar.html',
scope: {
page: '=',
},
link: function($scope) {
$scope.goToView = function () {
handleRedirectionFromPageViewTo(viewItem)
}
}
}
}])
}
}
}
}])
.directive('menuBarPage', [
function () {
return {
templateUrl: 'src/menu/MenuBar.html',
scope: {
page: '=',
},
link: function($scope) {
$scope.goToView = function () {
handleRedirectionFromPageViewTo(viewItem)
}
}
}
}])
Conclusion
Polymorphism is a powerful tool both for making the code more usable to other developers by making it more explicit how to use a piece of code. Also, removing if-statements not only makes the code simpler but also makes the tests simpler. I just discovered this could be done with directives without too much overhead. It is worth naming directive inheritance here. However to my understanding it solves a different problem, namely sharing code.
RépondreSupprimerThanks for providing a good stuff.
Angular JS Online training
Angular JS training in hyderabad
Nice post.
RépondreSupprimerAngular JS training
APP V training
Application packagining training
Blockchain training
C training
Data power training
Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
RépondreSupprimerangular js online training
best angular js online training
top angular js online training
malatya eskort
RépondreSupprimerağrı eskort
adana eskort
edirne eskort
zonguldak eskort
rize eskort
balıkesir eskort
karabük eskort
kırşehir eskort
konak eskort
yalova eskort
RépondreSupprimerantep eskort
sivas eskort
iskenderun eskort
van eskort
antalya eskort
düzce masöz
manisa masöz
perde modelleri
RépondreSupprimersms onay
mobil ödeme bozdurma
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
trafik sigortası
dedektör
web sitesi kurma
Aşk kitapları
It s a very useful page. Thank you. 16913ec96b9cdabef1198d04b0f42eb3
RépondreSupprimergoynuk
sinanpasa
bafra
bayat
aydin
sultanhisar
duzici
beylikduzu
kuluncak
Congratulations on your article, it was very helpful and successful. 5880f1f38a75eb4b58ab63a87a1844d7
RépondreSupprimernumara onay
website kurma
sms onay
Thank you for your explanation, very good content. 034d66dc7c7cb0625776d74708618132
RépondreSupprimeraltın dedektörü
Good text Write good content success. Thank you
RépondreSupprimerpoker siteleri
tipobet
slot siteleri
betpark
betmatik
kibris bahis siteleri
mobil ödeme bahis
bonus veren siteler
hatay
RépondreSupprimerkars
mardin
samsun
urfa
HUCM
edirne
RépondreSupprimerantakya
aydın
muş
afyon
W1836
https://saglamproxy.com
RépondreSupprimermetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
4KY1Q
Thanks for shareing comments
RépondreSupprimerThanks for sharing all this information here on this page.
RépondreSupprimerI truly enjoy looking through on this web site , it holds superb content .
RépondreSupprimerشركة عزل خزانات بخميس مشيط 0l8LmJbTyp
RépondreSupprimer