Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js binding collection to models after a fetch using ajax

Tags:

backbone.js

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?

like image 599
sf. Avatar asked Jun 22 '11 16:06

sf.


People also ask

Which of the following is the correct syntax for creating backbone collection with model model?

The Backbone. js collection models specify the array or models which are created inside of a collection. Syntax: collection.

Does backbone need jQuery?

You can use the Backbone. Model without jQuery, but Backbone.

Which function has to be used when you want to trigger the event only once before being removed?

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.

What is backbone JS?

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 , ...

Why use jQuery Deferreds in backbone model bind methods?

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.

How do I sync data between backbone collections and models?

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.

How to set the library backbone uses for DOM manipulation and Ajax?

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.


1 Answers

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;
    }
});   
like image 194
Elf Sternberg Avatar answered Sep 21 '22 15:09

Elf Sternberg