Backbonejs: Issue updating filtered collection after updating a model
I am having a terrible time trying to wrap my head around how to execute
the following: I am trying to update a filtered collection, but there's a
caveat.
To start, here's my MongoDB model schema:
var DocSchema = new Schema({
doc_id : String,
users: [String],
});
Here's what's happening on the backend:
The document saves if it does not exist in the database.
However if it already exists, say if another user already saved this
document to the DB, then the username is added to the users array and
saved without creating another instance in the db with this doc_id.
Here's relevant portion of the submission view:
var AddVideoView = Backbone.View.extend({
events: {
'click #videoSubmitButton': 'submit'
},
submit: function(event) {
event.preventDefault();
var newDoc = this.collection.create({
// $(...).vals() here
}, {wait: true});
}
});
What works: when the document does not exist in the DB, Backbone behaves
as expected. The collection and its view are updated. Here's the
collection view:
var DocsView = Backbone.View.extend({
tagName: 'ul',
initialize: function() {
this.collection.on('add', this.addOne, this);
},
render: function() {
this.options.filteredCollection.each(this.addOne, this);
return this;
},
addOne: function(doc) {
var docView = new DocView({ model: doc });
this.$el.append( docView.render().el );
},
});
And here's where the view gets instantiated:
var users_docs = docs.match_user('tom'); // Returns a new collection
var docsView = new DocsView({collection: docs, filteredCollection:
users_docs});
$('#allDocs').append( docsView.render().el );
So the problem is the submission view is not necessarily creating a new
entry all the time, it's possibly updating an entry that already exists.
With that said, how do I tell the filtered collection to update its data
and the collection view when a model is updated upon submission?
No comments:
Post a Comment