Scala PlayFramework and Angular JS - too much effort in terms of duplication and mixing concetps
Asked Answered
I

6

13

I tried to write an application on playframework + scala + Angular JS. The purpose was to create an web app that would work either good when JavaScript is OFF or ON in the browser. This is usual requirement when one writes Public Site (which should be good for people and for Google that would index it)

So I ended up with ~50% code written in JavaScript, having two(2) folders named "controller" - one for Scala code, one for JS code (since AngularJS uses controller concept as well).

Also, since JS code is supposed to use Ajax calls, I had to create one more scala-controller that returns json, but not html back to client's request. And.. this all I do not like. This seems as too much effort.

On playframework template side, I should think how to combine Scala with JavaScript, passing parameters from one language to another. Using some tricks like showing that when JS would be processed, showing that when it is supposed to be off.

This all makes my template less understanable, and one day, when I have huge code base, I may come up with a solution to duplicate my templates as well (js templates + scala templates) - to use proper template when JS is off/on. Then amount of JS code could rise up to 60%..

And then it seems that I'm going to duplicate everything, like having two different application - for Google and for people. What would be common (only) is my data themselves in my database, I have not to duplicate data. But.. there we might have a different issue with a format of that data (and most likely would be json-based [because I do not wasting processor time to do table-object transformation], but not table based - NoSQL.. then again we come to JS like DB - like to MongoDB), and JS works very good with Json, natively.

Then question, why not use 100% of JS for simple things like: request-response, forming a page, layouts. Server side JS controller could form my templates - then I do not need to switch from one language to another and be even more productive in this sense.

Question: Do you have any suggestions? Best practices about this? I'm not that guy who want to end up using NodeJS to useJavaScript as ONE languages for everything - for server and client. But let's speak about productivity and business needs.

There is an example (not mine) of AnjularJS controllers, how much JS I have to write: https://github.com/tastejs/todomvc/tree/gh-pages/architecture-examples/angularjs/js

and just imagine that I have to keep PlayFramework controllers along with that - for HTML and AJAX calls, mixing things in scala/playframework templates with JavaScript.

Inadvisable answered 13/9, 2013 at 15:56 Comment(3)
AngularJS is a RIA. Your question will be the same with a flex front-end. The first question i think is : What i nedd ? Is it a RIA ?Radiotransparent
This post really confuses me. If you're afraid of mixing concepts then don't. Don't use Play!'s templating engine. Use Play! just to serve assets and RESTful endpoints. Build your MVC app in JS with Angular.Philter
I just believe that there is might a solution in terms what Play could provide (eventually). That is more a message than a question.Inadvisable
C
2

I just made an example project showing how to write a AngularJS/Play application with Scala.js:

https://github.com/greencatsoft/scalajs-angular-todomvc

There should be some rough edges, but I believe being able to write everything (including the AngularJS parts) in Scala can appeal to some people.

Caporal answered 10/9, 2014 at 12:35 Comment(2)
This is a great initiative, looking forward to try this out!Entelechy
I guess I have to choose some answer (to prevent trying to answer it). And since I have tend to use scala-js, not necessary with angular though. The IDE and sbt make it possible - to be quite pleasant experience.Inadvisable
L
5

