Baseline Set Collection Attribute (for URL)

I need to pass the identifier to the collection for use in the url (e.g. / user / 1234 / projects.json), but I'm not sure how to do this, the example will be great.

The way to structure my application is to launch a collection of β€œusers”, which is pulled and visualized, then I want the user to click their β€œdocuments”, pull them from the server into a new collection and display them in a new view. The problem is getting the user ID in the document collection to provide the appropriate url for document.fetch ().


I think I have 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'; } 
+6
source share
3 answers

Your custom collection URL should be set to / user. After this set, your models should use this URL to do their magic. I believe (not quite positive) that if the model is in the collection, calling the url method will return / user /: id. This way all your typical REST-ish functions will be used in '/ user /: id'. If you are trying to do something with relationships (the user has a lot of documents), it’s like washing and repeating. So, for your collection of documents (which robs the user of the right?) You must set the URL "user_instance.url / documents".

To show a one-to-many relationship with the base model, you would do something like this (upgrade to base line 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 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') }); 
+7
source

Why do you need to override the collection URL property 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(); 
+5
source

I like the answer from Craig Monson, but for his work I needed to fix two things:

  • Binding a User URL Method Before Passing It to Documents
  • The return statement from the url function in Documents

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 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
source

Source: https://habr.com/ru/post/892996/


All Articles