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

javascript - Backbone.js with a custom fetch URL - Stack Overflow

programmeradmin3浏览0评论

I am trying to set up a variant fetch method on my backbone model that will fetch the current model for a given user. This is available from the API on /api/mealplans/owner/{username}/current.

I have written the following model. I commented out the URL Root, as the prototype fetch call was simply using the urlRoot and I wanted to see if that was overriding the url parameter I passed in portions somehow.

var mealPlan = Backbone.Model.extend({
  name: 'Meal Plan',
  //urlRoot: '/api/mealplans',
  defaults: {},
  fetchCurrent: function (username, attributes, options) {
    attributes = attributes || {};
    options = options || {};
    if (options.url === undefined) {
      options.url = "/api/mealplans/owner/" + username + "/current";
    }
    return Backbone.Model.prototype.fetch.call(this, attributes, options);
  },
  validate: function (attributes) {
    // To be done
    return null;
  }
});

I've seen this done, in some variations in other places, such as at backbone.js use different urls for model save and fetch - In that case the code is slightly different (I started with that and broke it down to make it easier for me to read.)

The options object has the url parameter in it fine when I pass it to fetch, but then it seems to ignore it!

I am trying to set up a variant fetch method on my backbone model that will fetch the current model for a given user. This is available from the API on /api/mealplans/owner/{username}/current.

I have written the following model. I commented out the URL Root, as the prototype fetch call was simply using the urlRoot and I wanted to see if that was overriding the url parameter I passed in portions somehow.

var mealPlan = Backbone.Model.extend({
  name: 'Meal Plan',
  //urlRoot: '/api/mealplans',
  defaults: {},
  fetchCurrent: function (username, attributes, options) {
    attributes = attributes || {};
    options = options || {};
    if (options.url === undefined) {
      options.url = "/api/mealplans/owner/" + username + "/current";
    }
    return Backbone.Model.prototype.fetch.call(this, attributes, options);
  },
  validate: function (attributes) {
    // To be done
    return null;
  }
});

I've seen this done, in some variations in other places, such as at backbone.js use different urls for model save and fetch - In that case the code is slightly different (I started with that and broke it down to make it easier for me to read.)

The options object has the url parameter in it fine when I pass it to fetch, but then it seems to ignore it!

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Aug 22, 2013 at 14:24 Keith JacksonKeith Jackson 3,2595 gold badges41 silver badges73 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

I was assuming the same parameters to fetch as to save - This is not the case.

The method signature for fetch ONLY takes 'options' and not 'attributes', hence the url parameter wasn't found.

The model code should look a bit more like this..

   var mealPlan = Ministry.Model.extend({

        name: 'Meal Plan',
        urlRoot: '/api/mealplans',

        defaults: {
        },

        fetchCurrent: function (username, options) {
            options = options || {};
            if (options.url === undefined) {
                options.url = this.urlRoot + "/owner/" + username + "/current";
            }

            return Backbone.Model.prototype.fetch.call(this, options);
        },

        validate: function (attributes) {
            // To be done
            return null;
        }
    });

I think it is better to override url() method, like below:

 var mealPlan = Ministry.Model.extend({

    name: 'Meal Plan',
    urlRoot: '/api/mealplans',

    //--> this gets called each time fetch() builds its url
    url: function () { 
        //call the parent url()
        var url=Backbone.Model.prototype.url.call(this);
        //here you can transform the url the way you need
        url += "?code=xxxx";
        return url;
    }
 ...

besides, in your example above I think there is a mistake and you should replace fetchCurrent by fetch

发布评论

评论列表(0)

  1. 暂无评论