Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js collection options

I have written a model/view/collection using Backbone.js. My collection uses the fetch method to load the models from a remote server. The url required for this collection needs an id like: messages/{id}. But I have found no clean way to pass options to the Collection.

The backbone.js view accepts options by passing it on construction: view([options]), but the collection expects a list of models upon construction: collection([models]).

What is the "cleanest" way of passing parameters/options to this collection?

Shortened code:

var Messages = Backbone.Collection.extend({  model: Message,    url: 'http://url/messages/' + id }); 
like image 204
Jens Avatar asked Dec 11 '11 13:12

Jens


People also ask

What is collections in Backbone JS?

Advertisements. Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is the only method available in the backbone JS history?

There is only method named "start" can be used to manipulate the Backbone. js history.


2 Answers

@Paul's answer is good, but it's also worth noting that the url attribute can be a function. In my opinion (and it's just opinion, since the end result is the same), the code is a little more legible, if more verbose, if you set the id in initialize and the reference it in a function:

var Messages = Backbone.Collection.extend({   initialize: function(models, options) {     this.id = options.id;   },   url: function() {     return '/messages/' + this.id;   },   model: Message, });  var collection = new Messages([], { id: 2 }); collection.fetch(); 

Just to make sure, though - you aren't confusing the id here with the Model id, are you? @Paul's answer, and my code above, assume that you have multiple Messages collections, each with its own id. If the API path /messages/<id> actually refers to a message, not a set of messages, then you only need to set the url to /messages/ in the Collection, and Backbone will automatically use /messages/<id> for each model.

like image 162
nrabinowitz Avatar answered Oct 09 '22 03:10

nrabinowitz


The way you have the url property declared, the value will be determined once when the browser initially loads the javascript file, and 'id' will be undefined.

The Backbone.Collection accepts options as the second argument in its constructor, so you can pass the id value as an option, and then set the url value within the initialize function of the collection.

Here's an example

var Messages = Backbone.Collection.extend({   initialize: function(models, options) {     this.url = 'http://url/messages/' + options.id;   },   model: Message, });  var collection = new Messages([], { id: 2 }); collection.fetch(); 
like image 28
Paul Avatar answered Oct 09 '22 02:10

Paul