惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
K
Kaspersky official blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Recorded Future
Recorded Future
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
S
Schneier on Security
P
Palo Alto Networks Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
N
Netflix TechBlog - Medium
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
C
Check Point Blog
L
LangChain Blog
腾讯CDC
小众软件
小众软件
T
Tenable Blog
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
About on SuperTechFans
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
雷峰网
雷峰网
美团技术团队
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
博客园_首页

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
requirejs学习笔记及requirejs在Open edX中的使用
2016-05-05 · via 夜行人

文章目录

原理

解决的问题

  • 实现js文件的异步加载,避免网页失去响应;
  • 管理模块之间的依赖性,便于代码的编写和维护。

概述

  • Require.JS 是一个基于 AMD 规范的 JavaScript 模块加载框架
  • 实现 JavaScript 文件的异步加载,管理模块之间的依赖性,提升网页的加载速度。
  • AMD 是 Asynchronous Module Definition 的缩写,意思就是 异步模块定义。它采用异步方式加载模块,模块的加载不影响它后面语句的运行。所有依赖这个模块的语句,都定义在一个回调函数中,等到加载完成之后,这个回调函数才会运行。

常见问题

参考javascript模块化编程(三):require.js的用法

模块的加载

  • 使用require.config()方法,我们可以对模块的加载行为进行自定义
    • require.config()就写在主模块(main.js)的头部
  • require.js提供了一个优化工具,当模块部署完毕以后,可以用这个工具将多个模块合并在一个文件中,减少HTTP请求数。
  • 如果某个模块在另一台主机上,也可以直接指定它的网址
1
2
3
4
5
6
:::text
 require.config({
    paths: {
      "jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min"
    }
  });

加载非规范的模块

require.config()接受一个配置对象,这个对象有一个shim属性,专门用来配置不兼容的模块。

入门

require.js插件

  • domready插件,可以让回调函数在页面DOM结构加载完成后再运行。
  • text和image插件,允许require.js加载文本和图片文件
  • json和mdown,用于加载json文件和markdown文件

参考

demo


Open edX与requirejs

Open edX前端采用了requirejs来管理模块依赖,这方面的相关文档可参考:<openedx.atlassian.net/wiki>

细节

  • /edx/app/edxapp/edx-platform/cms/static/js/base.js
  • require-config.js (ack-grep -g “require-config.js”)
    • /edx/app/edxapp/edx-platform/lms/static/lms/js/require-config.js
    • /edx/app/edxapp/edx-platform/cms/static/cms/js/require-config.js
      • 在此可以写给mathjax cdn路径,mathjax
  • 来看一下引用了require-config.js的地方(ack-grep require-config.js)
    • lms/templates/main.html
      • 页面开始的static_content.html来自common/djangoapps/pipeline_mako/templates/static_content.html
    • lms/static/lms/js/build.js
    • cms/templates/base.html
    • cms/static/cms/js/build.js
  • 引用require.js的地方 (ack-grep require.js)
    • cms/templates/base.html
    • cms/envs/common.py
    • lms/envs/common.py ,require.js被放在base_vendor_js里
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
base_vendor_js = [
    'js/vendor/jquery.min.js',
    'js/vendor/jquery.cookie.js',
    'js/vendor/url.min.js',
    'js/vendor/underscore-min.js',
    'js/vendor/require.js',
    'js/RequireJS-namespace-undefine.js',
    'js/vendor/URI.min.js',
    'js/vendor/backbone-min.js'
]
  • main_vendor_js由包括了base_vendor_js,之后lms/templates/main_django.html中有:
1
2
3
4
:::text
  {% block main_vendor_js %}
  {% javascript 'main_vendor' %}
  {% endblock %}

案例cms login

我们以cms的login.html为例(/edx/app/edxapp/edx-platform/cms/templates/login.html)

1
2
3
4
5
6
:::text
<%block name="requirejs">
  require(["js/factories/login"], function(LoginFactory) {
      LoginFactory("${reverse('homepage')}");
  });
</%block>

这部分就是添加自己逻辑的地方: /edx/app/edxapp/edx-platform/cms/static/js/factories/login.js

可以看到这是按照AMD规范写的模块

从这里我们也可以看到mako如何把参数传到js里

案例 search_results_view

其他案例包括How to add a new page to LMS or Studio推荐的例子:

关于ui方便,官方推荐使用backbone.js来写

案例asset(观察backbone的使用)

  • ack-grep backbone ,跟踪asset.js
    • /edx/app/edxapp/edx-platform/cms/static/js/collections/asset.js
  • ack-grep js/collections/asset
    • static/js/factories/asset_index.js
  • ack-grep “js/factories/asset_index”
    • /edx/app/edxapp/edx-platform/cms/templates/asset_index.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
:::text
<%block name="requirejs">
    require(["js/factories/asset_index"], function (AssetIndexFactory) {
        AssetIndexFactory({
          assetCallbackUrl: "${asset_callback_url}",
          uploadChunkSizeInMBs: ${chunk_size_in_mbs},
          maxFileSizeInMBs: ${max_file_size_in_mbs},
          maxFileSizeRedirectUrl: "${max_file_size_redirect_url}"
        });
    });
</%block>
  • ack-grep asset_index.html
    • djangoapps/contentstore/views/assets.py
  • -> def assets_handler
  • -> url(r’^assets/{}/{}?$’.format(settings.COURSE_KEY_PATTERN, settings.ASSET_KEY_PATTERN), ‘assets_handler’)
    • 对于studio课程里的文件&上传

文章作者 种瓜

上次更新 2016-05-05