2

I want to integrate AngularJS2 in Magnolia CMS (I'm new to AngularJS). I tried the quickstart Project localy it works correctly with npm start.

I used npm tsc to get the .js file to use those in Magnolia for testing if it works. All the node_modules files i got over unpkg.com (I locked up in the terminal which files where used for the quickstart project and integrated all of them). Adding the <my-app> tag in an ftl template and all the scripts at the same place (the App-js files are loaded from magnolia itselfs). This didn't worked, so I do not know what I'm missing.

I also tried to do something similar to this one (but with use @angular instead of angular2 beta).

I'm not sure if this is possible to do like this, since the Quickstart works (with npm start) but on my tomcat server it doesn't. I thought that the problem may be the tomcat server.

I also tried it without any server just with this code snipped, where the structure was as follow:

test/test.html

test/app/app.component.js & app.module.js & main.js

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Update these package versions as needed -->
 <script src="https://unpkg.com/zone.js@0.7.6/dist/zone.js"></script>
 <script src="https://unpkg.com/systemjs@0.20.5/dist/system.src.js"></script>
 <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
 <script src="https://unpkg.com/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script>
 <script src="https://unpkg.com/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
 <script src="https://unpkg.com/@angular/core/bundles/core.umd.js"></script>
 <script src="https://unpkg.com/@angular/common/bundles/common.umd.js"></script>
 <script src="https://unpkg.com/@angular/compiler/bundles/compiler.umd.js"></script>
 <script src="https://unpkg.com/rxjs/symbol/observable.js"></script>
 <script src="https://unpkg.com/rxjs/Observable.js"></script>
 <script src="https://unpkg.com/rxjs/Subject.js"></script>
 <script src="https://unpkg.com/rxjs/util/root.js"></script>
 <script src="https://unpkg.com/rxjs/util/toSubscriber.js"></script>
 <script src="https://unpkg.com/rxjs/Subscriber.js"></script>
 <script src="https://unpkg.com/rxjs/Subscription.js"></script>
 <script src="https://unpkg.com/rxjs/util/ObjectUnsubscribedError.js"></script>
 <script src="https://unpkg.com/rxjs/SubjectSubscription.js"></script>
 <script src="https://unpkg.com/rxjs/symbol/rxSubscriber.js"></script>
 <script src="https://unpkg.com/rxjs/Observer.js"></script>
 <script src="https://unpkg.com/rxjs/util/isFunction.js"></script>
 <script src="https://unpkg.com/rxjs/util/isArray.js"></script>
 <script src="https://unpkg.com/rxjs/util/isObject.js"></script>
 <script src="https://unpkg.com/rxjs/util/tryCatch.js"></script>
 <script src="https://unpkg.com/rxjs/util/errorObject.js"></script>
 <script src="https://unpkg.com/rxjs/util/UnsubscriptionError.js"></script>
 <script src="app/app.component.js"></script>
 <script src="app/app.module.js"></script>
 <script src="app/main.js"></script>
<script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
<title>Title</title>
</head>
<body>
 <my-app>Loading AppComponent content here ...</my-app>
</body>
</html>

But this also didnt worked.

I've a REST API to get an Validator for a formular and other stuff. But now I need to build a page for the formular. I've no idea how I could build this page. Is it possible to do it inside Magnolia with ftl? What's the best solution here

EagleT
  • 84
  • 1
  • 10

3 Answers3

1

You wouldn't integrate with the magnolia cms templates. It would be better if you used the Magnolia CMS REST API and built an angular app that interacted with that.

Nick
  • 978
  • 1
  • 12
  • 28
  • I've a REST API to get an Validator for a formular and other stuff. But now I need to build a page for the formular. What's the best purpose here? Can you maybe give me a good how to? – EagleT Feb 08 '17 at 16:25
1

Why don't you build it as Magnolia light module w/ Magnolia CLI (NPM) and run gulp as part of your build setting output to go in the /webresources folder?
That way, you can compile all comfortably, and get Magnolia serve your js files for you, with possibility to set security, reload things at runtime and all the other stuff Magnolia normally does for you.

More info about CLI, Light Module structure and light development in general.

Jan
  • 4,369
  • 14
  • 25
  • Thanks for the idea with the light module. Do you maybe also have a good way to fill in content in the App with magnolia? The only way I managed it to work at the moment, is with using the direct component rendering of magnolia, which gives me an html of the component with the content. But I'm still searching for a better way to solve this. – EagleT Feb 21 '17 at 06:04
  • "Fill in content" as in "consume content from CMS to display in Angular app? I would guess, you prefer to get the content as JSON instead of html, right? If that's the case, I'd recommend to define variation for the component so when requested w/ .js (or similar extension) it get's renderered as json. Example (for page) here: https://gist.github.com/47fc19d0e66c6cb8bbcaa07b1c66dced And example of the script to render content as json: https://gist.github.com/e78f74c425f1904490ad57f725f46d5c It uses jsonfn that you can get from: https://github.com/rah003/neat-jsonfn – Jan Feb 21 '17 at 20:29
  • Yes with fill in content I mean to consume content from the CMS to use it in the AngularApp templates. Of course html is okay here(since the Angular template needs to be in html), but at the moment this "binding" works only with copying the link to the component direct rendering. I was wondering if there is a better way to bind this. Since I'm new to AngularJS 2 (and 1) I'm not sure if this also works with JSON files. – EagleT Feb 22 '17 at 12:15
  • Sry I forgot to mention that I first tried the compilation process with gulp, but this didn't worked for me. As mentioned here: http://stackoverflow.com/questions/42141044/typescript-to-javascript-transpiler-for-angular2#answer-42141466 my next try will be to integrate the transpiling process with the build process using angular-cli. But finally, I want to be able to add content to a component on a site in magnolia, and this component is build with angularJS 2 (with angularJS 1 this isn't a problem) – EagleT Feb 22 '17 at 14:55
  • 1
    IMO that should not be the problem, once you build it and output in the light module to make it available to Magnolia, you can also have component in Magnolia whose template will just load compiled js resources. Should work just fine. If you run into concrete issue while trying so, let's try to look at whatever problem will be then. – Jan Feb 23 '17 at 08:12
0

With this [plunker][1] I was able to get AngularJS running without node.js. Before I tried to compile my .ts files with gulp. The compilation worked, but the files where formated wrong (here an [example][2] I found that worked with already compiled fils to js (the files are from the quickstart project that I already mentioned in the question.

 [1]: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=preview


 [2]: https://plnkr.co/edit/8lOtCy4GPsq9hZH6D38E?p=preview
EagleT
  • 84
  • 1
  • 10