Skip to content

Instantly share code, notes, and snippets.

@vtching
Forked from lstrrs/components.share-box.js
Last active October 30, 2017 18:28
Show Gist options
  • Save vtching/7e1d491a584e7a1b980323834e0a3f87 to your computer and use it in GitHub Desktop.
Save vtching/7e1d491a584e7a1b980323834e0a3f87 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Component.extend({
shareText: '',
word: '',
wordToObserve: '',
actions: {
onShareEditorValueChanged(shareEditorValue) {
this.set('shareText', shareEditorValue);
},
onSelectHashtag(text) {
// Trick to trigger a didReceiveAttrs for the same word
this.set('word', '');
this.set('word', text);
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
value: '',
sharingEventManager: Ember.inject.service(),
// PROBLEM: performance issues
// PROBLEM: not triggered when it is the same word
onWordToObserveChanged: Ember.observer('wordToObserve', function() {
console.log('onWordToObserveChanged', this.get('wordToObserve'));
this.insertText(this.get('wordToObserve'));
}),
didInsertElement() {
// PROBLEM: services are singleton, won't play well if
// there are multiple instances of this component
this.get('sharingEventManager').on('insertWord', this, function(text) {
this.insertText(text);
});
},
// PROBLEM: not triggered when it is the same word
// => set word to empty string before setting the new value (trick to trigger a didReceiveAttrs for the same word)
// PROBLEM: cannot target which attr to listen to
// => can be solved by ember-diff-attrs addon
didReceiveAttrs() {
console.log('didReceiveAttrs');
if (this.get('word')) {
this.insertText(this.get('word'));
}
},
insertText(text) {
this.set('value', `${this.get('value')}${text}`);
this.get('onValueChange')(this.get('value'));
},
actions: {
onValueChange() {
this.get('onValueChange')(this.get('value'));
},
}
});
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
onSelectText(text) {
this.get('onSelectText')(text);
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
sharingEventManager: Ember.inject.service(),
actions: {
onSelectText(text) {
this.get('sharingEventManager').trigger('insertWord', text);
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
onSelectText(text) {
this.set('wordToObserve', text);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Service.extend(Ember.Evented, {
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{share-box}}
<br>
<br>
{{share-editor word=word wordToObserve=wordToObserve onValueChange=(action "onShareEditorValueChanged")}}
{{share-suggestions-attrs onSelectText=(action "onSelectHashtag")}}
{{share-suggestions-observer wordToObserve=wordToObserve}}
{{share-suggestions-evented}}
<p>Share text is:</p>
{{shareText}}
{{textarea input=(action "onValueChange") value=value}}
<button onclick={{action "onSelectText" "#hashtag-attr1"}}>#hashtag-attr1</button>
<button onclick={{action "onSelectText" "#hashtag-attr2"}}>#hashtag-attr2</button>
<button onclick={{action "onSelectText" "#hashtag-evented1"}}>#hashtag-evented1</button>
<button onclick={{action "onSelectText" "#hashtag-evented2"}}>#hashtag-evented2</button>
<button onclick={{action "onSelectText" "#hashtag-observer1"}}>#hashtag-observer1</button>
<button onclick={{action "onSelectText" "#hashtag-observer2"}}>#hashtag-observer2</button>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment