Prometheus
Prometheus

Reputation: 33625

Dynamically pass urlRoot to backbone.js model

Using backbone.js I have a special case where I need to dynamically pass in the actual urlRoot to the model when creating it.

This is what I have tried with no luck:

 var reward = new Reward({urlRoot: campaign.get('url_created')});

 var Reward = Backbone.Model.extend({
       urlRoot: urlRoot
    });

Does Model.extend have any options for me to pass my url_created string. I have tried using ID option and leaving urlRoot blank but this will not work.

Upvotes: 1

Views: 728

Answers (1)

Andrew
Andrew

Reputation: 13853

You can specify a function that returns the urlRoot that will return the value, one way to pass it through is shown below,

var Reward = Backbone.Model.extend({
   urlRoot: function(){ return this.get('url_created') },
});

var reward = new Reward({'url_created': campaign.get('url_created')});

Upvotes: 3

Related Questions