javascript - How to call New variable Class Name in coffeescript without eval -
so, have standard class extended child classes. in cases, i'm passing child class use through jquery option instead of using 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 code works, ugly in many ways. how can properly? here @settings simple object, , @settings.classtouse string that's passed.
@newclass = eval("new #{@settings.classtouse}(this.$element, this.settings)")
the following not work , returns string error:
@framer = new @settings['classtouse'](this.$element, this.settings)
a clipped version of full source code below. errors in logic result of clipping code code in current state 100% functional.
you can see 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 merging defaults , options @init = -> @mainclass = eval("new #{@settings.classtouse}(this.$element, this.settings)") @init() $.fn.mediaframe = (options) -> return this.each -> plugin = new $.plugin this, options
how like:
class border constructor: (@options) -> class redborder extends border constructor: (@options) -> class blueborder extends border constructor: (@options) -> borders = default: border red: redborder blue: blueborder $.plugin = (element, options) -> @mainclass = new borders[@settings.class || 'default'](@settings)
Comments
Post a Comment