From 2240a61ac28ae8fc9b28c198ec175abd2f34fead Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 21 Dec 2015 00:47:39 -0500 Subject: [PATCH] Loosen coupling of FileLoader and window.FileReader --- scripts/game.js | 7 +++++-- src/FileLoader.js | 12 +++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/game.js b/scripts/game.js index 3b341a6..d2dde29 100644 --- a/scripts/game.js +++ b/scripts/game.js @@ -24,8 +24,11 @@ /* jshint browser:true */ /* global lasertank */ -const load_ltg = lasertank.FileLoader( document.getElementById( 'ltg' ) ), - load_lvl = lasertank.FileLoader( document.getElementById( 'lvl' ) ), +const ltg_input = document.getElementById( 'ltg' ), + lvl_input = document.getElementById( 'lvl' ), + + load_ltg = lasertank.FileLoader( ltg_input, new window.FileReader() ), + load_lvl = lasertank.FileLoader( lvl_input, new window.FileReader() ), ele_game = document.getElementById( 'game' ), ctx = document.getElementById( 'render' ).getContext( '2d' ); diff --git a/src/FileLoader.js b/src/FileLoader.js index 52bbc6b..ba63145 100644 --- a/src/FileLoader.js +++ b/src/FileLoader.js @@ -49,8 +49,9 @@ module.exports = Class( 'FileLoader', * Initialize file loader, monitoring the given file element * * @param {HtmlInputElement} element file element to monitor + * @param {FileReader} reader file reader */ - __construct: function( element ) + __construct: function( element, reader ) { if ( element.type !== 'file' ) { @@ -58,6 +59,7 @@ module.exports = Class( 'FileLoader', } this._element = element; + this._reader = reader; }, @@ -105,18 +107,18 @@ module.exports = Class( 'FileLoader', if ( files.length === 0 ) return; - const reader = new FileReader(); - reader.onload = function( revent ) + this._reader.onload = function( revent ) { _self._callback.call( this.__inst, null, revent.target.result ); }; - reader.onerror = function( e ) + + this._reader.onerror = function( e ) { _self._callback.call( this.__inst, e ); }; // load file - reader.readAsBinaryString( files[ 0 ] ); + this._reader.readAsBinaryString( files[ 0 ] ); } } );