Overriding fetch in multiple spine models

Posted by Adam Charnock on Stack Overflow See other posts from Stack Overflow or by Adam Charnock
Published on 2012-11-20T16:56:40Z Indexed on 2012/11/20 16:59 UTC
Read the original article Hit count: 233

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about coffeescript

Related posts about spine.js