Skip to content

Instantly share code, notes, and snippets.

@kdimatteo
Created June 4, 2013 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kdimatteo/5707938 to your computer and use it in GitHub Desktop.
Save kdimatteo/5707938 to your computer and use it in GitHub Desktop.
Backbone-Forms implementation of Bootstrap DatePicker
/**
* 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)
});
@szymdzum
Copy link

Please, please could I see a working example on jsfiddle I would love to understand what's going on here.

@romdim
Copy link

romdim commented May 3, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment