更改javascript对象中的类参数
我用的是Chris McCord的宝石 我的项目运作良好,除了与职位相关的小问题 在gem的coffeescript文件中更改javascript对象中的类参数,javascript,ruby-on-rails,coffeescript,Javascript,Ruby On Rails,Coffeescript,我用的是Chris McCord的宝石 我的项目运作良好,除了与职位相关的小问题 在gem的coffeescript文件中 class Sync.PartialCreator attributes: name: null resourceName: null authToken: null channel: null selector: null direction: 'append' refetch: false constr
class Sync.PartialCreator
attributes:
name: null
resourceName: null
authToken: null
channel: null
selector: null
direction: 'append'
refetch: false
constructor: (attributes = {}) ->
@[key] = attributes[key] ? defaultValue for key, defaultValue of @attributes
@$el = $("[data-sync-id='#{@selector}']")
@adapter = Sync.adapter
我需要将方向从append更改为prepend。
有什么方法可以改变这一点,否则我必须放弃创业板?有很多方法可以改变默认值
attributes={}
专门用于:
o = new Sync.PartialCreator(direction: 'prepend')
class Pancakes extends Sync.PartialCreator
attributes:
name: null
resourceName: null
authToken: null
channel: null
selector: null
direction: 'prepend'
refetch: false
o = new Pancakes
你必须在这里提供所有的东西。如果您有可用的$.extend
、..extend
或类似产品,则您可以说:
class Pancakes extends Sync.PartialCreator
attributes: _({}).extend(Sync.PartialCreator::attributes, direction: 'prepend')
o = new Pancakes
只替换方向
进行复制方向
:
class Pancakes extends Sync.PartialCreator
constructor: (args...) ->
super(args...) # Pass all the arguments up to the Sync.PartialCreator
@direction = 'prepend' # Force the direction
o = new Pancakes
或者只是摆弄一下想要的默认值:
class Pancakes extends Sync.PartialCreator
constructor: (args...) ->
args[0] ?= { }
args[0].direction ?= 'prepend' # Set the direction if one isn't supplied
super(args...) # Punt to the base class.
o = new Pancakes
Sync.PartialCreator
。在加载了Sync.PartialCreator
之后的某个地方,在尝试使用它之前,只需修补原型:
Sync.PartialCreator::attributes = 'prepend'
演示:@direction='prepend'不工作?