Mates I have the following:
App.Collections.Bookings = Backbone.Collection.extend({
url: 'bookings/',
model: App.Models.Booking,
howManyArriving: function() {
var bg = _.countBy( this.models, function(model) {
return model.get('indate') == moment().format('YYYY-MM-DD') ? 'even' : 'odd';
});
var lv = _.filter( this.models, function(model){
return model.get('indate') == moment().format('YYYY-MM-DD');
});
var r = {
count: bg,
models: lv
}
return r;
},
availableBtwn: function(bed,indate,outdate) {
var gf = _.filter(this.models, function(model){
return (
model.get('outdate') > outdate &&
model.get('indate') <= indate &&
model.get('id_bed') == bed
);
});
return gf;
},
getBooking: function(bed, date){
var gf = _.filter(this.models, function(model){
return (
model.get('outdate') > date &&
model.get('indate') <= date &&
model.get('id_bed') == bed
);
});
return gf;
},
getFullName: function(id){
var b = this.get(id);
return b.get('nombre') + ' ' + b.get('apellido');
}
});
I need to check when I populate the collection and when I add a single model if there's already an existing model with determined propperties equal to the model/s that i'm attempting to create. I've tried something like this:
App.Collections.Bookings.prototype.add = function(bookings) {
_.each( bookings, function(book){
var isDupe = this.any(function(_book) {
return _book.get('id') === book.id;
});
if (isDupe) {
//Up to you either return false or throw an exception or silently ignore
return false;
}else{
Backbone.Collection.prototype.add.call(this, book);
}
//console.log('Cargo el guest: ' + guest.get('id'));
}, this);
}
The thing is, it works, but when I populate the collection, it's not populated by App.Models.Booking, but with response's JSON.
Any idea?
Thanks a lot!