Tengo una enorme lista de tareas cargadas desde el principio.
Quiero mostrarlos dependiendo de la lista/buzón de entrada seleccionada, para que no haya cargas adicionales para cada lista.Backbone.js - Forma correcta de filtrar y mostrar los datos de la colección en una vista
window.Task = Backbone.Model.extend({});
window.TasksCollection = Backbone.Collection.extend({
model: Task,
url: '/api/tasks',
inbox: function() {
return this.filter(function(task) {
return task.get('list') == null;
});
},
list: function(id) {
return this.filter(function(task) {
return task.get('list') == id;
});
}
});
window.tasks = new TasksCollection;
window.TaskView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#item-template').html()),
initialize: function() {
_.bindAll(this, 'render', 'close');
this.model.bind('change', this.render);
this.model.view = this;
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
this.setContent();
return this;
},
});
window.TasksView = Backbone.View.extend({
el: '#todo-list',
collection: tasks,
initialize: function() {
_.bindAll(this, 'render');
this.collection.bind('reset', this.render);
this.collection.fetch();
},
render: function() {
var t = this;
$(t.el).html('');
this.collection.each(function(task) {
var view = new TaskView({ model:task });
$(t.el).append(view.render().el);
});
return this;
},
});
window.Nicetask = Backbone.Router.extend({
routes: {
'': 'inbox',
'/inbox': 'inbox',
'/list/:id': 'list',
},
initialize: function() {
_.bindAll(this, 'inbox', 'list');
window.tasksView = new TasksView;
},
inbox: function() {
tasks.reset(tasks.inbox());
},
list: function(id) {
tasks.reset(tasks.list(id));
}
});
Este código funciona, pero la función reset()
elimina otras tareas en la lista real de la colección de tareas. Y en otra ruta, la recolección de tareas está vacía.
¿Hay alguna manera razonable de lograr esto? gracias por cualquier idea
ps: columna vertebral novato
ACTUALIZACIÓN
Thx a @sled y @ibjhb para comentarios, aquí es fragmento de la solución de trabajo.
window.TasksView = Backbone.View.extend({
el: '#todo-list',
collection: Backbone.Collection.extend(),
initialize: function() {
_.bindAll(this, 'render', 'addOne', 'addAll');
this.collection.bind('add', this.addOne);
this.collection.bind('reset', this.render);
},
render: function(data) {
$(this.el).html('');
_.each(data, function(task) {
this.addOne(task);
}, this);
return this;
},
addOne: function(task) {
var view = new TaskView({ model:task });
$(this.el).append(view.render().el);
},
});
window.Nicetask = Backbone.Router.extend({
routes: {
'': 'inbox',
'/inbox': 'inbox',
'/today': 'today',
'/list/:id': 'list',
},
initialize: function() {
_.bindAll(this, 'inbox', 'today');
window.tasksView = new TasksView;
window.menuView = new MenuListView;
tasks.fetch();
},
inbox: function() {
tasksView.render(tasks.inbox());
},
today: function() {
tasksView.render(tasks.today());
},
list: function(id) {
tasksView.render(tasks.list(id));
}
});
estado saliendo de mi cabello durante horas. ¡gracias por la respuesta! – Chev
¿Usando Backbone Marionette tal vez? Eso resuelve muchos problemas. – Blacksonic
Hola Juraj, tengo una situación en la que obtengo un json tree y tengo que crear una colección solo cuando hacemos clic en los nodos secundarios. Por ej. Root1> child1> subchild1. Aquí tengo que mostrar primero los nodos raíz y solo al hacer clic en el nodo raíz 'Root1', se debe mostrar la lista secundaria 'child1' o se debe crear la colección secundaria. Acabo de encontrar tu situación algo similar. ¿Puedes arrojar algo de luz sobre esto? Gracias de antemano –