I would like to share the receipt used in the eventual seed (https://github.com/angyjoe/eventual):

  1. Write your HTML. Doing so, please feel free to use as many JavaScript frameworks and libraries as you wish (the seed is AngularJS though).

  2. Decide your Play models from which the controllers follow.

  3. Decide the operations (only those you need!) for each of controller (list, create, show(id), update(id), delete(id)). Implement these operations as Scala actions.

  4. Insert a Play route to serve each implemented action to the client framework.

  5. Insert one (and only one!) Play route to serve your styled HTML to the client framework.

  6. Spend the rest of your development time and effort on the client-side...

Louis answered 17/11, 2013 at 3:54 Comment(0)
I
3

For now this seems could help me:

https://github.com/nau/jscala

or this : https://github.com/lampepfl/scala-js

But this is not necessary an answer.

UPDATE:

The only one way I see for scala developers who do not want jump too deep in JS, is:

Wait until solutions like JScala or Scala-JS become ready to use (or contribute) in order to substitute CoffeScript in Play2. Before that day we just have to live with that.

Here you we can see: https://github.com/typesafehub/angular-seed-play

The play app with no app / view directory in it. Aslo the "routes" file is almost empty- because Angular cares about it on client side. So we do not need them.

Kind of RIA way. So if one day we decide to to make this app available for Google (to be googlable), we will put view & routes there.

UPDATE 2:

Now I'm working on some PlayFramework project. And I found that it is not so bad at all to use just plain request-response + little JQuery or JScala.

My point is (so commonly known point I should say, but so easy to forget): You don't know for sure whether your app will be popular or not. Will you have 1K users per second or not. If yes - then you (WILL) have problems with performance that you can improve with shifting some part of apps on client side (or use coupe of handred Akka Actors on server side). All people (like me) should think about the point of development productivity (this is what PlayFramework was created for initially) when your ideas could lay on your templates/html to see the result immediately. There is so much noise about AngualrJS that people (even me :) ) loosing your mind jumping there without thinking.

Does it make sense / critical whether you load 200k of json file only or 200k + 5k html from sever side (comparing request-response vs rest - json)? Probably not. For sites / app like stackoverflow and many others it is not critical. Development productivity is important.

The "point", that is important. "Follow the bright point and carry on (C)" :)

UPDATE 3: (evolution based on real needs)

Now I have an app that contains of 2 modules:

  1. "web" - nodejs app with angularjs on client side
  2. "server" play application with no views nor server side templates. But I'm disappointed that in IntelliJ idea with Scala & SBT plugin it just kills my laptop firing up all 8 cores with no reason (it may take some time when humans invent quantum computers.. I do not have time to wait for it).

All I need is simple rest stuff for now.. So I may end up having NODE JS server (for stuff like working with MongoDB) and Play 1.3 server (java)! (connecting it to scala libs/logic when needed). After all I just need REST layer - it is not suppose to be rocket science anyway. So to create a simple web site - I will use 10% of Play and 90% of JS until there is additional logic that make sense to create on scala making use of strong typing and functional approach and being connected with existing java api when I need it. Even though I'm java guy (historically) I find things tend to me over-complicated in java and now scala world.

Seems: one should consider create stuff with html, css and node / js first, and then think further what he needs when he creates site-based app. To make use of type-safety when the logic requires it.

UPDATE 4:

Interesting bits

  1. https://github.com/greencatsoft/scalajs-angular
  2. https://github.com/greencatsoft/scalajs-angular-todomvc
  3. related question: Scala-JS for real web project
Inadvisable answered 13/9, 2013 at 17:36 Comment(0)
V
3

It looks like angular is too much for your case. Probably you could stick with simpler Ajax for the js side, like jQuery, and use Rest endpoints as play routes to feed the json to your page.

Vainglory answered 14/9, 2013 at 0:0 Comment(0)
A
3

Have you seen this blog post - AngularJS and Play Framework?

Adeno answered 22/9, 2013 at 7:28 Comment(1)
In that blog the app works with sockets. there play is used as rest-server. again 20% of scala, 70% - JS.Inadvisable
C
2

I just made an example project showing how to write a AngularJS/Play application with Scala.js:

https://github.com/greencatsoft/scalajs-angular-todomvc

There should be some rough edges, but I believe being able to write everything (including the AngularJS parts) in Scala can appeal to some people.

Caporal answered 10/9, 2014 at 12:35 Comment(2)
This is a great initiative, looking forward to try this out!Entelechy
I guess I have to choose some answer (to prevent trying to answer it). And since I have tend to use scala-js, not necessary with angular though. The IDE and sbt make it possible - to be quite pleasant experience.Inadvisable
C
0

Here is a good project example of mixing (angularjs play mongodb and scala) with great tutorial, it shows how to build a modern web application, comprising of a Client-side JavaScript App built using AngularJS wrote in CoffeeScript, served from the Play Framework and using document persistence with Reactive Mongo a non-blocking Scala client for MongoDB :

https://github.com/lashford/modern-web-template#master

http://typesafe.com/activator/template/modern-web-template

Continuative answered 23/4, 2015 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.