Created
June 4, 2013 17:43
-
-
Save kdimatteo/5707938 to your computer and use it in GitHub Desktop.
Backbone-Forms implementation of Bootstrap DatePicker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Bootstrap Datepicker | |
* | |
* Quick editor to create a Bootstrap style datepicker (instead of multiple of dropdowns) | |
* @see: https://github.com/eternicode/bootstrap-datepicker/ | |
* @usage: takes 2 schema options, dateFormat and defaultValue | |
schema: { | |
MyDate: { | |
type: "BackboneDatepicker", | |
title: "My Date", | |
options: [ | |
{ dateFormat: 'd/m/yyyy', defaultValue: new Date().getDate() + "/" + (new Date().getMonth() + 1) + "/" + new Date().getFullYear() } | |
] | |
} | |
} | |
*/ | |
Form.editors.BackboneDatepicker = Form.editors.Base.extend({ | |
previousValue: '', | |
events: { | |
'hide': "hasChanged" | |
}, | |
hasChanged: function(currentValue) { | |
if (currentValue !== this.previousValue){ | |
this.previousValue = currentValue; | |
this.trigger('change', this); | |
} | |
}, | |
initialize: function(options) { | |
Form.editors.Base.prototype.initialize.call(this, options); | |
this.template = options.template || this.constructor.template; | |
}, | |
render: function(){ | |
var $el = $($.trim(this.template({ | |
dateFormat: this.schema.options[0]["dateFormat"], | |
value: this.schema.options[0]["defaultValue"] | |
}))); | |
this.setElement($el); | |
return this; | |
}, | |
}, { | |
// STATICS | |
template: _.template('<div class="input-append">\ | |
<input type="text" id="start_date" name="start_date" data-date-format="<%= dateFormat %>" class="datepicker input input-medium" readonly="readonly" value="<%= value %>">\ | |
<span class="add-on"><i class="icon-calendar"></i></span></div>', null, Form.templateSettings) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just forked this over here: https://gist.github.com/romdim/abfd7099d512e8eafb2edacb12eda4fa
I use coffeescript and had to make it to use it with years only for a project. @Asmodeus8 ping me if you want for any questions.