Scala PlayFramework 和 Angular JS - 在复制和混合概念方面付出了太多努力

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18790544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:39:52  来源:igfitidea点击:

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

javascriptajaxscalaangularjsplayframework-2.0

提问by ses

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)

我试着写一个应用程序playframework+ scala+ Angular JS。目的是创建一个 web 应用程序,当 JavaScript 在浏览器中关闭或打开时,它都能正常工作。这是编写公共站点时的通常要求(这对人们和将其编入索引的 Google 都有利)

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

所以我最终用.js编写了大约 50% 的代码JavaScript,有两(2)个名为“控制器”的文件夹- 一个用于Scala代码,一个用于JS代码(因为 AngularJS 也使用控制器概念)。

Also, since JScode is supposed to use Ajaxcalls, I had to create one morescala-controller that returns json, but not htmlback to client's request. And.. this all I do not like. This seems as too much effort.

此外,由于JS代码应该使用Ajax调用,我不得不创建另一个返回 的scala 控制器json,但不html返回客户端的请求。而且..这都是我不喜欢的。这似乎太费力了。

On playframeworktemplate side, I should think how to combine Scalawith JavaScript, passing parameters from one language to another. Using some tricks like showing thatwhen JS would be processed, showing thatwhen it is supposed to be off.

playframework模板方面,我应该想到如何结合Scala使用JavaScript,传递参数从一种语言到另一种。使用一些技巧,如显示的是当JS会被处理,显示的是当它应该被关闭。

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 JSis off/on. Then amount of JS code could rise up to 60%..

这一切都让我的模板不太容易理解,有一天,当我拥有庞大的代码库时,我可能会想出一个解决方案来复制我的模板(js 模板 + scala 模板)——在JS关闭/打开时使用正确的模板。那么 JS 代码的数量可能会上升到60%。.

And then it seems that I'm going to duplicate everything, like having two different application- for Googleand 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 JSworks very good with Json, natively.

然后似乎我将复制所有内容,例如拥有两个不同的应用程序- 用于Google和用于人员。常见的(仅)是我的数据本身在我的数据库中,我不必复制数据。但是..在那里,我们可能对数据的格式有不同的问题(并且很可能是基于 json 的 [因为我不会浪费处理器时间来进行表对象转换],但不是基于表的 - NoSQL ..然后我们再次来到JS像DB -喜欢的MongoDB),以及JS作品很好用JSON,本身

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

那么问题来了,为什么不将 100%JS用于简单的事情,例如:请求-响应形成页面布局。服务器端JS控制器可以形成我的模板 - 然后我不需要从一种语言切换到另一种语言并且在这个意义上更加高效。

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

问题:您有什么建议吗?这方面的最佳实践?我不是那种人谁想要最终使用NodeJS使用JavaScript作为一种语言用于一切-服务器和客户端。但让我们谈谈生产力和业务需求。

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

有一个 AnjularJS 控制器的例子(不是我的),我必须写多少 JS: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.

想象一下,我必须同时保留 PlayFramework 控制器 - 对于 HTML 和 AJAX 调用,将 scala/playframework 模板中的内容与 JavaScript 混合。

采纳答案by mysticfall

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

我刚刚制作了一个示例项目,展示了如何使用 Scala.js 编写 AngularJS/Play 应用程序:

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

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.

应该有一些粗糙的边缘,但我相信能够在 Scala 中编写所有内容(包括 AngularJS 部分)可以吸引某些人。

回答by user2845946

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

我想分享最终种子中使用的收据(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...

  1. 编写您的 HTML。这样做,请随意使用任意数量的 JavaScript 框架和库(尽管种子是 AngularJS)。

  2. 确定控制器遵循的 Play 模型。

  3. 为每个控制器 ( list, create, show(id), update(id), delete(id))决定操作(只有那些你需要的!)。将这些操作实现为 Scala 操作。

  4. 插入一个 Play 路由来为客户端框架提供每个实现的操作。

  5. 插入一个(并且只有一个!)播放路由以将您的样式化 HTML 提供给客户端框架。

  6. 将剩余的开发时间和精力花在客户端上...

回答by ses

For now this seems could help me:

现在这似乎可以帮助我:

https://github.com/nau/jscala

https://github.com/nau/jscala

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

或者这个: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:

对于不想在 JS 中跳得太深的 Scala 开发人员,我看到的唯一一种方法是:

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

等到像 JScala 这样的解决方案或Scala-JS准备好使用(或贡献)以替代 CoffeScript inPlay2。在那一天之前,我们只需要忍受它。

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

在这里我们可以看到:https: //github.com/typesafehub/angular-seed-play

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

没有app / view目录的播放应用程序。同样,“路由”文件几乎是空的——因为 Angular 在客户端关心它。所以我们不需要它们。

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& routesthere.

一种 RIA 方式。因此,如果有一天我们决定将这个应用程序提供给 Google(可搜索),我们会将视图路线放在那里。

UPDATE 2:

更新 2:

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

现在我正在做一些PlayFramework项目。而且我发现使用简单的请求-响应+ 小JQueryJScala并没有那么糟糕。

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 secondor 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 Actorson server side). All people (like me) should think about the point of development productivity(this is what PlayFrameworkwas created for initially) when your ideas could lay on your templates/html to see the result immediately. There is so much noise about AngualrJSthat people (even me :) ) loosing your mind jumping there without thinking.

