On Github benvinegar / backbone-at-disqus-talk
Ben Vinegar / @bentlegen
post•mor•tem – an analysis or discussion of an event after it is over
45 pages in and I’m pretty sure Third Party JavaScript should be required reading for all client-side js devs.
— Rebecca Murphey (@rmurphey) April 27, 2013(function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://disqus.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild(dsq); })();
<div class="disqus-collapse"> <button>Collapse post<button> </div>
/* Us */ #disqus-container .disqus-collapse { /* ... */ } /* Them */ #our-container #disqus-container .disqus-collapse { display: none !important; }
// Them Array.prototype.push = function (val) { this[this.length] = val; } // Us var someArray = []; someArray.push(2, 3); console.log(someArray); // => [2]
Backbone.$ = root.jQuery || root.Zepto || root.ender || root.$;
document.addEventListener('click', function () { var msg = { event: 'window.clicked' }; iframe.contentWindow.postMessage( JSON.stringify(msg), '//disqus.com' // Target origin ) }, false);
window.onmessage = function (evt) { var msg = JSON.parse(evt.data); switch (msg.name) { case 'window.clicked': /* ... */ break; case 'window.scroll': /* ... */ break; case 'window.hashchange': /* ... */ break; } }
var Bus = Backbone.extend({}, Backbone.Events); window.onmessage = function (evt) { var msg = JSON.parse(evt.data); Bus.trigger(msg.name, msg.data); }
var UserMenu = Backbone.View.extend({ initialize: function () { this.listenTo(Bus, 'window.click', this.closeMenu); }, closeMenu: function () { ... }, // ... rest of view definition });
var posts = new PostCollection([ { 'id': '1375382', 'message': 'What a great article, Ben!' 'author': { 'username': 'benvinegar', 'displayName': 'Ben Vinegar' } }, { 'id': '1375387', 'message': 'Great show old boy.' 'author': { 'username': 'benvinegar', 'displayName': 'Ben Vinegar' } }, ]);
session.login(); // => 'benvinegar' session.get('displayName'); // => 'Ben "Salt N" Vinegar' posts .get('1375382') .get('author') .get('displayName'); // => 'Ben Vinegar' :(
var UniqueUser = Backbone.UniqueModel(User); var first = new UniqueUser({ username: 'benvinegar', name: 'Ben Vinegar' }); var second = new UniqueUser({ username: 'benvinegar', name: 'Ben "Salt N" Vinegar' }); first === second; // => true first.get('name') === 'Ben "Salt N" Vinegar';
// Logged out (anonymous) this.session = new AnonUser(); // Logged in this.session = new User(); // Single sign-on this.session = new SingleSignOnUser();
var UserMenu = Backbone.View.extend({ initialize: function (options) { this.session = options.session; this.listenTo(Bus, 'window.click', this.closeMenu); this.listenTo(session, 'change', this.render); }, closeMenu: function () { ... }, // ... rest of view definition }); // Elsewhere this.session = new User();
var Session = Backbone.Model.extend({ initialize: function () { this.set(new AnonUser()) }, set: function (user) { this.clear(); this.user = user; this.user.on('all', this.proxy, this); }, clear: function () { this.user.off('all', this.proxy, this); this.user = null; }, proxy: function (/* event args */) { this.trigger.apply(this, arguments); } }