Skip to content

Instantly share code, notes, and snippets.

@kara-ryli
Created May 14, 2012 02:50
Show Gist options
  • Save kara-ryli/2691504 to your computer and use it in GitHub Desktop.
Save kara-ryli/2691504 to your computer and use it in GitHub Desktop.
A simple wrapper around datasource that allows multiple instances to be created, but only a single request be made.
/*global YUI*/
/**
A simple wrapper around datasource that allows multiple
instances to be created, but only a single request be
made.
@module singleton-datasource
@requires base-build, base-base, datasource-local
**/
YUI.add("singleton-datasource", function (Y) {
"use strict";
var GlobalEnv = YUI.namespace("Env.SingletonDatasource"),
/**
Creates a new instance of SingletonDatasource, or
returns an existing instance if one has been created
with the same properties.
@class SingletonDatasource
@constructor
@example
var ds = new Y.SingletonDatasource({
type: Y.Datasource.IO,
source: "/expensive-ajax-request"
});
ds.once("response", function (event) {
// do something with the response
});
**/
SingletonDatasource = function (config) {
var sig;
if (!(config.type && config.source)) {
throw new Error("SingletonDatasource requires a valid type and source.");
}
sig = [config.type.NAME, String(config.source), config.request].join("|");
if (GlobalEnv[sig] instanceof SingletonDatasource) {
return GlobalEnv[sig];
}
this.sig = sig;
SingletonDatasource.superclass.constructor.apply(this, arguments);
};
Y.mix(SingletonDatasource, {
NAME: "singleton-datasource",
ATTRS: {
/**
The datasource type to use. The value must be a class that
extends Y.Datasource.Local.
@attribute type
@type Function
@writeOnce
**/
type: {
value: Y.Datasource.Local,
validator: function (DataSource) {
return Y.Lang.isFunction(DataSource) && (new DataSource() instanceof Y.DataSource.Local);
},
writeOnce: true
},
/**
Pointer to live data.
@attribute source
@type MIXED
@writeOnce
**/
source: {
value: "",
writeOnce: true
},
/**
The request to send to the live data source, if any.
@attribute request
@type String
@writeOnce
**/
request: {
value: "",
validator: Y.Lang.isString,
writeOnce: true
},
/**
Array of plugins to add to the datasource. Each entry should be an object
with the following properties:
<dl>
<dt>fn</dt>
<dd>The Plugin class to use</dd>
<dt>cfg</dt>
<dd>Config object to supply the plugin, if any.</dd>
</dl>
@attribute plugins
@type Array
@writeOnce
**/
plugins: {
value: [],
writeOnce: true
}
}
});
Y.extend(SingletonDatasource, Y.Base, {
initializer: function () {
var attrs = this.getAttrs(),
ds = new attrs.type({ source: attrs.source }),
self = this;
this.publish("response", {
emitFacade: true,
fireOnce: true
});
if (Y.Lang.isArray(attrs.plugins)) {
Y.Array.each(attrs.plugins, function (plugin) {
ds.plug(plugin.fn, plugin.cfg);
});
}
ds.sendRequest({
request: attrs.request,
on: {
success: function (event) {
self.fire("response", {
datasource: ds,
data: event.data,
originalEvent: event
});
},
failure: function (event) {
self.fire("response", {
datasource: ds,
error: true,
originalEvent: event
});
}
}
});
this.ds = ds;
},
destructor: function () {
this.ds.destroy();
delete GlobalEnv[this.sig];
}
});
Y.SingletonDatasource = SingletonDatasource;
}, "3.4.1", {
requires: ["base-build", "base-base", "datasource-local"]
});
@kara-ryli
Copy link
Author

I've written code like this a bunch recently, where I've needed to load content to the page exactly once given an undefined number of possible requests.

The next step is to write an example that extends Y.Datasource.Function to load a javascript library with the Y.Get.script method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment