最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone set collection attribute (for the url) - Stack Overflow

programmeradmin2浏览0评论

I need to pass an id to a collection for use in the url (e.g. /user/1234/projects.json) but am not sure how to do this, an example would be wonderful.

The way my application is structured is on launch a collection of 'users' is pulled and rendered, I then want when a user is clicked their 'documents' are pulled from the server into a new collection and rendered in a new view. The issue is getting the user id into the documents collection to give the relevant URL for the documents.fetch().


think I've got it, here is an example:

  //in the the view initialize function     
  this.collection = new Docs();
  this.collection.project_id = this.options.project_id;
  this.collection.fetch();

  //in the collection
  url: function() {
     return '/project/api/' +this.project_id+'/docs';
  }

I need to pass an id to a collection for use in the url (e.g. /user/1234/projects.json) but am not sure how to do this, an example would be wonderful.

The way my application is structured is on launch a collection of 'users' is pulled and rendered, I then want when a user is clicked their 'documents' are pulled from the server into a new collection and rendered in a new view. The issue is getting the user id into the documents collection to give the relevant URL for the documents.fetch().


think I've got it, here is an example:

  //in the the view initialize function     
  this.collection = new Docs();
  this.collection.project_id = this.options.project_id;
  this.collection.fetch();

  //in the collection
  url: function() {
     return '/project/api/' +this.project_id+'/docs';
  }
Share Improve this question edited Jul 18, 2011 at 19:56 henry.oswald asked Jul 18, 2011 at 14:51 henry.oswaldhenry.oswald 5,43413 gold badges53 silver badges76 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

Your user collection url should be set to /user. Once that's set, your models should utilize that url in order to do their magic. I believe (not pletely positive) that if a model is in a collection, calling the 'url' method will return /user/:id. So all your typical REST-ish functionality will be utilized on '/user/:id'. If you are trying to do something with a relationship (a user has many documents) it's kind of rinse and repeat. So, for your documents collection (which belogs to user correct?) you'd set the url to 'user_instance.url/documents'.

To show a one to many relationship with a backbone model, you'd do something like this (upgrade to backbone 0.5.1 for urlRoot):

var User = Backbone.Model.extend({
    initialize: function() {
        // note, you are passing the function url.  This is important if you are
        // creating a new user that's not been sync'd to the server yet.  If you
        // did something like: {user_url: this.url()} it wouldn't contain the id
        // yet... and any sync through docs would fail... even if you sync'd the
        // user model!
        this.docs = new Docs([], {user_url: this.url});
    },
    urlRoot: '/user'
});

var Doc  = Backbone.Model.extend();

var Docs = Backbone.Collection.extend({
    initialize: function(models, args) {
        this.url = function() { args.user_url() + '/documents'; };
    }
});

var user = new User([{id: 1234}]);
user.docs.fetch({ success: function() { alert('win') });

Why do you need to override the URL property of the collection with a function?.. you could do:

 this.collection = new Docs();
 this.collection.project_id = this.options.project_id;
 this.collection.url = '/project/api/' + this.options.project_id + '/docs';
 this.collection.fetch();

I like the answer from Craig Monson, but to get it working I needed to fix two things:

  • Binding the User url method before passing it to the Docs
  • A return statement from the url function in Docs

Updated example:

var User = Backbone.Model.extend({
    initialize: function() {
        // note, you are passing the function url.  This is important if you are
        // creating a new user that's not been sync'd to the server yet.  If you
        // did something like: {user_url: this.url()} it wouldn't contain the id
        // yet... and any sync through docs would fail... even if you sync'd the
        // user model!
        this.docs = new Docs([], { user_url: this.url.bind(this) });
        },
        urlRoot: '/user'
    });

var Doc  = Backbone.Model.extend();

var Docs = Backbone.Collection.extend({
    initialize: function(models, args) {
        this.url = function() { return args.user_url() + '/documents'; };
    }
});

var user = new User([{id: 1234}]);
user.docs.fetch({ success: function() { alert('win') });
发布评论

评论列表(0)

  1. 暂无评论