-
-
Save wienczny/15cc41d50a533fd8bc5e to your computer and use it in GitHub Desktop.
behavior singleton model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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