我的观点是(我应该说的是众所周知的观点,但很容易忘记):您不确定您的应用程序是否会流行。每秒是否有1000 个用户。如果是 - 那么您(将)会遇到性能问题,您可以通过在客户端移动某些部分应用程序来提高性能(或在服务器端使用手动Akka Actors 的轿跑车)。当您的想法可以放在模板/html 上以立即查看结果时,所有人(像我一样)都应该考虑开发效率(这是PlayFramework最初创建的目的)。关于AngualrJS那个人(甚至我:))的噪音太大了,不假思索地跳到那里。

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

无论您是200k of json仅加载文件还是200k + 5k html从服务器端加载文件(比较请求-响应与休息 - json)是否有意义/关键?可能不是。对于网站/应用程序stackoverflow和许多其他网站,这并不重要。开发生产力很重要。

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

“点”,这很重要。“跟随亮点,继续(C)”:)

UPDATE 3: (evolution based on real needs)

更新3:(基于实际需求的演变)

Now I have an app that contains of 2 modules:

现在我有一个包含 2 个模块的应用程序:

  1. "web"- nodejsapp with angularjson client side
  2. "server"playapplication 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 coreswith no reason (it may take some time when humans invent quantum computers.. I do not have time to wait for it).
  1. “web”-客户端nodejs带有angularjs 的应用程序
  2. “服务器”play应用程序,没有视图也没有服务器端模板。但我很失望,在IntelliJ 想法与 Scala 和 SBT 插件它只是杀死我的笔记本电脑无缘无故地启动所有 8 个内核(当人类发明量子计算机时可能需要一些时间......我没有时间等待它) .

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.3server (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 JSuntil 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.

我现在需要的只是简单的休息。所以我最终可能会拥有 NODE JS 服务器(用于使用 MongoDB 之类的东西)和Play 1.3服务器(java)!(在需要时将其连接到 Scala 库/逻辑)。毕竟我只需要 REST 层——无论如何它都不应该是火箭科学。因此,要创建一个简单的网站 - 我将使用10%的 Play 和90% 的 JS,直到有额外的逻辑可以在 Scala 上使用强类型和功能方法创建并在我需要时与现有的 java api 连接它。即使我是 Java 人(从历史上看),我发现 Java 和现在的 Scala 世界中的事情往往过于复杂。

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

似乎:应该首先考虑使用html,cssnode/创建东西,js然后在创建基于站点的应用程序时进一步考虑他需要什么。为了利用type-safety当逻辑需要它。

UPDATE 4:

更新 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
  1. https://github.com/greencatsoft/scalajs-angular
  2. https://github.com/greencatsoft/scalajs-angular-todomvc
  3. 相关问题:Scala-JS for real web project

回答by pagoda_5b

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.

看起来角度对于您的情况来说太多了。可能你可以坚持在 js 端使用更简单的 Ajax,比如 jQuery,并使用 Rest 端点作为播放路由来将 json 提供给你的页面。

回答by Adrian

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

你看过这篇博文 - AngularJS and Play Framework吗?

回答by Seif Tamallah

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 :

这是一个很好的混合项目示例(angularjs play mongodb 和 scala)和很棒的教程,它展示了如何构建一个现代 Web 应用程序,包括一个使用 CoffeeScript 编写的 AngularJS 构建的客户端 JavaScript 应用程序,由 Play 框架和将文档持久性与 Reactive Mongo 一起使用,这是 MongoDB 的非阻塞 Scala 客户端:

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

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

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

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