Ir al contenido principal

How to create an AEM component using Angularjs

If you want to create an AEM app using Angular.js components and add them into a page, in this series, you will learn how. Above is the vid and below you will find some useful notes. In the second part we will see How to configure the Sling Model for the AEM Angularjs component.
  1. 1.

    Pre-reqs

  2. 2.

    Creating an AEM application with Angular.js support

    • You will need Adobe's archetype for creating your AEM application with SPA support. The support was added starting from the 23rd version of the archetype.
    • So let’s create the app from scratch. I am providing the most important properties in a single line here given that the other properties will be inferred from them, notice that the frontendModule property was set to angular
      mvn -B archetype:generate `
          -D archetypeGroupId=com.adobe.granite.archetypes `
          -D archetypeArtifactId=aem-project-archetype `
          -D archetypeVersion=23 `
          -D aemVersion=6.5.0 `
          -D appTitle="Angularjs Simple Example" `
          -D appId="angularjs-simple-example" `
          -D groupId="co.dlighthouse.aem.angularjs.simpleexample" `
          -D frontendModule=angular `
          -D includeExamples=y `
          -D includeDispatcherConfig=n
      
    • Let’s build and deploy our new app (make sure that you have AEM up and running)
      mvn clean install -PautoInstallPackage -Padobe-public
      
  3. 3.

    AEM component using angularjs

    • For creating an AEM component using angular we need at least 4 things, a skeleton AEM component with a dialog, a angular component, an entry importing the component inside the app.module.ts file and a sling model which I’ll be configuring in the next video.
    • I am going to use the yeoman generator for creating the skeleton AEM component for me with some additional example fields in the dialog. I am calling it basic-component and I am making sure that the names for the folders and component group match my current application’s configuration.
      yo aem-application:aem-component `
          mavenAppsModuleName=ui.apps `
          componentParentPath=angularjs-simple-example/components `
          componentNodeName=basic-component `
          componentName="Basic Component" `
          componentGroup="Angularjs Simple Example - Content" `
          mavenBundleModuleName=core `
          javaRootPackageName=co.dlighthouse.aem.angularjs.simpleexample.core `
          javaModelRelativePackageName=models `
          javaModelClassName=BasicModel
      
    • We should end up with a component inside componentParentPath=angularjs/components called Basic Component, if you check the dialog’s xml file you will find two example fields, one for the text and another for the checkbox.
      <items
        jcr:primaryType=\"nt:unstructured\">
          <text
              jcr:primaryType=\"nt:unstructured\"
              sling:resourceType=\"granite/ui/components/coral/foundation/form/textfield\"
              fieldLabel=\"Text\"
              name=\"./text\"/>
          <checkbox
              jcr:primaryType=\"nt:unstructured\"
              sling:resourceType=\"granite/ui/components/coral/foundation/form/checkbox\"
              text=\"Checkbox\"
              uncheckedValue=\"false\"
              value=\"true\"
              name=\"./checkbox\"/>
          <checkboxType
              jcr:primaryType=\"nt:unstructured\"
              sling:resourceType=\"granite/ui/components/foundation/form/hidden\"
              name=\"./checkbox@TypeHint\"
              value=\"Boolean\"/>
      </items>
      
    • Before creating the component make sure that you have the Angular command-line interface (CLI) by first installing it in your computer
      npm install -g @angular/cli
      
    • Use the Angular.js CLI to create the component inside the components folder.
      ng generate component components/basic-component
      
      In the ui.frontend module you will find the Angular.js components inside the src/app/components folder. Add the imports for the Input annotation object from the angular/core library and the MapTo helper from adobe.
      import { Component, Input, OnInit } from '@angular/core';
      import { MapTo } from '@adobe/cq-angular-editable-components';
      
    • Create an editconfig obj which will be used for displaying some text when the component is empty, in this case given that the component’s properties like the ones defined on the dialog are available through the angular’s cqModel object we can just check if cqModel.text doesn’t have a value so that we can mark this component as empty
      const BasicComponentEditConfig = {
        emptyLabel: 'Basic Component',
        isEmpty: cqModel =>
          !cqModel || !cqModel.text || cqModel.text.trim().length < 1
      };
      
    • Then we have the component which looks like a typical angular component. Here we declare that we are going to receive the text and checkbox properties.
      
      export class BasicComponentComponent implements OnInit {
        @Input() text: string;
        @Input() checkbox: boolean;
      ...
      
    • Finally we call the MapTo helper function that maps the angular component to a particular resourceType inside AEM so that aem knows which angular component to use when rendering our page
      MapTo('angularjs-simple-example/components/basic-component')(
        BasicComponentComponent,
        BasicComponentEditConfig
      );
      
    • Then inside the app.module.ts file we just add an entry into the entryComponents array given that the basic component is added dynamically.
      
      @NgModule({
        ...
        entryComponents: [TextComponent, PageComponent, BasicComponentComponent],
        ...
      })
      export class AppModule {}
      
    • Build and deploy the application again
      mvn clean install -PautoInstallPackage -Padobe-public
      
  4. 4.

Comentarios

Entradas populares de este blog

Creating Docker containers for Adobe Experience Manager

This is a Docker tutorial for creating a docker image for the Galen framework, above is the vid and below you will find some of the steps followed. Adobe experience manager is a content management system which in a nutshell is an application that allows us to create web sites to be consumed by end users. You might be familiar with other such applications like wordpress or drupal which serves the same purpose A typical deployment would be comprised of two AEM instances, the author instance used for creating and modifying content, the publish instance which serves the content and finally we have a dispatcher which is a static web server used for caching, load balancing and some security purposes. We can configure an AEM instance to work as an author or publish instance by either changing the file name for the jar file java -jar cq-author-450…

Creating a Mongo replicaset using docker: Mongo replicaset + Nodejs + Docker Compose

This is a Docker tutorial for creating a Mongo replica set using docker compose and also a couple more containers to experiment with the replica set, above is the vid and below you will find some of the steps followed.
StepsPre-reqsHave node.js installedAnd docker installed (make sure you have docker-compose as well)Create a container for defining configurations for a mongo instanceCreate a container for setting up the replica setCreate a simple node app using expressjs and mongoose (A modified version from the previous video)Create a docker-compose file with the mongo and setup containers and two additional containers for experimenting with the replica setBuild, Run and experiment with your new containers Create a dockerfile for the first mongo container (not really needed but you could configure more stuff if needed)Include container with mongo preinstalled: FROM mongoCreate default/working directory: WORKDIR /usr/src/configsCopy mongo's configurations file into the container
C…

Create a custom AEM workflow process step with a dialog

In this tutorial I talk about how to create a custom workflow step process with an additional dialog for configuring it, above is the vid and below you will find some useful notes. 1.Pre-reqs Have access to an Adobe Experience Manager instance. Have Maven installed, understand how it works and also understand how to use Adobe's archetype, you can watch my video about maven here: Creating an AEM application using Maven and Adobe's archetype 2.What is an AEM Workflow and workflow model Workflows allow you to automate different tasks inside AEM by defining a sequence of steps to be executed and the actions to be performed at each step. A workflow model represents the definition of a workflow using nodes for the steps or actions to be executed and transitions to define how the steps are organized and what is going to be th…