CRM-20600, CRM-20112 - AngularLoader - Add helper for base-pages and injections
authorTim Otten <totten@civicrm.org>
Thu, 25 May 2017 03:43:07 +0000 (20:43 -0700)
committerTim Otten <totten@civicrm.org>
Sat, 17 Jun 2017 02:05:09 +0000 (19:05 -0700)
commitb20ea91388fcd4941b2187dc35ffa4d6a16a8762
tree13991e3f06068d78d8eaa8111fb3b06d8996a418
parent8da6c9b8aae1cd2ca4474473018a1349061061f7
CRM-20600, CRM-20112 - AngularLoader - Add helper for base-pages and injections

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.
Civi/Angular/AngularLoader.php [new file with mode: 0644]
Civi/Angular/Page/Main.php
ang/crmApp.js