Skip to content

Instantly share code, notes, and snippets.

@dmarkow
Created March 28, 2014 20:52
Show Gist options
  • Save dmarkow/9842754 to your computer and use it in GitHub Desktop.
Save dmarkow/9842754 to your computer and use it in GitHub Desktop.
/* Put your CSS here */
html, body {
margin: 20px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ember Starter Kit</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.2.1.js"></script>
<script src="http://builds.emberjs.com/tags/v1.4.0/ember.js"></script>
<script src="http://builds.emberjs.com/beta/ember-data.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script src="http://dbushell.github.io/Pikaday/pikaday.js"></script>
<link rel="stylesheet" href="http://dbushell.github.io/Pikaday/css/pikaday.css">
</head>
<body>
<script type="text/x-handlebars">
<h2> Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<ul>
<li>Event Name: {{name}}</li>
<li>Event Date: {{date}}</li>
</ul>
{{view App.DateField date=date}}
<button {{action "today"}}>Set to Today</button>
</script>
</body>
</html>
App = Ember.Application.create();
App.Router.map(function() {
// put your routes here
});
App.Event = DS.Model.extend({
name: DS.attr("string"),
date: DS.attr("date")
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord("event", {name: "Party!", date: new Date()});
},
actions: {
today: function() {
this.set("controller.date", new Date());
}
}
});
App.DateField = Ember.TextField.extend({
picker: null,
updateValue: function() {
var date = moment(this.get("date"));
if (date.isValid()) {
this.set("value", date.format("L"));
this.get("picker").setDate(date.format("L"));
} else {
this.set("value", null);
}
}.observes("date"),
updateDate: function() {
var date = moment(this.get("value"));
if (date.isValid()) {
this.set("date", date.toDate());
} else {
this.set("date", null);
}
}.observes("value"),
didInsertElement: function() {
var picker = new Pikaday({
field: this.$()[0],
format: "MM/DD/YYYY"
});
this.set("picker", picker);
this.updateValue();
},
willDestroyElement: function(){
var picker = this.get("picker");
if (picker) {
picker.destroy();
}
this.set("picker", null);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment