Updated docs for `Discourse.Singleton`

This commit is contained in:
Robin Ward 2013-08-08 13:00:18 -04:00
parent ce05b43c1d
commit 477eb81bc9
1 changed files with 43 additions and 0 deletions

View File

@ -2,6 +2,49 @@
This mixin allows a class to return a singleton, as well as a method to quickly
read/write attributes on the singleton.
Example usage:
```javascript
// Define your class and apply the Mixin
User = Ember.Object.extend({});
User.reopenClass(Discourse.Singleton);
// Retrieve the current instance:
var instance = User.current();
```
Commonly you want to read or write a property on the singleton. There's a
helper method which is a little nicer than `.current().get()`:
```javascript
// Sets the age to 34
User.currentProp('age', 34);
console.log(User.currentProp('age')); // 34
```
If you want to customize how the singleton is created, redefine the `createCurrent`
method:
```javascript
// Define your class and apply the Mixin
Foot = Ember.Object.extend({});
Foot.reopenClass(Discourse.Singleton, {
createCurrent: function() {
return Foot.create({toes: 5});
}
});
console.log(Foot.currentProp('toes')); // 5
```
@class Discourse.Singleton
@extends Ember.Mixin
@namespace Discourse