Skip to content

Commit d2d24b1

Browse files
author
Dan Lasky
committed
made it easier to pass external plugin list to sync on init
1 parent 92672b7 commit d2d24b1

6 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/shared/js/ajaxapiplugin.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
var DataUtils = StrandLib.DataUtils;
1212

13-
scope.AjaxAPIPlugin = function (config) {
13+
scope.AjaxAPIPlugin = function (config, name) {
1414

1515
this.config = DataUtils.copy({
16-
defaultRequest:"noop",
17-
defaultResponse:"noop",
18-
marshaller:{noop:function(o) {return o;}},
16+
defaultRequest:"noop",
17+
defaultResponse:"noop",
18+
marshaller:{noop:function(o) {return o;}},
1919
enabled:true,
2020
}, config);
2121

22+
this.name = name;
2223
};
2324

2425
scope.AjaxAPIPlugin.prototype = {

src/shared/js/ajaxbusterplugin.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
var DataUtils = StrandLib.DataUtils;
1212

13-
scope.AjaxBusterPlugin = function (config) {
13+
scope.AjaxBusterPlugin = function (config, name) {
1414
this.config = DataUtils.copy({
1515
name:"nocache",
1616
enabled:true
1717
}, config);
18+
this.name = name;
1819
};
1920

2021
scope.AjaxBusterPlugin.prototype = {

src/shared/js/ajaxcsrfplugin.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
var DataUtils = StrandLib.DataUtils;
1313
var Storage = StrandLib.Storage;
1414

15-
scope.AjaxCSRFPlugin = function (config) {
15+
scope.AjaxCSRFPlugin = function (config, name) {
1616
this.config = DataUtils.copy({
1717
useCache:true,
1818
header:"X-CSRF-Token",
19-
enabled:true,
19+
enabled:true,
2020
token:""
2121
}, config);
2222

2323
if (this.config.useCache) {
2424
this.storage = new Storage("csrf");
2525
this.config.token = this.config.token || this.storage.value;
2626
}
27+
28+
this.name = name;
2729
};
2830

2931
scope.AjaxCSRFPlugin.prototype = {

src/shared/js/ajaxpageplugin.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
var DataUtils = StrandLib.DataUtils;
1515

16-
scope.AjaxPagePlugin = function(config) {
16+
scope.AjaxPagePlugin = function(config, name) {
1717
this.config = DataUtils.copy({
1818
enabled:false,
1919
page:0,
@@ -26,10 +26,11 @@
2626
pageType:"offset",
2727
countPath:""
2828
},config);
29+
this.name = name;
2930
};
3031

3132
scope.AjaxPagePlugin.prototype = {
32-
33+
3334
nextPage: function() {
3435
this.page++;
3536
},
@@ -64,7 +65,7 @@
6465
if (opts.body && DataUtils.isType(ajaxOpts.body, "object")) {
6566
if (opts.pageName) ajaxOpts.body[opts.pageName] = page;
6667
if (opts.sizeName) ajaxOpts.body[opts.sizeName] = opts.pageSize;
67-
68+
6869
}
6970
}
7071
return ajaxOpts;
@@ -80,5 +81,5 @@
8081
}
8182
};
8283

83-
})(window.StrandLib = window.StrandLib || {});
84-
</script>
84+
})(window.StrandLib = window.StrandLib || {});
85+
</script>

src/shared/js/ajaxplugin.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
var DataUtils = StrandLib.DataUtils;
1212

1313
scope.AjaxPlugin = function(pluginQueue) {
14-
14+
1515
this.queue = [];
1616
if (pluginQueue) {
1717
this.queue = pluginQueue.map(function(p) {
18-
if (typeof p === "function")
18+
if (typeof p === "function") {
1919
return new p();
20+
} else {
21+
return p;
22+
}
2023
});
2124
}
2225
};
@@ -69,5 +72,5 @@
6972
}
7073

7174
};
72-
})(window.StrandLib = window.StrandLib || {});
73-
</script>
75+
})(window.StrandLib = window.StrandLib || {});
76+
</script>

src/shared/js/sync.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}, init);
6565
}
6666

67-
scope.Sync = function(endpoint, options, data, domHook, saveResponse) {
67+
scope.Sync = function(endpoint, options, data, domHook, saveResponse, plugins) {
6868
this._eventPrefix = _eventPrefix;
6969
this.endpoint = endpoint || "/";
7070
this.options = DataUtils.copy({
@@ -79,12 +79,13 @@
7979
this.saveResponse = (typeof saveResponse === "boolean") ? saveResponse : true;
8080

8181
this._ajax = new Ajax();
82-
this._plugin = new AjaxPlugin();
82+
this._plugin = new AjaxPlugin(this.plugins || [
83+
new AjaxCSRFPlugin({}, "csrf"),
84+
new AjaxBusterPlugin({}, "buster"),
85+
new AjaxAPIPlugin({}, "api"),
86+
new AjaxPagePlugin({}, "page"),
87+
]);
8388

84-
this._plugin.register("csrf", AjaxCSRFPlugin);
85-
this._plugin.register("buster", AjaxBusterPlugin);
86-
this._plugin.register("api", AjaxAPIPlugin);
87-
this._plugin.register("page", AjaxPagePlugin);
8889
};
8990

9091
scope.Sync.prototype = {
@@ -174,5 +175,5 @@
174175

175176
EventDispatcher.prototype.apply(scope.Sync.prototype);
176177

177-
})(window.StrandLib = window.StrandLib || {});
178-
</script>
178+
})(window.StrandLib = window.StrandLib || {});
179+
</script>

0 commit comments

Comments
 (0)