Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js with a custom fetch URL

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!

like image 207
Keith Jackson Avatar asked Aug 22 '13 14:08

Keith Jackson


People also ask

What is backbone JS?

Backbone.js. Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface. The project is hosted on GitHub , ...

What is the use of backbone in backend?

Backbone.js Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

What's new in backbone for JSON?

On the other hand, parse is now an excellent place to extract and vivify incoming nested JSON into associated submodels. Many tweaks, optimizations and bugfixes relating to Backbone 1.0, including URL overrides, mutation of options, bulk ordering, trailing slashes, edge-case listener leaks, nested model parsing...

How to set the library backbone uses for DOM manipulation and Ajax?

To set what library Backbone uses for DOM manipulation and Ajax calls, use Backbone.$ = ... instead of setDomLibrary . Removed the Backbone.wrapError helper method. Overriding sync should work better for those particular use cases.


2 Answers

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;
        }
    });
like image 89
Keith Jackson Avatar answered Sep 28 '22 13:09

Keith Jackson


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

like image 20
Nadir Avatar answered Sep 28 '22 14:09

Nadir