Skip to content

Instantly share code, notes, and snippets.

Created January 29, 2014 19:31
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 anonymous/8695139 to your computer and use it in GitHub Desktop.
Save anonymous/8695139 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Ember.js • TodoMVC</title>
</head>
<body>
<script type="text/x-handlebars" data-template-name="todos">
<h1>Todos</h1>
{{input type="text" id="new-todo" value=newTodo action="create"}}
<ul>
{{#each}}
{{render 'todo' this}}
{{/each}}
</ul>
</script>
<script type="text/x-handlebars" data-template-name="todo">
<li>{{title}}
<ul>
{{#each comments}}
<li>{{body}}</li>
{{/each}}
</ul>
{{input type="text" value=newComment action="createComment"}}
</li>
</script>
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/jquery.min.js"></script>
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/handlebars.js"></script>
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/ember.js"></script>
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/ember-data.js"></script>
</body>
</html>
window.Todos = Ember.Application.create();
Todos.ApplicationAdapter = DS.FixtureAdapter.extend();
Todos.Router.map(function () {
this.resource('todos', { path: '/' });
});
Todos.Todo = DS.Model.extend({
title: DS.attr('string'),
isCompleted: DS.attr('boolean'),
comments: DS.hasMany('comment')
});
Todos.Comment = DS.Model.extend({
body: DS.attr('string'),
todo: DS.belongsTo('todo')
});
Todos.TodosRoute = Ember.Route.extend({
model: function () {
return this.store.find('todo');
}
});
Todos.TodosController = Ember.ArrayController.extend({
actions: {
create: function() {
var title = this.get('newTodo');
if (!title.trim()) { return; }
// Create the new Todo model
var todo = this.store.createRecord('todo', {
title: title,
isCompleted: false
});
// Clear the "New Todo" text field
this.set('newTodo', '');
// Save the new model
todo.save();
}
}
});
Todos.TodoController = Ember.ObjectController.extend({
newComment: '',
actions: {
createComment: function() {
var body = this.get('newComment');
if (!body.trim()) { return; }
console.log(this.get('model.title'));
var comment = this.store.createRecord('comment', {
body: body,
todo: this.get('model')
});
this.set('newComment', '');
comment.save();
}
}
});
Todos.Todo.FIXTURES = [
{
id: 1,
title: 'Learn Ember.js',
isCompleted: true,
comment: [1, 2]
},
{
id: 2,
title: '...',
isCompleted: false
},
{
id: 3,
title: 'Profit!',
isCompleted: false
}
];
Todos.Comment.FIXTURES = [
{
id: 1,
body: 'hola',
trip: 1
},
{
id: 2,
body: 'dos',
trip: 1
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment