Skip to content

Instantly share code, notes, and snippets.

@automagisch
Created June 23, 2015 08:19
Show Gist options
  • Save automagisch/f4dd050fb55a01252572 to your computer and use it in GitHub Desktop.
Save automagisch/f4dd050fb55a01252572 to your computer and use it in GitHub Desktop.
Basic Ember Date Selector
<div class="col-xs-2 nopadding">
<label for="date-day">Day</label>
{{view "select" content=view.days value=view.day classNames="form-control"}}
</div>
<div class="col-xs-7">
<label for="date-month">Month</label>
{{view "select" content=view.months optionValuePath="content.value" optionLabelPath="content.label" value=view.month classNames="form-control"}}
</div>
<div class="col-xs-3 nopadding">
<label for="date-month">Year</label>
{{view "select" content=view.years value=view.year classNames="form-control"}}
</div>
App.DateSelectionView = Em.View.extend({
templateName: "date-selection",
tagName: "div",
classNames: ['date-selector','clearfix'],
classNameBindings: ['showLabels::no-labels'],
date: null,
showLabels: true,
timezone: true,
until_year: 2030,
from_year: 1900,
month: null,
year: null,
day: null,
init: function() {
this._super();
},
years: function() {
var years = [],
until_year = this.get('until_year'),
from_year = this.get('from_year');
for(var year = from_year; year <= until_year; year++) {
years.push(year);
}
return years;
}.property(''),
months: function() {
return [
{ value: 0, label: 'January' },
{ value: 1, label: 'February' },
{ value: 2, label: 'March' },
{ value: 3, label: 'April' },
{ value: 4, label: 'May' },
{ value: 5, label: 'June' },
{ value: 6, label: 'July' },
{ value: 7, label: 'August' },
{ value: 8, label: 'September' },
{ value: 9, label: 'October' },
{ value: 10, label: 'November' },
{ value: 11, label: 'December' }
];
}.property(''),
days: function() {
var days = [];
for(var day = 0; day < 31; day++) {
days.push(day+1);
}
return days;
}.property(''),
parseDate: function() {
var date = moment(this.get('date'));
this.set('day', date.date());
this.set('month', date.month());
this.set('year', date.year());
}.on('init'),
writeDate: function() {
var day = this.get('day');
var month = this.get('month');
var year = this.get('year');
if(day && month && year) {
var mom = moment();
mom.date(day);
mom.month(month);
mom.year(year);
var d = mom.format();
if(!this.get('timezone')) {
d = d.split("+")[0];
}
this.set('date', d);
}
}.observes('day','month','year')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment