AngularJS, code.explode, Coding

Hailing All Frequencies – Communicating in AngularJS with the Pub/Sub Design Pattern

Hailing All Frequencies! Is anybody out there? This is the USS AngularJS, we have a problem our services are speaking in Klingon and our controllers can’t communicate with their Ferengi directives. Can anyone help us!

I don’t know how many times I’ve seen a question about what is the best way to communicate between components in AngularJS. A lot of the time the resulting answer is to use the $rootScope object to $broadcast a message to anyone who might be listening for it. But, that really isn’t the best way to do it. Broadcasting messages between components means that they need to know too much about how things are coded reducing their modularity and re-use.

In this article I show you how to use the Pub/Sub Design Pattern for inter-component communications in AngularJS.

Continue reading

Standard
AngularJS, code.explode, Coding

Localizing Your AngularJS Apps: Yet Another Update

So you are probably really jazzed about the addition of the new directive to the angularjs-localizationservice project and the fixes to help improve it’s performance. But, now that you are probably using the project I bet you are probably saying the same thing my devs and designers were saying, “Hey Jim, this new directive really works great, but how do we do translations for placeholders and titles?”

Well, this post is going to cover a new directive that’s been added to the project just to handle such things.

Continue reading

Standard
AngularJS, code.explode, Coding

Localizing Your AngularJS Apps: Update

In my previous article on Localization, Localizing Your AngularJS App, I provided a simple service and filter that allows you to provide language translations based on a translation file. Initially this worked for about 50% of our transalation needs, however once we started looking at the overall performance of our AngualrJS App it was apparent that I needed to tweak things a bit.

Continue reading

Standard
AngularJS, code.explode, Coding

Creating a Simple AngularJS Directive

One of the core features of AngularJS is the ability to extend the HTML Syntax with directives. Directives allow you to componentize segments of HTML into a single tag that can be re-used throughout your AngularJS app. So instead of repeating the following HTML all over your code:

    <div class="row-fluid" ng-repeat="adjunct in adjuncts | filter:search | orderBy:'name'">
        <div class="span1">{{adjunct.Name}}</div>
        <div class="span1">{{adjunct.Type}}</div>
        <div class="span1">{{adjunct.Use}}</div>
        <div class="span1">{{adjunct.Time}}</div>
        <div class="span1">{{adjunct.Amount}}</div>
        <div class="span1">{{adjunct.UseFor}}</div>
        <div class="span1">{{adjunct.Notes}}</div>
        <div><a href="#/editadjunct/{{adjunct._id.$oid}}"><i class="icon-pencil"></i></a></div>
    </div>

With a directive you can use the following HTML:

    <div data-display-adjunct class="row-fluid" ng-repeat="adjunct in adjuncts | filter:search | orderBy:'name'">

Now anywhere where you would use the above HTML, you can use the directive instead and be consistent across you entire site. This also saves you time when changes are required. If the amount of data that needs to be displayed changes you only need to update the directive and your done. You don’t have to hunt around your HTML files looking for every occurrence to change.

Directives allow you to not only specify what data from your app to bind to, but they can also handle the work of transforming the data into different formats, removing that responsibility from your controller, helping you to keep to the single responsibility principle.

If you apply this simple principle on bigger scales, directives can be used to build rich components that bind to your app’s data and reduce the amount of HTML you need to create across your entire site.

In this article, I’m going to show you how to write a simple directive that generates an image tag with a user’s avatar from the Gravatar site, http://en.gravatar.com/

Continue reading

Standard