Skip to content

Instantly share code, notes, and snippets.

@bhserna
Forked from anonymous/jsbin.Ovuw.html
Created January 29, 2014 19:31

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 29, 2014.
    44 changes: 44 additions & 0 deletions jsbin.Ovuw.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <!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>
    96 changes: 96 additions & 0 deletions jsbin.Ovuw.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    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
    }
    ];