Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Created May 11, 2012 13:55
Show Gist options
  • Save rlivsey/2659816 to your computer and use it in GitHub Desktop.
Save rlivsey/2659816 to your computer and use it in GitHub Desktop.
Extensive example of using previous/next items in CollectionView
MB.ChatItemView = Ember.Mixin.create({
tagName: "li"
layoutName: "templates/chat/item-layout"
classNameBindings: ["isSamePosterAsPrevious", "isFirstInDay", "isLastInDay"]
didInsertElement: ->
@checkNeighbours()
checkNeighbours: () ->
if prev = @get("previousMessageView")
oldNext = prev.cacheFor("nextMessage")
if this.get("content") != oldNext
prev.beginPropertyChanges()
prev.notifyPropertyChange("nextMessage")
prev.notifyPropertyChange("nextMessageView")
prev.endPropertyChanges()
if next = @get("nextMessageView")
oldPrevious = next.cacheFor("previousMessage")
if this.get("content") != oldPrevious
next.beginPropertyChanges()
next.notifyPropertyChange("previousMessage")
next.notifyPropertyChange("previousMessageView")
next.endPropertyChanges()
previousMessageView: (() ->
collection = @getPath("collectionView._childViews")
index = collection.indexOf(this)
return false if index == 0
collection[index-1]
).property()
nextMessageView: (() ->
collection = @getPath("collectionView._childViews")
index = collection.indexOf(this)
collection.objectAt(index+1) || false
).property()
previousMessage: (() ->
collection = @getPath("collectionView.content")
index = collection.indexOf(@get("content"))
return false if index == 0
collection.objectAt(index-1)
).property()
nextMessage: (() ->
collection = @getPath("collectionView.content")
index = collection.indexOf(@get("content"))
collection.objectAt(index+1) || false
).property()
isSamePosterAsPrevious: (() ->
return unless previousMessage = @get("previousMessage")
previousId = previousMessage.getPath("postedBy.id")
@getPath("content.postedBy.id") == previousId && @get("isSameDayAsPrevious")
).property("previousMessage")
isSameTimeframeAsPrevious: (() ->
return unless previousMessage = @get("previousMessage")
previousTime = previousMessage.get("postedAt")
@get("postedAtRounded").isEqual(roundedTime(previousTime))
).property("previousMessage")
isSameDayAsPrevious: (() ->
return unless previousMessage = @get("previousMessage")
currentDate = @getPath("content.postedAt").toTimezone(Ember.DateTime.timezone).toFormattedString("%y%m%d")
previousDate = previousMessage.get("postedAt").toTimezone(Ember.DateTime.timezone).toFormattedString("%y%m%d")
currentDate == previousDate
).property("previousMessage")
isSameDayAsNext: (() ->
return unless nextMessage = @get("nextMessage")
currentDate = @getPath("content.postedAt").toTimezone(Ember.DateTime.timezone).toFormattedString("%y%m%d")
nextDate = nextMessage.get("postedAt").toTimezone(Ember.DateTime.timezone).toFormattedString("%y%m%d")
currentDate == nextDate
).property("nextMessage")
isFirstInDay: (() ->
!@get("isSameDayAsPrevious")
).property("isSameDayAsPrevious")
isLastInDay: (() ->
@get("nextMessage") && !@get("isSameDayAsNext")
).property("nextMessage", "isSameDayAsNext")
postedAtRounded:(() ->
roundedTime(@getPath("content.postedAt"))
).property()
})
roundedTime = (time) ->
over = time.get("minute") % 5
rounded = if over == 0 then time else time.advance({minute: -over})
rounded.adjust({second: 0})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment