How to set a Collection's url

2019-02-03 00:21发布

let's say I have :

var Book = Backbone.Model.extend();

var Collection = Backbone.Collection.extend({
  model: Book,
  url: '/books',
  initialize: function(){
    this.fetch();
  })
})

How can I change the Collection's url when instantiating a new collection ?

var AdventureBooks = new Books({ url: '/books/adventure' }) does not work

var AdventureBooks = new Books({ category: 'adventure' })

and in the Collection definition:

url : '/books/' + this.category does not work either.

Thanks.

标签: backbone.js
8条回答
时光不老,我们不散
2楼-- · 2019-02-03 00:42

The following should work:

var AdventureBooks = new Books();
AdventureBooks.url = '/books/adventure';
查看更多
爷的心禁止访问
3楼-- · 2019-02-03 00:45

I know that this a late reply, but I had a similar although slightly more complicated situation, and the selected answer didn't really help me.

I have a Conditions collection, and each Experiment model has multiple conditions, and I needed my url to be /api/experiments/:experimentId/conditions, but I didn't know how to access the experimentId from the empty Conditions collection.

In my Conditions collection url function, I did a console.log(this.toJSON()) and discovered that Backbone inserts a single dummy model in the empty collection with whatever attributes you passed in at it's creation time.

so:

var Conditions = new ConditionsCollection({
  experimentId: 1
});

I somehow doubt that this would be considered a best practice, hopefully someone else will respond with a better solution, but here's how I defined my Collection:

var ConditionsCollection = Backbone.Collection.extend({
  model: Condition,
  url:  function(){
    var experimentId = this.at(0).get("experimentId");
    return "/api/experiments/" + experimentId + "/conditions";
  }
});
查看更多
太酷不给撩
4楼-- · 2019-02-03 00:51

The best solution for me is the initialize method, look at this example:

Entities.MyCollection = Backbone.Collection.extend({
    model: Entities.MyModel,
    initialize: function(models,options) { 
        this.url = (options||{}).url || "defaultURL";
    },
}

use it as follows:

var items = new Entities.MyCollection();                     //default URL
var items = new Entities.MyCollection([],{url:'newURL'});    //changed URL
查看更多
Melony?
5楼-- · 2019-02-03 00:54

This work for me (tested with backbone 1.2.1):

var serverData = Backbone.Collection.extend({
url: function() {
    return '//localhost/rest/' + this.dbname;
},
constructor: function(a) {
    if(a.dbname){
        this.dbname = a.dbname;
    }
    Backbone.Collection.apply(this, arguments);
}
});

use it as follows:

var users = new serverData({dbname : 'users'});
查看更多
家丑人穷心不美
6楼-- · 2019-02-03 00:58

Like Daniel Patz pointed out , the problem lies in how you're instantiating the collection. I just struggled with this for a bit right now, so I thought I'd update this, even though the question is somewhat old.

The first argument is expected to be an array of models, with the options coming after. This should work:

var AdventureBooks = new Books([], { url: '/books/adventure' })

If you want a dynamic URL, then Raynos' answer might be the way to go.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-02-03 01:02

For some reason the parameters passed to Collection constructor (for example "url") are not set to the object. The collection uses only few of those (model and comparator).

If you want to pass the url via constructor you need to create initialize method that copies the necessary parameters to the object:

var Book = Backbone.Model.extend({
    initialize: function(props) { 
        this.url = props.url;
    }
}
var book = new Book({url: "/books/all"});
查看更多
登录 后发表回答