codecombat/app/models/LevelComponent.coffee

41 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2014-06-30 22:16:26 -04:00
CocoModel = require './CocoModel'
2014-01-03 13:32:13 -05:00
module.exports = class LevelComponent extends CocoModel
2014-06-30 22:16:26 -04:00
@className: 'LevelComponent'
@schema: require 'schemas/models/level_component'
@EquipsID: '53e217d253457600003e3ebb'
2014-06-30 22:16:26 -04:00
urlRoot: '/db/level.component'
2014-01-03 13:32:13 -05:00
set: (key, val, options) ->
if _.isObject key
[attrs, options] = [key, val]
else
(attrs = {})[key] = val
if 'code' of attrs and not ('js' of attrs)
attrs.js = @compile attrs.code
super attrs, options
2014-02-11 18:47:59 -05:00
onLoaded: ->
2014-01-03 13:32:13 -05:00
super()
@set 'js', @compile(@get 'code') unless @get 'js'
compile: (code) ->
if @get('codeLanguage') and @get('codeLanguage') isnt 'coffeescript'
2014-06-30 22:16:26 -04:00
return console.error('Can\'t compile', @get('codeLanguage'), '-- only CoffeeScript.', @)
2014-01-03 13:32:13 -05:00
try
js = CoffeeScript.compile(code, bare: true)
catch e
2014-06-30 22:16:26 -04:00
#console.log 'couldn\'t compile', code, 'for', @get('name'), 'because', e
2014-01-03 13:32:13 -05:00
js = @get 'js'
js
getDependencies: (allComponents) ->
results = []
for dep in @get('dependencies') or []
comp = _.find allComponents, (c) ->
c.get('original') is dep.original and c.get('version').major is dep.majorVersion
for result in comp.getDependencies(allComponents).concat [comp]
results.push result unless result in results
results