Skip to content

Instantly share code, notes, and snippets.

@diegocasmo
Last active May 10, 2016 16:37
Show Gist options
  • Save diegocasmo/3f01fcca8dc186214d2e to your computer and use it in GitHub Desktop.
Save diegocasmo/3f01fcca8dc186214d2e to your computer and use it in GitHub Desktop.
Defining a backbone model with the singleton pattern. Useful for models that represent things such as the "AppState".
define([
'underscore',
'backbone'
], function (_, Backbone) {
var Singleton = Backbone.Model.extend({
}, {
singleton: null,
getInstance: function() {
Singleton.singleton = Singleton.singleton || new Singleton;
return Singleton.singleton;
}
});
return Singleton;
});
// usage
// var model = Singleton.getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment