So, I have a standard class that is extended by child classes. In some cases, I'm passing what child class to use through jQuery as a option instead of using the default standard class.
Example:
jQuery('img').myPlugin();
Calls
New mainClass(arg1, arg2)
While
jQuery('img').myPlugin({classToUse : 'myExtendedClass'});
Calls
New myExtendedClass(arg1, arg2)
Here's the code that works, but it is ugly in so many ways. How can we do this properly? Here @settings is a simple object, and @settings.classToUse is a string that's passed.
@NewClass = eval("new #{@settings.classToUse}(this.$element, this.settings)")
The following does not work and returns a string error:
@Framer = new @settings['classToUse'](this.$element, this.settings)
A clipped version of the full source code is below. Any errors in the logic are a result of clipping the code as the code in its current state is 100% functional.
You can also see the code compiled here.
class mainClass
constructor: (@framedObject, @options) ->
doSomething: ->
alert('something')
class myExtendedClass extends mainClass
doSomething: ->
alert('somethingDifferent')
class anotherExtendedClass extends mainClass
doSomething: ->
super
alert('Woah!')
$ ->
$.plugin = (element, options) ->
@settings = {classToUse : 'myExtendedClass'} #built by merging defaults and options
@init = ->
@mainClass = eval("new #{@settings.classToUse}(this.$element, this.settings)")
@init()
this
$.fn.mediaFrame = (options) ->
return this.each ->
plugin = new $.plugin this, options