Vertebral.js fetch con parámetros


Siguiendo la documentación , hice:

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

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

La url resultó ser: http://localhost:1273/Items?[object%20Object]

Esperaba algo como http://localhost:1273/Items?page=1

Entonces, ¿cómo paso parámetros en el método fetch?

Author: Shawn Mclean, 2011-07-12

4 answers

Cambiando:

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

A:

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

Así que sin más hacerlo, esto se llama con su {data: {page:1}} objeto como 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);
};

Así que envía los 'datos' a jQuery.ajax que hará todo lo posible para agregar lo que sea params.data a la URL.

 209
Author: Joe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-09 12:49:15

También puede establecer processData en true:

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

Jquery procesará automáticamente el objeto de datos en una cadena param,

Pero en Backbone.función de sincronización, Backbone desactiva processData porque Backbone usará otro método para procesar datos en POST, ACTUALIZACIÓN...

En Backbone fuente:

if (params.type !== 'GET' && !Backbone.emulateJSON) {
    params.processData = false;
}
 69
Author: Jimchao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-19 17:49:28

Otro ejemplo si está utilizando aleación de titanio:

 collection.fetch({ 
     data: {
             where : JSON.stringify({
                page: 1
             })
           } 
      });
 1
Author: Nightwish1986,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-23 12:55:48
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);
}
 -2
Author: Walter von Entferndt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-07 14:49:29