Parametrelerle Backbone.js getirme


152

Belgeleri takip ederek yaptım:

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

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

url olduğu ortaya çıktı: http://localhost:1273/Items?[object%20Object]

Gibi bir şey bekliyordum http://localhost:1273/Items?page=1

Peki getirme yönteminde parametreleri nasıl iletirim?


Kesinlikle garip. Sahip olduğunuz şey , API dokümanlarına dayalı olarak iyi çalışmalıdır . Backbone.js'nin en son sürümünü mü kullanıyorsunuz?
Matt Ball

Deneyebilir JSON.stringify({ data: { page: 1} })misin?
Joe

@Joe Tuskan, bununla ne yapacağımdan emin değilim, ama yaptım: collection.fetch(JSON.stringify({ data: { page: 1} }));ve url'de hiçbir şey geçmedi.
Shawn Mclean

Tamam, şunu yap: collection.fetch ({data: JSON.stringify ({sayfa: 1})});
Joe

3
Backbone 1.0 fyi'de yazdığınız gibi iyi çalışıyor
Dominic

Yanıtlar:


213

değiştirme:

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

için:

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

Yani bunu yapmak için dışarıdan, bu {data: {page:1}}nesne ile deniroptions

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);
};

Böylece URL'ye ne varsa eklemek için elinden geleni yapacak olan jQuery.ajax 'veri' gönderir params.data.


71

ProcessData öğesini true olarak da ayarlayabilirsiniz:

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

Jquery, veri nesnesini param dizesine otomatik olarak işleyecektir,

ancak Backbone.sync işlevinde, Omurga POST, UPDATE ... içindeki verileri işlemek için başka bir yöntem kullanacağından Omurga processData özelliğini kapatır.

Omurga kaynağında:

if (params.type !== 'GET' && !Backbone.emulateJSON) {
    params.processData = false;
}


-2
try {
    // THIS for POST+JSON
    options.contentType = 'application/json';
    options.type = 'POST';
    options.data = JSON.stringify(options.data);

    // OR THIS for GET+URL-encoded
    //options.data = $.param(_.clone(options.data));

    console.log('.fetch options = ', options);
    collection.fetch(options);
} catch (excp) {
    alert(excp);
}
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.