Backbone Live Collection End Point Fetch - cid overwrite

by iros

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js"></script>
<h1> Repos: </h1>
<div id="example_content"></div>

JavaScript

// A container for a repo object.
var Repo = Backbone.Model.extend({
	initialize: function() {
		this.cid = this.id;
	}
});

// A basic view rendering a single repo
var RepoView = Backbone.View.extend({
    tagName: "li",
    className: "repo",

    render: function() {

        // just render the repo name and description as the content of this element.
        $(this.el).html(
            '<b>' + this.model.get("name") + "</b> - " +    
            this.model.get("description"));
        
        return this;
    }
});

// A collection holding many repo objects.
// also responsible for performing the
// search that fetches them.
var Repos = Backbone.Collection.extend({
    model: Repo,
    initialize: function(models, options) {
        this.language = options.language;
    },
    url: function() {
        return "https://api.github.com/search/repositories?q=language:" + 
               this.language + "&sort=stars&order=desc";
    },
    parse: function(data) {

        // note that the original result contains repos inside of an items array, not at 
        // the root of the response.
        return data.items;
    }
});



// A rendering of a collection of tweets.
var ReposView = Backbone.View.extend({
    tagName: "ul",
    className: "repos",
    initialize: function(options) {
        // Bind on initialization rather than rendering. This might seem
        // counter-intuitive because we are effectively "rendering" this
        // view by creating other views. The reason we are doing this here
        // is because we only want to bind to "add" once, but effectively we should
        // be able to call render multiple times without subscribing to "add" more
        // than once.
        this.collection.bind("add", function(model) {

            var repoView = new RepoView({
                model: model
            });

            $(this.el).prepend(repoView.render().el);
        }, this);
    },
    render: function() {
        return this;
   ...