Skip to content
  • totten's avatar
    CRM-20600, CRM-20112 - AngularLoader - Add helper for base-pages and injections · b20ea913
    totten authored
    This commit extracts the `Civi\Angular\Page\Main::registerResources()` and
    creates a new utility-class, `AngularLoader`.  The `AngularLoader` can be
    used in new Angular base-pages, e.g.
    
    ```php
    class Example extends CRM_Core_Page {
      public function run() {
        $loader = new \Civi\Angular\AngularLoader();
        $loader->setPageName('civicrm/foo/bar');
        $loader->setModules(array('crmApp', '...'));
        $loader->load();
        return parent::run();
      }
    }
    ```
    
    The `AngularLoader` only loads the resources or assets (JS/CSS/HTML files).
    To start Angular, you'll need to call `ng-app` or `angular.bootstrap(...)`.
    One way to do this is to define a page-template:
    
    ```html
    <!-- Example.tpl -->
    <div ng-app="crmApp">
      <div ng-view></div>
    </div>
    ```
    
    Or you can reuse the existing template:
    
    ```php
      public function getTemplateFileName() {
        return 'Civi/Angular/Page/Main.tpl';
      }
    ```
    
    Note: This is framed as a utility-class which loads the Angular resource
    files.  It's not a page-class/base-class.  This approach allows us to call
    the utility from other situations -- e.g.  inject AngularJS onto an
    pre-existing page via hook.  Doing that may or may not be wise, but the
    class-hierarchy shouldn't be the issue.
    b20ea913