简体   繁体   English

在Django中使用JavaScript的最佳实践

[英]Best practice for using JavaScript in Django

I want to push my Django project with some JavaScript/jQuery. 我想用一些JavaScript / jQuery来推动我的Django项目。 To make it right from the beginning on I'd like to know, which way of organizing the .js-files ist the optimal one. 从一开始我就知道,组织.js文件的哪种方式是最佳的。

For loading one big file includes less overhead than loading many small ones and also because it looks cleaner in the code I considered to make one global .js-file and include that with the base.html (from which every template inherites). 对于加载一个大文件包含比加载许多小文件更少的开销,并且因为它在我认为制作一个全局.js文件的代码中看起来更干净,并且包含base.html(每个模板从中继承)。 However, the result would be, that JavaScript would try to assign all the event-binings, even if the elements which the events should be bind to aren't in the current document. 但是,结果是,JavaScript会尝试分配所有事件的结构,即使事件应该绑定到的元素不在当前文档中。 With all the jQuery-selectors which then would have to do their work that can't be too efficient. 有了所有jQuery选择器,那么它们就必须完成他们的工作,这些工作效率不会太高。 From earlier web-development experience I know that one can do something like if(location.href == '/some/url/') { (JavaScript code) ... } . 从早期的Web开发经验我知道可以做一些像if(location.href == '/some/url/') { (JavaScript code) ... } That seems not practicable for me in this case, for with changing URLs, I'd have to change the URLconf and the .js-file (while using reverse() and {% url %} to prevent that elsewhere). 在这种情况下,这对我来说似乎不切实际,因为对于更改URL,我必须更改URLconf和.js文件(同时使用reverse()和{%url%}来防止其他地方)。 I guess there is no possibility to make use of the named URLs here? 我想这里不可能使用命名的URL吗?

Has anyone an idea how to organize the JavaScript without having a file for every single template on the one hand and without killing performance unnecessarily? 有没有人知道如何组织JavaScript一方面没有每个模板的文件,也没有不必要的杀死性能?

I don't know that this question is specific to Django - similar issues come up managing Javascript in all sorts of systems. 我不知道这个问题是特定于Django的 - 类似的问题出现在各种系统中管理Javascript。

That said, I usually try to tier my Javascript files, so that truly global scripts and libraries are included in one file, scripts specific to a section of the site are included in a set of section-specific files, and scripts specific to a single page are included in yet another site of page-specific files (or in inline code, depending on the context). 也就是说,我通常会尝试对Javascript文件进行分层,以便真正的全局脚本和库包含在一个文件中,特定于站点一部分的脚本包含在一组特定于部分的文件中,以及特定于单个脚本的脚本页面包含在另一个页面特定文件的站点中(或内联代码,具体取决于上下文)。

Django has good support for this approach, because you can tier your templates as well. Django对这种方法有很好的支持,因为你也可以对模板进行分层。 Include the global script in your base.html template, then create a mysection-base.html template that inherits from base.html and just adds the Javascript (and CSS) files specific to that section. base.html模板中包含全局脚本,然后创建一个继承自base.htmlmysection-base.html模板,只添加特定于该部分的Javascript(和CSS)文件。 Then subpages within that section can inherit from mysection-base.html instead of base.html , and they'll all have access to the section-specific scripts. 然后该部分中的子页面可以继承mysection-base.html而不是base.html ,并且他们都可以访问特定于部分的脚本。

I find django-compressor invaluable as it automatically compresses and minifies your JavaScript and CSS pre-deployment. 我发现django-compressor非常有用,因为它会自动压缩和缩小你的JavaScript和CSS预部署。 It even automatically handles SASS, LESS and CoffeeScript if they float your boat. 如果他们漂浮你的船,它甚至会自动处理SASS,LESS和CoffeeScript。

来自http://djangopackages.com/grids/g/asset-managers/的应用可能有所帮助。

You use modular javascript. 你使用模块化的JavaScript。

Choose your packager of choice (mine is browserify ) that packages all your modules into one package that you minify and gzip. 选择您所选择的打包程序(我的是browserify ),您的所有模块打包成您压缩一个包和gzip。 You send this file to the client and it is cached. 您将此文件发送到客户端并缓存它。

This means you have all your code cached, minimize HTTP requests and stay lean and efficient. 这意味着您可以缓存所有代码,最大限度地减少HTTP请求并保持精简和高效。

And since you have modular code you just load your code as you would normally. 而且由于您有模块化代码,您只需按正常方式加载代码即可。

Personally I would use some form feature detection to load modules. 我个人会使用一些表单特征检测来加载模块。 You can choose to feature detect on almost any feature (some css selector, routes, url segments). 您可以选择对几乎任何功能(某些css选择器,路由,网址段)进行检测。

Feature detection would look like this : 功能检测如下所示:

var Features = {
  "class": "name",
  "class2": "name2",
  "dynamic-scroll": "dynamic-scroll",
  "tabstrip": "tabstrip",
  ...
}

for (var key in Features) {
  require(Features[key]);
}

Where as routing with davis would look like 使用davis的路由看起来像

Davis(function() {
  this.get("blog", function(req) {
    require("blog")(req);
  });

  this.get("blog/:post", function(req) {
    require("blog-post")(req);
  });

  this.get("shop", function(req) {
    require("shop")(req);
  });
  ...
});

Alternatively you can try an event driven architecture. 或者,您可以尝试事件驱动的架构。 This means each module binds to events 这意味着每个模块都绑定到事件

// some-module

mediator.on("blog-loaded", function() {
  // load in some libraries

  // construct some widgets

  mediator.emit("blog-ui-build", widgets);
});

And you would need some bootstrapping in place to kick off the event loop. 你需要一些引导来启动事件循环。 Feel free to look at an EDA demo 随意看一下EDA演示

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

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