Skip to content

Instantly share code, notes, and snippets.

@wienczny
Forked from garlicnation/exampleBehavior.js
Last active September 16, 2015 21:11
Show Gist options
  • Save wienczny/15cc41d50a533fd8bc5e to your computer and use it in GitHub Desktop.
Save wienczny/15cc41d50a533fd8bc5e to your computer and use it in GitHub Desktop.
behavior singleton model
<script>
// Resolve / declare namespace
window.MyCustomBehaviors = window.MyCustomBehaviors || {};
// This is a globally shared connection.
var singletonConnection = null;
window.MyCustomBehaviors.SocketConnectionBehavior = {
properties: {
connection: {
type: Object,
value: function () {
return getConnection();
}
}
},
// This function returns the global connection and creates an instance of it if necessary.
getConnection: function () {
if (singletonConnection === null) {
// Shared instance is created and assigned here
singletonConnection = new SocketConnection();
}
return singletonConnection;
}
};
window.MyCustomBehaviors.MyCustomInheritedBehavior = [
Polymer.NeonSharedElementAnimatableBehavior, // just an example
window.MyCustomBehaviors.SocketConnectionBehavior
];
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment