如何从缓存或 ajax 加载中返回模板
How to return templates from cache or ajax load?
在我的代码中,我尝试从缓存加载模板。如果缓存中不存在模板 - 通过 ajax 从服务器加载模板。加载完成后,我想将模板放入缓存并返回。是这样的:
var manager = function () {
return {
cache: [],
getTemplate: function (templateId) {
this.templateId = templateId;
if (this.cache[this.templateId]) {
return this.cache[this.templateId];
}
return this.loadTemplate();
},
loadTemplate: function() {
var
self = this;
$.get('/assets/templates/' + this.templateId + '.html', function (templateHtml) {
self.cache[self.templateId] = templateHtml;
return self.getTemplate(self.templateId);
});
}
}
}
var
manager = manager();
$('body').append( manager.getTemplate('template') );
我知道我的代码不起作用,因为 ajax 请求在函数加载模板结束后完成。我认为代码可以使用延迟对象修复,但不知道如何修复。谁能帮我找到解决方案?
有两种方法可以实现您的目标:
-
承诺(有很多库/垫片)。我将它重写为 ES6 只是为了学习:
let manager = function () { return { cache: [], getTemplate(id) { let cache = this.cache; return new Promise((resolve, reject) => { if (cache[id]) { resolve(cache[id]); } else { this.loadTemplate(id) .then(template => { cache[id] = template; resolve(template); }) .fail(reject); } }); }, loadTemplate(id) { return $.get('/assets/templates/' + id + '.html'); } } }; let manager = manager(); manager.getTemplate('template').then((template) => { $('body').append(template); });
-
回调:
let manager = function () { return { cache: [], getTemplate(id, cb) { let cache = this.cache; if (cache[id]) { cb(cache[id]); } else { this.loadTemplate(id) .then(template => { cache[id] = template; cb(template); }); } }, loadTemplate(id) { return $.get('/assets/templates/' + id + '.html'); } } }; let manager = manager(); manager.getTemplate('template', (template) => { $('body').append(template); });
这是
您将如何做到这一点,支持所有主要浏览器,并缓存请求。这样,每个模板将只执行 1 个请求。(其他答案仅缓存响应)。
var Manager = function() {
return {
cache: [],
getTemplate(id) {
var that = this;
if (that.cache[id] && that.cache[id].then){
console.log("Promise cache");
return that.cache[id]; //return promise from cache
}
return $.Deferred(function() {
var def = this;
if (that.cache[id]) {
console.log("Retrieved from cache!");
return def.resolve(that.cache[id]); //return cached template
}
that.cache[id] = def; //Cache promise
console.log("Retrieving template...");
that.loadTemplate(id).then(function(template) {
that.cache[id] = template;
def.resolve(template)
}).fail(function() {
def.reject();
});
return that.cache[id]; //return promise
}).promise();
},
loadTemplate(id) {
return $.get('https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js');
}
}
};
var manager = Manager();
manager.getTemplate('template').then(function(template){
console.log("loaded 1");
});
//This will use the promise from the first call (1 Request only)
manager.getTemplate('template').then(function(template){
console.log("loaded 2");
manager.getTemplate('template').then(function(template){
console.log("loaded 3"); //This will be retrieved fully from cache
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
当您通过 AJAX
获取模板时,您只能在 AJAX 成功时附加结果。因此,您需要将追加逻辑作为回调传递。检查下面的代码。
var manager = function () {
return {
cache: [],
getTemplate: function (templateId,callback) {
this.templateId = templateId;
if (this.cache[this.templateId]) {
callback(this.cache[this.templateId]);
}
this.loadTemplate(callback);
},
loadTemplate: function(callback) {
var
self = this;
$.get('/assets/templates/' + this.templateId + '.html', function (templateHtml) {
self.cache[self.templateId] = templateHtml;
callback(templateHtml)
});
}
}
}
var
manager = manager();
manager.getTemplate('template',function(result) {
$('body').append( result );
});
您可能不需要 2 个函数来执行此操作。所以你可以把它作为一个整体
相关文章:
- 通过浏览器加载页面时触发加载脚本(js或jQuery)'s”;返回“;作用
- 将数据加载到使用JSON返回的表单字段时出现问题
- JQuery BlockUI鼠标加载光标没有't在Google Chrome中返回默认值
- 加载时的getelementbyID返回null
- 返回上一页,不加载执行正文
- Phonegap-返回按钮重新加载整个页面
- 调用一个不返回任何内容的servlet..加载时从jsp
- 通过单击JSP和javascript加载两个返回函数
- 返回并重新加载页面时显示不需要的服务器端警报消息
- outerWidth(true) 连续返回两次不同的值,而无需重新加载页面
- 返回后页面不会重新加载
- 在 java 脚本中从 JSON 加载内容之前返回值
- 返回顶部按钮在加载时出现
- 如何在自定义模式对话框中加载通过 AJAX 调用返回的 JSON 数据
- 在浏览器返回时触发事件,并使用新 url 强制刷新或重新加载页面
- 返回到以前加载的页面并显示弹出窗口
- 加载 JS 文件时返回 404 错误
- 等待加载的数据,然后在 AngularFire 0.9.0 中返回
- getElementById 在加载 dom 后返回未定义
- AngularJS 返回加载控制器的泛型函数