reco
reco

Reputation: 470

Spine.js relation, get next and previous of current lated item

i am building a photo album app with spine mobile. I am using spine.relation to model a Album Photo relationship.

class Album extends Spine.Model
  @configure 'Album', ....

  @hasMany "photos", 'models/photo'


class Photo extends Spine.Model
  @configure 'Photo', ....

Controller:

class PhotosShow extends Panel
  events:
    'tap .next' : 'next'
    'tap .prev' : 'prev'

  constructor: ->
    super

    Photo.bind 'change', @render
    @active @change


  render: =>
    return unless @item
    @html require('views/photos /show')(@item)

  change: (params) ->
    @item = Asset.find(params.id)
    @render()

  next: ->
    # show next photo

  prev: ->
    # show previous photo

I would like to have references to next and previous photo. for the next and prev functions. whats the best way to get those?

Upvotes: 0

Views: 208

Answers (1)

LnL7
LnL7

Reputation: 438

I think the best way to do that is to add a weight property to your model and use the @findByAttribute(name, value) method to find the model, then you can easily increase/decrease the weight value to get the next/previous.
Using a propery to define the order also gives you the ability to easily modify the order of your list.

Upvotes: 1

Related Questions