all files / src/ selectedModel.cjsx

0% Statements 0/21
0% Branches 0/2
0% Functions 0/8
0% Lines 0/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                                                                                                   
 
React = require('react')
Backbone = require('backbone')
ContextualData = require('./contextualData')
 
# see ./selectedModel.md
module.exports = class SelectedModel extends ContextualData
  @displayName: "react-datum.SelectedModel"
 
  # this class also supplies a "model" context to it's children that is
  # the selected model in the collection we recieve as a prop or context
  dataType:  Backbone.Model
  # this is the key in @context children should use to access thing
  contextKey: 'model'
 
  # we don't need any of the base propTypes and our collection prop can also be
  # given as a context arg
  @proptypes:
    collection: React.PropTypes.instanceOf(Backbone.Collection)
    placeholder: React.PropTypes.node  # anything react can render
 
  @contextTypes:
    collection: React.PropTypes.instanceOf(Backbone.Collection)
 
  @childContextTypes:
    model: React.PropTypes.instanceOf(Backbone.Model)
 
 
  # extends super: renders placeholder if provided or blank when
  # there is no selected model
  renderContent: ->
    superContent = super
    if @state.collectionOrModel?
      return superContent
      
    return <div className="large-placeholder">{@props.placeholder}</div>
 
 
  # extends - also need to consider our inbound context collection may have changed
  needsReinitializing: () ->
    truth = super() || @context.collection != @_lastContextCollection
    @_lastContextCollection = @context.collection
    return truth;
 
  ###
    override - We are going to provide a 'model' context (contextKey), but we listen to a 
    collection
  ###
  getInputCollectionOrModel: () ->
    @props.collection || @context.collection
 
 
  # override - @collectionOrModel should be the selected model in the collection
  getCollectionOrModelToProvide: () ->
    collection = @props.collection || @context.collection 
    return collection?.getSelectedModels?()[0]
 
 
  # extends - in addition to listening to our selected model events, listen for
  # selections changed on collection
  bindEvents: (model) ->
    super
    @getInputCollectionOrModel()?.on "selectionsChanged", @_onSelectionsChanged
 
 
  unbindEvents: () ->
    super
    @getInputCollectionOrModel()?.off "selectionsChanged", @_onSelectionsChanged
 
 
  _onSelectionsChanged: =>
    @setCollectionOrModel()
    @forceUpdate()