I'm trying to learn backbone.js and I'm having trouble understanding how to bind models and read them after a fetch. This is my code:
$(function() {
var Bid = Backbone.Model.extend();
var BidsList = Backbone.Collection.extend({
model: Bid,
url: '/buyers/auction/latestBids?auctionId=26&latestBidId=0',
});
var BidsView = Backbone.View.extend({
el: $('#bids'),
initialize: function() {
log('hi');
_.bindAll(this, 'render');
this.collection = new BidsList();
this.collection.fetch();
this.render();
},
render: function() {
log(this.collection);
return this;
},
});
var bidsView = new BidsView();
});
function log(m) { console.log(m); }
This is what the webservice json looks like
{
"AuctionState":3,
"ClosedOn":null,
"Bids":[
{
"BidId":132,
"AuctionId":26
},
{
"BidId":131,
"AuctionId":2
}
]
}
How do I would I bind that response to the model?
The Backbone. js collection models specify the array or models which are created inside of a collection. Syntax: collection.
You can use the Backbone. Model without jQuery, but Backbone.
js Event once() The event once method is just like event on method but it causes the bound callback to only fire once before being removed.
Backbone.js. Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface. The project is hosted on GitHub , ...
Here is a JsFiddle of all three Backbone Model fetch () bind methods to play with. Using jQuery Deferreds in Backbone is great for binding single callbacks that depend on multiple Models loading. It is much cleaner than chaining a series of callbacks, and allows the load events to run in parallel.
Backbone Collections and Models can be set up to asynchronously load data from the server with the fetch () method, which fires off an Ajax sync call. You can bind a callback method to a Collection’s reset event, which Backbone provides.
To set what library Backbone uses for DOM manipulation and Ajax calls, use Backbone.$ = ... instead of setDomLibrary . Removed the Backbone.wrapError helper method. Overriding sync should work better for those particular use cases.
You need to override the parse() method on your BidCollection to pull the Bids out and present them, and them only, to the collection's add() routine. You can do other things with the parse() method to manage the AuctionState field.
You also need to listen for 'change' events in your view, so the view automatically updates after the fetch. You shouldn't need to call render() in your view; you should bind the model's 'change' event to to render(), then fetch the data and let that trigger the render.
As always, Backbone's source code is highly readable. I recommend learning and understanding it.
For example:
var BidsList = Backbone.Collection.extend({
model: Bid,
url: '/buyers/auction/latestBids?auctionId=26&latestBidId=0',
parse: function(response){
return response.Bids;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With