dimanche 29 mars 2015

Creating multiple to-dos with Ember example

I'm trying to create multiple to-do lists using Ember example. I tried to extend it using this, but I'm having some trouble blending into the Ember to-do example.


I have TodosRoute and TodosIndexRoute making the to-do list work, and I'm creating model 'stack' to group individual todos.


Here's my router.js



Todos.Router.map(function() {

this.resource('todos', { path: '/todos' }, function () {
// additional child routes will go here later
this.route('active');
this.route('completed');
});
this.resource('stacks', {path: '/'}, function(){
this.route('stack', {path: '/stack/:stack_id'});
});
});




Todos.TodosRoute = Ember.Route.extend({
model: function() {
return this.store.find('todo');
}
});




Todos.TodosIndexRoute = Ember.Route.extend({
model: function() {
return this.modelFor('todos');
}
});




Todos.StacksRoute = Ember.Route.extend({
model: function() {
return this.store.find('stack');
}
});



Todos.StacksStackRoute = Ember.Route.extend({
model: function(params){
return this.store.find('stack', params.list_id);
}
});



Todos.TodosActiveRoute = Ember.Route.extend({
model: function(){
return this.store.filter('todo', function(todo) {
return !todo.get('isCompleted');
});
},
renderTemplate: function(controller) {
this.render('todos/index', {controller: controller});
}
});




Todos.TodosCompletedRoute = Ember.Route.extend({
model: function() {
return this.store.filter('todo', function(todo) {
return todo.get('isCompleted');
});
},
renderTemplate: function(controller) {
this.render('todos/index', {controller: controller});
}
});


And my stacks_controller.js



Todos.StacksController = Ember.ArrayController.extend({
actions: {
createNewStack: function() {
var stack = this.store.createRecord('stack');
var _id = stack.get('id');
stack.set('title', 'new stack ' + _id);
stack.save();
}
}
});


Model.js



Todos.Todo = DS.Model.extend({
title: DS.attr('string'),
isCompleted: DS.attr('boolean')
});



Todos.Stack = DS.Model.extend({
title: DS.attr('string'),
todos: DS.hasMany('todo')
});


And then some bits from template



<button {{action "createNewStack"}} class="btn btn-default navbar-btn navbar-left">+ stack</button></a>

<section id ="list">
{{#each}}
{{#link-to 'stack' this}}
<li>{{title}}</li>
{{/link-to}}
{{/each}}
</section>


It would be great if you could help me think about how I can structure the stack-todos relationship. I thought about making todos a child route of stack (and todo is already a child route of todos), then it would be a three layered relationship, which I don't know how to build.


Thanks a ton!





Aucun commentaire:

Enregistrer un commentaire