Sha256: 991d65b5ff1cb9d5e8885aa75bf4fd3dfeb69d6fdf1ad74380c10f66779fad73

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

class Lanes.Component.ChoicesInput extends Lanes.Component.Base

    session:
        selections: 'collection'

    subViewOptions: ->
        { field_name: @field_name, mappings: @mappings }

    constructor: (options={})->
        if options.has_many
            @association = options.model.associations[options.has_many]
            @field_name  = @association.fk
        else
            @field_name = options.field_name || options.subViewId

        @mappings = _.extend({
            id: 'id', title: 'title', selected: 'selected'
        },options.mappings||{})
        super


    initialize: (options={})->
        if @association
            collection = Lanes.Data[@association.model].sharedCollection()
            collection.fetch().then (m)=>
                @selections = m
                this.on('change:model', this.onModelChange )
        else if options.data
            @selections = options.data

    selectionForID: (id)->
        q={}; q[@mappings.id]=id
        @selections.findWhere( q )

    onAttributeChange: (model,fkid)->
        this.select( this.selectionForID( fkid ) )

    onModelChange: ->
        if (old_model = this.previousAttributes()['model'])
            this.stopListening(old_model,"change:#{@association.fk}", this.onAttributeChange )
        this.listenTo(@model,"change:#{@association.fk}", this.onAttributeChange )
        fk_id = @model.get( @association.fk )
        record = if fk_id
            this.selectionForID( fk_id )
        else
            @model[@association.name]
        this.select(record)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lanes-0.0.8 client/lanes/components/ChoicesInput.coffee
lanes-0.0.5 client/lanes/components/ChoicesInput.coffee
lanes-0.0.3 client/javascripts/component/ChoicesInput.coffee
lanes-0.0.2 client/javascripts/component/ChoicesInput.coffee
lanes-0.0.1 client/javascripts/component/ChoicesInput.coffee