Skip to content

Commit

Permalink
plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
dlasky committed Sep 10, 2015
1 parent 0535ca8 commit 348c1ed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/shared/js/ajaxbusterplugin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<link rel="import" href="datautils.html"/>
<script>
(function(scope) {
scope.AjaxBusterPlugin = function AjaxBusterPlugin(enabled, name) {
this.enabled = enabled;
this.name = name || "nocache";
}
AjaxBuster.prototype = {
requestHandler: function(ajaxOptions) {
if (this.enabled) {
ajaxOptions.queryParams.push(DataUtils.param(this.name, Date.now()));
}
}
responseHandler: function(response) {
return response;
}
reset: function() {

}
};
}(window.StrandLib = window.StrandLib || {}));
</script>
47 changes: 47 additions & 0 deletions src/shared/js/ajaxcsrfplugin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<link rel="import" href="ajaxplugin.html"/>
<link rel="import" href="storage.html"/>
<link rel="import" href="datautils.html"/>
<script>
(function(scope) {
var DataUtils = StrandLib.DataUtils;
var Storage = StrandLib.Storage;

scope.AjaxCSRFPlugin = function AjaxCSRFPlugin(enable, useCache, header) {
this.enabled = enable;
this.useCache = useCache;
this.header = header || "X-CSRF-Token";
if (this.useCache) {
this.storage = new Storage("csrf");
this.token = this.storage.value;
}
}

AjaxCSRFPlugin.prototype = {

requestHandler:function(ajaxOpts) {
if (this.enabled && ajaxOpts && this.token) {
var csrf = DataUtils.param(this.csrfHeader, this.token);
ajaxOpts.headers.push(csrf);
}
return ajaxOpts;
},

responseHandler:function(response) {
var ajax = DataUtils.getPathValue("instance",response);
var hasHeader = ajax.xhr.getAllResponseHeaders().indexOf(this.csrfHeader) !== -1;
if (hasHeader && this.enabled) {
var csrf = ajax.xhr.getResponseHeader(this.csrfHeader);
this.token = csrf;
if (this.useCache) {
this.storage.value = csrf;
}
}
return response;
},
reset:function() {

}
};
}(window.StrandLib = window.StrandLib || {}));

</script>

0 comments on commit 348c1ed

Please sign in to comment.