Overriding fetch in multiple spine models
- by Adam Charnock
I need to override Spine's @fetch() method in all of my Spine models. Currently I have code duplication as follows:
TastypieEndpointMixin =
fromJSON: (data) ->
return unless data
return Spine.Model.fromJSON(data.objects)
class App.models.Position extends Spine.Model
@configure 'Position', 'code', 'name'
@extend Spine.Model.Ajax
@extend TastypieEndpointMixin
@url: '/api/v1/position/?format=json'
validate: ->
'code is required' unless @code
@fetch: ->
defer = $.Deferred()
@one "refresh", ->
defer.resolve()
super
return defer
class App.models.Player extends Spine.Model
@configure 'Player', 'first_name', 'last_name', ...
@extend Spine.Model.Ajax
@extend TastypieEndpointMixin
@url: '/api/v1/player/?format=json'
@fetch: ->
defer = $.Deferred()
@one "refresh", ->
defer.resolve()
super
return defer
My question is: How can I create some form of parent class which contains @fetch()?
I know this should be a simple problem to solve. I have tried many options (including extending Spine.Model and Spine.Model.Ajax), but nothing works and I cannot seem to get my head around it.