File tree Expand file tree Collapse file tree
app/assets/javascripts/discourse/mixins Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments