JavaScript 架构/应用程序结构最佳实践?

发布于 2024-10-25 17:42:06 字数 267 浏览 1 评论 0原文

这些存在吗?

多年来,我一直是大型强类型面向对象语言(Java 和 C#)的奴隶,并且是 Martin Fowler 及其同类的信徒。 Javascript,由于它的松散类型和函数性质,似乎不适合我习惯的习惯用法。

组织 javascript 富客户端的最佳实践是什么?我对一切都感兴趣,从保存代码的位置(一个文件或多个文件)到 MVC 模式,再到四种模式再到分层。

不将东西放入全局命名空间似乎是唯一的共识。

我使用 JQuery 作为“扩展 API”。

Do these exist?

I've been a slave to big strongly typed OO languages (Java & C#) for many years and am a devotee of Martin Fowler and his ilk. Javascript, due to it's loosely typed and functional nature seems to not lend itself to the idioms I am used to.

What are the best practices for organizing a javascript rich client? I am interested in everything from where to keep your code, (one file or multiple files) to MVC patterns to gang of four patterns to layering.

Not putting stuff in the Global namespace seems to be the only consensus.

I am using JQuery as the "Extended API."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不会写诗 2024-11-01 17:42:06

我喜欢使用某种 MVC 客户端架构。

  • 我有一个页面控制器,
  • DOM 是我的视图,
  • 服务器是我的模型。

通常,我创建一个单例页面控制器类(需要关闭支持类)来控制 ajax 调用和视图绑定。

var pageXController = {
  init: function(param) {
    pageXController.wireEvents();
    // something else can go here
  },

  wireEvents : function() {
    // Wire up page events
  }

  // Reactive methods from page events
  // Callbacks, etc
  methodX : function() {}
}

$(document).ready( function() {
  // gather params from querystring, server injection, etc
  pageXController.init(someparams);
});

我还应该在这里补充一点,在这种情况下,您的模型是您的 DTO(数据传输对象),它们针对它们解决的问题进行了优化。这不是您的域模型。

I like to use a sort of MVC client side architecture.

  • I have a page CONTROLLER
  • The DOM is my VIEW
  • The server is my MODEL

Typically I create a singleton page controller class (with supporting classes off that is needed) that controls the ajax calls and view binding.

var pageXController = {
  init: function(param) {
    pageXController.wireEvents();
    // something else can go here
  },

  wireEvents : function() {
    // Wire up page events
  }

  // Reactive methods from page events
  // Callbacks, etc
  methodX : function() {}
}

$(document).ready( function() {
  // gather params from querystring, server injection, etc
  pageXController.init(someparams);
});

I should also add here that your MODEL in this case is your DTO's (Data Transfer Objects) which are optimised to the problem they solve. This is NOT your domain model.

我早已燃尽 2024-11-01 17:42:06

根据我的经验,对于复杂的 Javascript 开发,构建代码库至关重要。从历史上看,作为一种修补语言,Javascript 开发有一个很大的趋势,就是以大量脚本文件告终。

我建议在逻辑上分离应用程序的功能区域,以清除松散耦合和自包含的模块。例如,如下所示,您的产品套件可能有多个产品模块,每个模块都有多个子模块:

在此处输入图像描述

一次你有能力创建分层模块,这是在相关子模块中创建 UI 组件的问题。这些 UI 组件最好也是独立的。这意味着每个都有自己的模板、CSS、本地化等,如下所示:

在此处输入图像描述

我创建了一个 JS 参考架构使用示例代码来分享我在几个大型 JS 项目中获得的经验。我是boilerplateJS 的作者。如果您想要一个内置了几个关键问题的参考代码库,请将其用作您的启动代码库。

http://boilerplatejs.org

For complex Javascript development, structuring your codebase it critical in my experience. Historically being a patching language, there is a great tendency that Javascript development ending up with massive script files.

I'd recommend to logically separate the functional areas of the application in to clear modules that are loosely coupled and self contained. For example as shown below your product suite might have multiple product modules and each module with multiple sub modules:

enter image description here

Once you have the ability to create hierachical modules, it is a mater of creating UI components in relavant sub-module. These UI components should also preferably be self contained. Meaning each with own template, css, localization, etc. like shown below:

enter image description here

I created a JS reference architecture with sample code to share my expierince I gained in couple of large scale JS projects. I'm the author of boilerplateJS. If you want a reference codebase where several of the critical concerns built-in, feel fee to use it as your startup codebase.

http://boilerplatejs.org

━╋う一瞬間旳綻放 2024-11-01 17:42:06

如果我们谈论 javascript 应用程序架构,那么 Nicholas Zakas 2011 播客是必看的:
Nicholas Zakas:可扩展的 JavaScript 应用程序架构

还有 Addy Osmani 的在线参考:
大规模 JavaScript 应用程序架构模式

If we are talking about javascript apps architecture then Nicholas Zakas 2011 podcast is a must see:
Nicholas Zakas: Scalable JavaScript Application Architecture

Also Addy Osmani's online reference:
Patterns For Large-Scale JavaScript Application Architecture

土豪我们做朋友吧 2024-11-01 17:42:06

您可能想要研究的一件事是 Backbone.js 它为您提供了一个用于构建模型的良好框架类来表示应用程序中的数据并将它们绑定到 HTML UI。这优于将数据绑定到 DOM。

更一般地说,这是一篇来自 Opera 的关于 JavaScript 最佳实践的精彩文章开发博客。

One thing you might want to look into is Backbone.js which gives you a nice framework for building Model classes to represent that data in your application and bind them to your HTML UI. This is preferred to tying your data to the DOM.

More generally, here is a great article on JavaScript best practices from the Opera development blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文