简体   繁体   English

带有Ionic,Cordova,Node.js,视图和其他“静态”文件的AngularJs

[英]AngularJs with Ionic, Cordova, Node.js, views and other 'static' files

Full disclosure, pretty new to Angular and Ionic and I'm trying to understand it conceptually and get a feel for best practices so that I don't gotcha myself further down the road. 全面披露,这对Angular和Ionic来说还很陌生,我正在尝试从概念上理解它,并获得最佳实践的感觉,以便我不再陷入困境。

I've built websites and apps based off vanilla html/css/js coupled with node.js backends and jquery mobile for the app UI. 我已经基于香草html / css / js结合了node.js后端和针对应用程序UI的jquery mobile构建了网站和应用程序。

In those cases where a web service/backend was required I mostly used .ejs files and partials for the views and served them using res.render responses (passing data into the view dynamically) something like... 在那些需要Web服务/后端的情况下,我通常使用.ejs文件和局部视图作为视图,并使用res.render响应(将数据动态传递到视图中)为它们提供服务,例如...

    app.get('/showallcustomers', function(req, res){
        db.connect("SELECT * from customers", [], function(result, error){
             var data = result.rows;
             res.render('pages/showall', {data:data})      
        });
    });

...while using S3 for my static JS/CSS/Images etc to alleviate the load on the node.js web service with regards to the static resources. ...同时将S3用于我的静态JS / CSS / Images等,以减轻有关静态资源的node.js Web服务的负担。

Other requests made via ajax would simply send/receive json from the node.js server. 通过ajax发出的其他请求将仅从node.js服务器发送/接收json。

Now, with angular js and the separation of concerns, with the view being separate from the controller and the data, I'm trying to figure out the best place to 'host' or serve my views and controllers. 现在,在使用角度js和关注点分离的情况下,将视图与控制器和数据分离,我试图找出“托管”或为我的视图和控制器提供服务的最佳位置。

In the case of an app, bundled with cordova, I could quite easily leave all these angular views/controllers in the root www folder of the app and package them up with the app (a bit like the giant 'page' driven index.html file for jQuery mobile) but could I instead use a cdn like S3 for my 'static' files like the angular view.html files and the controller.js files? 对于与cordova捆绑在一起的应用程序,我可以很容易地将所有这些角度视图/控制器保留在应用程序的根www文件夹中,并与应用程序打包在一起(有点像巨型的“页面”驱动的index.html jQuery mobile的文件),但我可以改用像S3这样的CDN作为我的“静态”文件(例如,角度view.html文件和controller.js文件)吗? Or even a more traditional web hosting type setup for static files? 甚至是针对静态文件的更传统的虚拟主机类型设置?

I could then (presumably?) (using the $http module) restrict the "data" flow for my site/app to a mysite.com/ api type setup? 然后,我可以(大概吗?)(使用$ http模块)将我的网站/应用程序的“数据”流限制为mysite.com/ api类型设置? Something like: 就像是:

app.config(function($routeProvider){
        $routeProvider
            .when('/',{
                controller: 'customersController',
                templateUrl: 'app/views/customers.html'

            })

and in the controller something like... 在控制器中,类似...

$http.get('api/customers');

But if this is the case, and I serve static files in this way, how does the routeProvider know where to find the controller? 但是,如果是这种情况,并且我以这种方式提供静态文件,则routeProvider如何知道在哪里可以找到控制器?

What do people generally do here? 人们在这里通常做什么? Completely separate the static files from the dynamic data? 将静态文件与动态数据完全分开? Or bundle it all together? 还是捆绑在一起? Is there a correct way to do this? 有正确的方法吗? Or am I misunderstanding something conceptually? 还是我在概念上误解了某些东西?

Assumptions: 假设:

  • I am not that familiar with Cordova, but I would assume it would work like browser extensions in high level. 我对Cordova不太熟悉,但是我认为它会像高级浏览器扩展一样工作。
  • I am also assuming that your application is getting enough traffic that alleviating the load of serving files is not an overkill. 我还假设您的应用程序获得了足够的流量,从而减轻了服务文件的负担,这并不是一个过大的选择。

Thoughts: 想法:

  • For JS files, bundling is probably a good idea. 对于JS文件,捆绑可能是一个好主意。 Regardless whether your server serves the files or S3 does, it will minimize the amount of connections the browser has to make. 无论您的服务器是提供文件还是由S3提供服务​​,它都将最大程度地减少浏览器必须建立的连接数。
  • For JS files, since they will most likely be included into the file via <script> tags, you can simply upload them into S3. 对于JS文件,由于它们很可能会通过<script>标记包含在文件中,因此您只需将它们上传到S3中即可。 This shouldn't give you too big of a problem. 这不应该给您带来太大的问题。
  • For HTML template files, I believe you can do the same. 对于HTML模板文件,我相信您也可以这样做。 Using RouteProvider and ng-include , you should be able to fetch them from S3. 使用RouteProviderng-include ,您应该能够从S3中获取它们。
  • Also, if you are doing API calls, using an angular service would be a better idea than using $http module in the controllers. 另外,如果您正在执行API调用,则使用角度服务比使用控制器中的$http模块更好。

Hope these help. 希望这些帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM