Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  48] [ 3]  / answers: 1 / hits: 142287  / 13 Years ago, tue, july 12, 2011, 12:00:00

Following the documentation, I did:



var collection = new Backbone.Collection.extend({
model: ItemModel,
url: '/Items'
})

collection.fetch({ data: { page: 1} });


the url turned out to be: http://localhost:1273/Items?[object%20Object]



I was expecting something like http://localhost:1273/Items?page=1



So how do I pass params in the fetch method?


More From » backbone.js

 Answers
21

changing:



collection.fetch({ data: { page: 1} });


to:



collection.fetch({ data: $.param({ page: 1}) });


So with out over doing it, this is called with your {data: {page:1}} object as options



Backbone.sync = function(method, model, options) {
var type = methodMap[method];

// Default JSON-request options.
var params = _.extend({
type: type,
dataType: 'json',
processData: false
}, options);

// Ensure that we have a URL.
if (!params.url) {
params.url = getUrl(model) || urlError();
}

// Ensure that we have the appropriate request data.
if (!params.data && model && (method == 'create' || method == 'update')) {
params.contentType = 'application/json';
params.data = JSON.stringify(model.toJSON());
}

// For older servers, emulate JSON by encoding the request into an HTML-form.
if (Backbone.emulateJSON) {
params.contentType = 'application/x-www-form-urlencoded';
params.processData = true;
params.data = params.data ? {model : params.data} : {};
}

// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
// And an `X-HTTP-Method-Override` header.
if (Backbone.emulateHTTP) {
if (type === 'PUT' || type === 'DELETE') {
if (Backbone.emulateJSON) params.data._method = type;
params.type = 'POST';
params.beforeSend = function(xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', type);
};
}
}

// Make the request.
return $.ajax(params);
};


So it sends the 'data' to jQuery.ajax which will do its best to append whatever params.data is to the URL.


[#91237] Sunday, July 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;