Sha*_*ean 152 javascript backbone.js
在文档之后,我做了:
var collection = new Backbone.Collection.extend({
model: ItemModel,
url: '/Items'
})
collection.fetch({ data: { page: 1} });
Run Code Online (Sandbox Code Playgroud)
网址原来是: http://localhost:1273/Items?[object%20Object]
我期待着类似的东西 http://localhost:1273/Items?page=1
那么如何在fetch方法中传递params呢?
Joe*_*Joe 213
变化:
collection.fetch({ data: { page: 1} });
Run Code Online (Sandbox Code Playgroud)
至:
collection.fetch({ data: $.param({ page: 1}) });
Run Code Online (Sandbox Code Playgroud)
因此,在执行此操作时,将使用您的{data: {page:1}}对象调用它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);
};
Run Code Online (Sandbox Code Playgroud)
因此它将'data'发送到jQuery.ajax,它将尽力将任何params.data内容附加到URL.
Jim*_*hao 71
您还可以将processData设置为true:
collection.fetch({
data: { page: 1 },
processData: true
});
Run Code Online (Sandbox Code Playgroud)
Jquery会自动将数据对象处理成param字符串,
但是在Backbone.sync函数中,Backbone关闭了processData,因为Backbone将使用其他方法来处理POST,UPDATE ...
在Backbone来源:
if (params.type !== 'GET' && !Backbone.emulateJSON) {
params.processData = false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
138429 次 |
| 最近记录: |