Vinay
Vinay

Reputation: 15

Instantiating a Collection Backbone

I have a router class which actually has separate hashes for views and collections as mentioned below. How do i set the url parameter of the collection when i take the instance in a view render method.

The Router Class

Router = (function() {
    'use strict';

    var
        viewHash = {},
        collectionsHash = {},
        EvtCalRouter, startRouter;

    // Set up the Backbone Router.
    // Evaluates URL with parameters and maps them to functions contained within the Router.
    // This enables us to do things like allow users to bookmark search results.
    // See "http://backbonejs.org/#Router" for more info.
    EvtCalRouter = Backbone.Router.extend({

        // Define the Routes we care about.
        // See "http://backbonejs.org/#Router-routes" for more info.
        routes: {
            "": "home",
            "route1": "route1"
        }
        buildSearchScreen: function() {
            collectionsHash['events'] = ESPN.apps.ADTG.EC.EventsCollection.newInstance({});

        },
        startRouter = function() {
            new EvtCalRouter();
            Backbone.history.start();
        };

        // Start routing functionality
        $(document).ready(startRouter);

        // For any module that needs to know...
        $(document).ready(function() {
            $(document).trigger(ESPN.apps.ADTG.EC.events.ecInit);
        });

        // Public API

        return {
            getCollection: function(name) {
                return collectionsHash[name] || {};
            }
        };

    })();

The Collection Class is defined like this

Collection = (function() {
    var Events = Backbone.Collection.extend({
        initialize: function(props) {
            this.url = props.url;
            alert(this.url);
        }
    });

    return {
        newInstance: function(options) {
            return new Events(options);
        }
    };
})();

Upvotes: 0

Views: 101

Answers (1)

Yurui Zhang
Yurui Zhang

Reputation: 2232

How do i set the url parameter of the collection when i take the instance in a view render method.

You should be able to pass an url in your options hash:

Collection.newInstance({url: "your url"});

BUT.

To initialize a collection, you need to pass an array of models, so you need to change your collection defintion:

Collection = (function(){

  var Events;

  Events = Backbone.Collection.extend({

    initialize: function(models, options){
      this.url = options.url;
      alert(this.url);
    }
  });

  return {
    newInstance : function(models, options) { return new Events(models, options); }
  };
})();

I'm not sure why you want to have a dynamic url for your collection though. :/ and you probably want to define which model the collection is for...but well, you can pass it thru options as well.

Collection.newInstance([
  //array of models...
], {
  url: "meh"
});

EDIT:

If you need dynamic urls but most part of it are still the same, you can define url as a function:

Events = Backbone.Collection.extend({
  url: function(urlOptions) {
    return 'yourDomain/resources?'+ urlOptions; // just return the url as a string
  }
});

jsfiddle example:

jsfiddle.net/sbjaz/13

Upvotes: 1

Related Questions