JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
JavaScript
var MyModel = Backbone.Model.extend({
defaults: {
status: 0,
name: undefined
}
});
var MyCollection = Backbone.Collection.extend({
model: MyModel,
fetch: function(options) {
var self = this;
// Simulate a slow fetch
// This is not ideal, as Collection.fetch is still called
// straight away - but gives you the idea of events
setTimeout(function() {
self.reset([
{ name: "foo", status: 1 },
{ name: "moo", status: 1},
{ name: "not me!"}
]);
}, 2000);
}
});
var MyView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, "doSomething");
this.collection.on("reset", this.doSomething);
},
doSomething: function() {
console.log("Received on event trigger", this.collection.where({status: 1}));
}
});
var myCollection = new MyCollection();
var myView = new MyView({ collection: myCollection });
myCollection.fetch();
console.log("Will fail, data hasn't been received", myCollection.where({status: 1}));