In this lesson, we'll learn how we can create our own globally available helper methods & properties in AdonisJS. We'll install currency.js, wrap it in a brief custom class, and make our custom method available throughout our application by exporting them, importing them, and registering them as EdgeJS Globals.
🥁 Subscribe To Stay Notified For New Lessons
📺 View on YouTube
📚 Chapters:
- Intro
- Creating Our Helper Service
- Usage In App Directory (Controllers, Exceptions, Models, etc)
- Usage in EdgeJS Views
- Outro
📜 Transcript:
one of the things that I see frequently
Asked is how to create your own globals
so that you have methods available
everywhere throughout Adonis and
adonisjs does this themselves via their
helper methods So today we're going to
take a look at how you can go about this
and it's actually a lot simpler than you
might imagine so as I mentioned
adonisjaz does have their own helpers
and these are available just by
importing them from their namespace in
addition to that they're also made
available with an edge as well so you
can see the same camel case method is
available just like so so let's dive in
and create our own for our example let's
say that we're working with a currency
heavy application so for that I'd like
to use currency.js this accepts a
plethora of different types of inputs
for currency and it outputs them as easy
to work with values so you can do basic
math with this as well as change it from
set values to full dollar values and
display values as well so first let's go
ahead and get this installed so I'm just
going to go ahead and copy this install
command jump into my terminal for some
random project and paste this in here so
we'll utilize this as our main helper
that we'll want to make available
throughout our application so let's jump
into our application here and we can
either import the currency.js itself or
we can create our own service wrapper so
importing itself is pretty easy you just
import it just like any other module
anywhere within your application so
let's go ahead and create a surface
wrapper for us so services and let's
call this Currency Service so right
there we just created a new service
directory and put a Currency Service
file within it and then let's go ahead
and just export default class
Currency Service just like so next let's
go ahead and import currency from
currency.js and let's do public static
currency equals currency so we're just
going to set the currency import value
to currency so that's available at
Currency Service dot currency so that we
have access to all the methods should we
need them and then let's also create a
couple of helper methods so we'll have
public we'll make this static so that we
don't need to create a new instance of
our Currency Service class and let's
call this to display so for this what
we'll want to do is take in a sense
value of type number and then return
back a display safe value so this should
have a dollar sign in front of it as
well as any necessary commas so we can
return and then you can either just do
currency or you can do this dot currency
since we do have this locally on our
service pass in the sent value and then
we'll want to say that this is from
sense
and set our Precision to two lastly so
that we actually get this to a display
safe format let's go ahead and just call
format that will add in the dollar sign
and any comments that might be needed
for the actual value so we have one
method there let's go ahead and add
another static and let's call this 2db
so we'll take in a display value and
then make it DB safe by changing it back
to sense so taking some value this could
be a number or it could be a string and
then all that we need to do here is
return this again you could just do
currency if you wanted to currency
provided the value and then get back the
Imp value and that will give us the
actual set value cool so that gives us a
basic wrapper for our Currency Service
here so we can call it to display to
make it display safe or 2db to make it
database safe ready and go ahead and
give that a save so now in order to
utilize the service anywhere outside of
edge JS so that would be anywhere within
our controllers middleware exceptions
what have you all we need to do is
actually import this class and then call
these methods or reach for the currency
value in itself so let's run through a
hypothetical of utilizing this so let's
say on our user model we're storing some
account balance we're not actually doing
that so this won't persist to the
database but we could do at column here
and let's say that we have a public
account balance of number we can easily
set this column up so that it stores
sense in the database and then anytime
that's queried it will utilize the
actual display safe value so we can
utilize that via the column decorator
here and we can set consume take in the
value and then let's import our Currency
Service and call our two display method
and then provided that value and what
this will do is anytime that our account
balance is queried it will take the
value from the database and make it
display safe via our currency.2 display
method here we could do the inverse side
of that as well utilizing prepare taking
the value there and then we can reach
for our currency service.2db and provide
the value and this will do the inverse
it will take a display saved value and
turn it into sense making it safe for
storage in our database and so this will
become a cycle whenever we query it will
be made display safe and then whenever
we store it to our database it will be
changed into sense so that we can easily
consume it once more later on and since
we put this on the column decorator this
will happen automatically anytime that
we query our actual user utilizing the
model so we've got one half of the
equation set up we just need to follow
this same approach anywhere within our
application outside of Edge Let's go
ahead and finish the other half by
covering how we can make this service
available throughout Edge as well as an
edge Global so for this we can easily
create a globals file Within our start
directory I just so happen to already
have one within this project but you can
easily do this by jumping into your
terminal and running node Ace make prld
file and then giving it a name of
something like globals like we have here
and that will create out a file like
this I believe you start out with
something just like this so then all
that you would want to do is import view
from Adonis core View and then just like
we're doing with the roles here already
in order to make our Currency Service
globally available we can just do view
Global Currency or you can call it
Currency Service and then provide it in
at the imported value of our Currency
Service so utilizing this approach here
will actually namespace all of our
Currency Service methods and properties
to the currency namespace within Edge
and all throughout Edge so if we wanted
to we could jump into some file here and
we could do a paragraph and we could say
okay let's do currency dot to display
and let's provide it 1000. give that a
save let's boot this application up so
that we can actually take a look at it
open up and there you go we have a
display Safe 10 dollars so that's one
approach of utilizing it we can also
take a look at how the inverse would
work so let's do another paragraph here
and let's say 2db obviously we're not
storing this to the database we're just
going to print it out onto the page but
we could say maybe we have 350. give
that a save jump back into our browser
give it a refresh and you can see
there's no space here but we do have our
10 previous value as well as 350 cents
for our database safe value so this
approach is working just fine now let's
say maybe you didn't want it bound to
the currency namespace how would you go
about that so instead of binding the
entire service to a single namespace we
can grab just the properties that we
want out of the service so we could do
currency to display is currency
service.2 display then we could do the
same thing for our 2db so Global
Currency to DB give it whatever name
that you want here just make sure that
you're naming it well enough so that's
not going to have any collisions Down
the Road 2 DB okay so now we have both
our currency to display and currency 2
DB methods off of our Currency Service
available you can do this with
properties as well so we can do
view.global and just name this currency
and then provide it Currency Service dot
currency we could also import the
currency package all together and
provide it in as the single value so we
give that a save jump back into our view
here I'm gonna go ahead and put these
inside of a div so that they're a little
bit easier to discern and let's copy
those two and paste them once more and
this time we'll call currency to display
currency to DB and we'll add one more
for the actual currency property in
itself so we'll do just
currency let's say 5 million and then if
we wanted to we could add a million to
that and then call format to get a
display safe value give that a save jump
back into our browser refresh so we have
10 and 350 from our previous examples we
have 10 and 350 from our new example
reaching just for the currency to
display and currency 2db methods and
then we also have 6 million for our 5
million plus 1 million to format and you
can mix and match these as well so
instead of doing two format here we
could also do currency to display in
value so here we're grabbing the cent
value of this currency chain so this
will return back the cents and then
we're taking no sense and making them
display safe via our currency to display
value so we can give this a save refresh
and we still have that 6 million right
there so there you have it there's how
you can create your own helper services
and make them available throughout the
entirety of your adonisjs application
similar to how Adonis is doing it
themselves via their helpers hopefully
you learned something new in this lesson
if you did please be sure to hit the
like button and subscribe down below and
I will see you in the next one
[Music]
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!