This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
twitter-api-client.js
71 lines (63 loc) · 1.92 KB
/
twitter-api-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* globals twttr */
var twitterScriptPromise;
export default Ember.Object.extend({
/*
* A tracking object implementing `shared(serviceName, payload)` and/or
* `clicked(serviceName, payload)` can be set on this object, and will
* be delegated to if present.
*/
tracking: null, // optional injection
load: function() {
var self = this;
if (!twitterScriptPromise) {
twitterScriptPromise = new Ember.RSVP.Promise(function(resolve, reject) {
window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
twttr.ready(function(twttr) {
Ember.run(function(){
self.twttr = twttr;
self.subscribeToTweetEvent();
resolve(twttr);
});
});
});
}
return twitterScriptPromise;
},
subscribeToTweetEvent: function() {
var tracking = this.tracking;
if(!tracking) { return; }
this._onTweet = function(ev) {
if(tracking.shared) { tracking.shared('twitter', ev); }
}
this._onClick = function(ev) {
if(tracking.clicked) { tracking.clicked('twitter', ev); }
}
this.twttr.events.bind('tweet', this._onTweet);
this.twttr.events.bind('click', this._onClick);
},
unsubscribeFromTweetEvents: function() {
if(this._onTweet) {
this.twttr.events.unbind('tweet', this._onTweet);
}
if(this._onClick) {
this.twttr.events.unbind('click', this._onClick);
}
},
willDestroy: function() {
this._super();
this.unsubscribeFromTweetEvents();
}
});