How to create dynamic url in collection and model using trunk

My collection and model is like this:

detail_userid = 0; detail_contactid = 0; var ContactDetail = Backbone.Model.extend({ urlRoot: URL_CONTACTS1+detail_userid+"/"+detail_contactid }); var ContactDetailCollection = Backbone.Collection.extend({ model: ContactDetail, url: URL_CONTACTS1+detail_userid+"/"+detail_contactid }) 

Input:

 ContactDetailManagePageModel.prototype.init = function(m,n){ detail_userid = m; detail_contactid = n; var myContactDetails = new ContactDetailCollection(); var contactDetailListView = new ContactDetailListView({ collection: myContactDetails }); myContactDetails.fetch({reset:true}); } 

But when it starts, the url is: http://localhost:8080/ws/users/contacts/0/0 0/0, which means that the assignment of detail_userid and detail_contactid were unsuccessful, I donโ€™t know why.

I am hope for your help. Thanks.

+7
source share
3 answers

I think you statically define the urlRoot and url properties before you start the init pageModel pages (not quite sure where you get m and n from, though ...)

Both url and urlRoot can be a function, so you can pass parameters during instance creation and dynamically set them on the model.

A simple example defining a collection and then creating one

 var ContactDetailCollection = Backbone.Collection.extend({ model: ContactDetail, url: function(){ return URL_CONTACTS1 + this.options.detail_userid + "/" + this.options.detail_contactid; } }); var myContactDetails = new ContactDetailCollection({ detail_userid: foo, detail_contactid: bar }); 

As I said, I'm not sure what your init function does, I assume that this is something non-standard from your application that I need not worry about.

I am sure that the main thing to remove is to set a dynamic url and urlRoot

+13
source

I would follow the accepted answer with a few comments.

  • The first parameter when initializing Backbone.Collection is an array of models , then . To create an empty collection with parameters, you must do the following

    var c = new Backbone.Collection( null , {opt1: val1, opt2: val2} );

  • In fact, you cannot access this.options in the url , bec function. There are no such options as in the model. What you can do is assign the required properties from the parameters during initialization.


 initialize: function (models, options) { // `parseInt()` is used for consistency that `id` is numeric, just to be sure this.detail_userid = parseInt(options.detail_userid); this.detail_contactid = parseInt(options.detail_contactid); } 

You can access them later as follows:

 url: function() { return URL_CONTACTS1 + this.detail_userid + "/" + this.detail_contactid; } 
+7
source

I wanted to use haref HATEOAS from one model to retrieve data from another model. It worked to simply set the url on the newly created collection, and not immediately define it in the constructor.

 var DailyMeasuresCollection = Backbone.Collection.extend({ //url : set dynamically with collection.url = url model : DailyMeasuresModel, parse : function(data) { return data._embedded.dailyMeasures; } }); var DailyMeasuresTopicListItemView = Backbone.View.extend({ //... events : { 'click .select-topic' : 'onClick' }, onClick : function() { var topicMeasures = new DailyMeasuresCollection() topicMeasures.url = this.model.attributes._links.measures.href // <- here assign var topicMeasuresView = new DailyMeasuresListView({ collection : topicMeasures }); topicMeasures.fetch() } }); 
0
source

All Articles