加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Dojo Cache

发布时间:2020-12-16 21:28:29 所属栏目:百科 来源:网络整理
导读:分析 Dojo 源代码时遇到 Cache,以及pendingCacheInsert不是很理解,在Dojo官方网站找到这篇文章。http://dojotoolkit.org/reference-guide/1.7/dojo/cache.html. Dojo Cache 是通过getter 和 setter方法 获取和存储模块和指定URL相关的字符串。 这种存取方法

分析 Dojo 源代码时遇到 Cache,以及pendingCacheInsert不是很理解,在Dojo官方网站找到这篇文章。http://dojotoolkit.org/reference-guide/1.7/dojo/cache.html.

Dojo Cache 是通过getter 和 setter方法 获取和存储模块和指定URL相关的字符串。 这种存取方法已经被dojo/text!插件替代。写这篇文档主要作用是为了给 1.4 版本的用户。

介绍

许多时会你想要将原始的HTML 注入到DOM里。 但大部分时间你只能在Javascript里, 以字符串的形式输写HTML.

dojo.cache 允许你指定一个含有HTML文件的路径。 之后dojo.cache将调用XMLHttpRequest(XHR) 以同步的方式加载这个文件。 因为 XHR的方法,只能加载同域下的html页面。

Dojo 编译系统会在调用了dojo.cache的地方将HTML内联为字符串。 因此为了更好的性通,最好做Dojo编译. 编译也允许模块的跨域加载。

使用

dojo.cache 是Dojo的核心模块。 在你的页面中包含Dojo Cache, 需要先加载Dojo.cache模块。 当你使用dojo 1.7时,你应该请求 djojo/text.js,因为定义在dojo/cache.js 相关的功能已经移动到text.js里面。

// Dojo 1.7 (AMD)
require("dojo/text",function(){
    // write your code here
});

// Dojo < 1.7
dojo.require("dojo.cache");
dojo.cache 采用以下的参数
dojo.cache(module,url,configValue);

举例

// Dojo 1.7 (AMD)
require("dojo/text",function(){
  var text = dojo.cache("my.module","template.html");
});

// Dojo 1.7
dojo.require("dojo.cache");
var text = dojo.cache("my.module","template.html");
如果 "my/module/template.html" 包含 "<div>Hello World</div>",则 text变量将被设置为这个值。

以下是使用 sanitize: true选项的例子:
// Dojo 1.7 (AMD)
require("dojo/text","template.html",{sanitize: true});
});

// Dojo < 1.7
dojo.require("dojo.cache");
var text = dojo.cache("my.module","template.html");

如果 my/module/template.html 包含 "<html><body><h1>Hello</h1></body></html>",则 text变量将只会包含"<h1>Hello</h1>".

在举一个使用对象的例子,效果跟之前的一样。 但是使用对像的toString()方法代表文件的路径。
// Dojo 1.7 (AMD)
require("dojo/text",function(){
  var text = dojo.cache(new dojo._Url("my/module/template.html"),{sanitize: true});
});

dojo.require("dojo.cache");
var text = dojo.cache(new dojo._Url("my/module/template.html"),{sanitize: true});

获得更详细的信息,请查看
  1. dojo/text!
  2. dojo.require

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读