Skip to content

Commit 477eb81

Browse files
committed
Updated docs for Discourse.Singleton
1 parent ce05b43 commit 477eb81

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

app/assets/javascripts/discourse/mixins/singleton.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,49 @@
22
This mixin allows a class to return a singleton, as well as a method to quickly
33
read/write attributes on the singleton.
44
5+
6+
Example usage:
7+
8+
```javascript
9+
10+
// Define your class and apply the Mixin
11+
User = Ember.Object.extend({});
12+
User.reopenClass(Discourse.Singleton);
13+
14+
// Retrieve the current instance:
15+
var instance = User.current();
16+
17+
```
18+
19+
Commonly you want to read or write a property on the singleton. There's a
20+
helper method which is a little nicer than `.current().get()`:
21+
22+
```javascript
23+
24+
// Sets the age to 34
25+
User.currentProp('age', 34);
26+
27+
console.log(User.currentProp('age')); // 34
28+
29+
```
30+
31+
If you want to customize how the singleton is created, redefine the `createCurrent`
32+
method:
33+
34+
```javascript
35+
36+
// Define your class and apply the Mixin
37+
Foot = Ember.Object.extend({});
38+
Foot.reopenClass(Discourse.Singleton, {
39+
createCurrent: function() {
40+
return Foot.create({toes: 5});
41+
}
42+
});
43+
44+
console.log(Foot.currentProp('toes')); // 5
45+
46+
```
47+
548
@class Discourse.Singleton
649
@extends Ember.Mixin
750
@namespace Discourse

0 commit comments

Comments
 (0)