Created
November 30, 2012 18:54
-
-
Save ivan-hilckov/4177731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Модуль инициализирующий приложение | |
| window[ "Elections" ] = Elections = window[ "Elections" ] or {} | |
| class window[ "Elections" ][ "Loader" ] | |
| constructor: -> | |
| # расширяем будущее приложение событиями из Backbone | |
| @app = _.extend {}, window[ "Backbone" ][ "Events" ] | |
| @dfd = $.Deferred() | |
| init: ( queue, settings ) -> | |
| # получаем настройки из хмурого | |
| settings.get ( data ) => @getFirstChunk( @filter( queue, data ) ) | |
| return @dfd.promise() | |
| _banModules: ( queue , modulesName ) -> | |
| # FIXME REWIE блиy как же мне не нравиться этото тройной фор аж искупаться охото | |
| for modulName in modulesName | |
| for chunk in queue when _.where( chunk, name: modulName ).length | |
| for modul in chunk | |
| if modul.name is modulName | |
| modul.load = false | |
| break | |
| return queue | |
| filter: ( queue, settings ) -> | |
| acl = settings.acl | |
| page = settings[ "page" ][ Elections.page ] | |
| ban_modules = page.defult | |
| if acl.allow_login is false | |
| ban_modules = page.allow_login | |
| else if acl.allow_registration is false | |
| ban_modules = page.allow_registration | |
| if acl.allow_openstat is false | |
| ban_modules = _.union ban_modules, page.allow_openstat | |
| return @_banModules queue, ban_modules | |
| getFirstChunk: ( queue ) -> | |
| if queue.length | |
| @initChunk( ( _.first queue ), ( => @getFirstChunk _.rest( queue )) ) | |
| else | |
| @finish() # если модулей нет, то заканчиваем инициализацию | |
| initChunk: ( chunk, callback ) -> | |
| dfds = [] | |
| for modul in chunk when modul.load | |
| @app[ modul.name ] = new modul.nameClass( @app ) # создаем экземпляр модуля | |
| @app[ modul.name ].name = modul.name | |
| returns = @app[ modul.name ].load() if @app[ modul.name ]?.load? # если модуль асинхронный просим его загрузить данные | |
| if returns? and _.isObject( returns ) and _.isFunction( returns.always ) # сохраняем обещание модля | |
| dfds.push returns | |
| $.when( dfds... ).done( callback ) | |
| finish: -> | |
| # сообщяем внутрь приложения что приложение загрузилось | |
| @app.trigger "ready" | |
| # возвращяем наружу наш обект с модулями | |
| @dfd.resolve( @app ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment