Skip to content

Instantly share code, notes, and snippets.

@romdim
Forked from kdimatteo/datepicker.js
Last active October 5, 2018 21:34
Show Gist options
  • Save romdim/abfd7099d512e8eafb2edacb12eda4fa to your computer and use it in GitHub Desktop.
Save romdim/abfd7099d512e8eafb2edacb12eda4fa to your computer and use it in GitHub Desktop.
Backbone-Forms implementation of Bootstrap DatePicker
###*
# Bootstrap Datepicker for Backbone.Forms
#
# Quick editor to create a Bootstrap style datepicker (instead of multiple of dropdowns)
# @see: https://github.com/eternicode/bootstrap-datepicker/
# @usage: takes 5 optional schema options: format, minViewMode, autoclose, clearBtn and endDate
# Forked because I wanted to use only years as shown in the example below
schema:
MyDate:
type: "DatePicker"
title: "My Date"
options:
format : 'yyyy'
minViewMode : 'years'
endDate : String (new Date).getFullYear()
###
class Backbone.Form.editors.DatePicker extends Backbone.Form.editors.Text
previousValue: ''
events: 'hide': 'hasChanged'
hasChanged: (currentValue) ->
if currentValue != @previousValue
@previousValue = currentValue
@trigger 'change', this
render: ->
# Set initial configs
format = @schema.options?['format'] or 'dd/mm/yyyy'
minViewMode = @schema.options?['minViewMode'] or 'days'
autoclose = @schema.options?['autoClose'] or true
clearBtn = @schema.options?['clearBtn'] or true
endDate = @schema.options?['endDate'] or false
# Call the parent's render method
Backbone.Form.editors.Text::render.call @
# Then make the editor's element a datepicker.
@$el.datepicker
format : format
minViewMode : minViewMode
autoclose : autoclose
clearBtn : clearBtn
endDate : endDate
@
setValue: (value) ->
if value? and value != ''
@$el.val moment(value).format('YYYY')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment