Spine.js - How do you unbind a specific event handler from a model instance using JavaScript?

StackOverflow https://stackoverflow.com/questions/14457403

  •  17-01-2022
  •  | 
  •  

Frage

It's pretty easy to unbind specific event handlers from model classes, but it seems the only way to unbind an event handler from an instance of a model is to unbind ALL event handlers attached to that instance using unbind() (takes no arguments).

Is there an equivalent to Model class' unbind([eventName, function]) function for model instances, or is there another better way to unbind specific handlers without unbinding them all?

I've looked through the Spine.js documentation but no luck so far. Adding arguments to the unbind() function has no effect - it just ignores them and unbinds all anyway.

War es hilfreich?

Lösung

In the end we switched to a more suitable framework for our needs, Backbone.js. Maintenance on the Spine.js GitHub repository seems to have ground to a halt in September last year and, though Spine supports use with JavaScript, the documentation for it is pretty poor.

If anyone can provide a working solution to the original problem I'll still accept it for the benefit of anyone else who stumbles across this.

Andere Tipps

I don't think this specifically answers your question but might get you to what you are going for.

Spine has a not so documented .one() event

from the source:

one: (ev, callback) ->
  @bind ev, ->
    @unbind(ev, arguments.callee)
    callback.apply(this, arguments)

So it basically does the unbind for you behind the scenes, but it doesn't use a Model unbind which actually just triggers 'unbind'.

unbind: (ev, callback) ->
  unless ev
    @_callbacks = {}
    return this

  list = @_callbacks?[ev]
  return this unless list

  unless callback
    delete @_callbacks[ev]
    return this

  for cb, i in list when cb is callback
    list = list.slice()
    list.splice(i, 1)
    @_callbacks[ev] = list
    break
  this

vs.

  unbind: ->
    @trigger('unbind')

We use @item.one in a few places and have found it works okay.

example use:

@item.one 'awaitingPermit', (item) =>
  @navigate('/document', item.id, 'show')

update: We have worked on some solutions to this missing feature in Spine.js. see this issue thread https://github.com/spine/spine/issues/418

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top