Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Event-Driven Applications with Twitter Flight

kbingman
August 29, 2014

Building Event-Driven Applications with Twitter Flight

Flight doesn't have a flashy demos, but instead is a small modular framework for building highly decoupled applications

kbingman

August 29, 2014
Tweet

More Decks by kbingman

Other Decks in Technology

Transcript

  1. define(function (require) { ! var uiRequestImages = require('ui_request_images'); ! return

    initialize; ! function initialize() { uiRequestImages.attachTo('[data-input="search"]'); } ! });
  2. var component = require('flight').component; ! return component(requestTaggedImages); ! function requestTaggedImages()

    { this.attributes({ 'tagInput': 'input[name="tag"]', 'searchButton': '[data-button]' }); ! this.requestTag = function(e, data){ … }; ! this.after('initialize', function() { this.on('submit', this.requestTag); }); }
  3. this.before('initialize', function() { … }); this.around('teardown', function() { … });

    this.after('initialize', function() { this.on('click', this.doSomething)… });
  4. Data Component Attached the document itself Used for processing /

    persistence AJAX / localStorage Does not render anything
  5. define(function (require) { ! var uiRequestImages = require(‘data_provide_images’); ! return

    initialize; ! function initialize() { dataProvideImages.attachTo('document'); } ! });
  6. ! ! function dataInstagram() { ! this.attributes({ … }); !

    this.getImages = function(e, data){ … }; ! this.after('initialize', function() { this.on('needsTaggedImages', this.getImages); }); } !
  7. ui data request — uiNeedsStoredData ui user action — uiFollowAction

    ui request — uiCloseModal ui moment — uiColumnOptionsShown data — dataCalculateSystems http://www.simplebutgood.com/
  8. –Angus Croll “And on the 6th day, God created an

    abundance of Talking Animals, that they may be used in JavaScript inheritance examples.”
  9. Exploits commonalities This turns out to be incredibly flexible and

    the number one thing I was missing from backbone
  10. define(function(require) { ! var utils = require('flight/lib/index').utils; ! return withRequest;

    ! function withRequest() { this.request = function(path, options){ var xhr = { url: this.attr.baseUrl + path, }; xhr = utils.merge(xhr, options); return $.ajax(xhr); }; } ! });
  11. describeComponent('component_ui/ui_images', function () { beforeEach(function () { setupComponent(); this.component.trigger('uiShowTaggedImages', {

    meta: { code: 200 }, data: [mockdata] }); }); ! it('should add an image on <uiShowTaggedImages>', function () { expect(this.component.$node.find('img').length) .toEqual(1); }); ! it('should add the correct size to the img src', function () { expect(this.component.$node.find('img').attr('src')) .toEqual('/thumbnail.jpg'); }); });
  12. ! ! it('should trigger <needsImages>', function () { var eventSpy

    = spyOnEvent(document, 'needsImages'); var $input = this.component.$node.find(‘[data-tag]'); $input.val('gastown'); this.component.$node.submit(); ! expect(eventSpy).toHaveBeenTriggeredOn(document); }); !
  13. beforeEach(function () { var html = '<form data-input="tag">'; html +=

    '<input type="text" name="tag" />'; html += '<button>Search</button>'; html += '</form>'; ! setupComponent(html); });
  14. describeComponent('ui_request_images', function () { beforeEach(function () { var html =

    '<form data-input="tag">'; html += '<input type="text" name="tag" />'; html += '<button>Search</button>'; html += '</form>'; ! setupComponent(html); }); ! it('should be defined', function () { expect(this.component).toBeDefined(); }); ! });
  15. var Templates = require('templates'); var fixtures = require('text!fixtures/pages.json'); ! describeComponent('component/list_ui',

    function() { var spy; var pages = JSON.parse(fixtures); ! beforeEach(function() { spy = sinon.spy(); setupComponent(Templates['_canvas'].render({ pages: pages })); }); }); !
  16. load HTML first, then begin with API / single page

    behaviour Multiple points of entry Hybrid apps
  17. SEO

  18. Using events, can talk to backbone and jQuery applications with

    no real change to either Easily integrated