Amplify.js - 类似CRUD的网址

Bri*_*unt 10 javascript amplifyjs

我正在使用amplify.request,我希望在向服务器发送数据和从服务器发送数据时使用类似CRUD的URL.这是一个例子:

定义资源

resources = {
"document_create"           : ['/d/crud/',           "POST"],
"document_read"             : ['/d/crud/{id}',       "GET"],
"document_update"           : ['/d/crud/{id}',       "PUT"],
"document_delete"           : ['/d/crud/{id}',       "DELETE"]
};

$.each(resources, function (resource, settings) {
  definition = {
    url     : settings[0],
    type    : settings[1],
    dataType: "json",  // what comes back
    decoder : 'jsend',
    contentType: 'application/json' // what goes there
  };

  amplify.request.define(resource, "ajax", definition);
});
Run Code Online (Sandbox Code Playgroud)

使用资源

function make_request(resource, params, success_cb, error_cb) {
 if (this.is_post(resource)) {
   // this.is_post is a test, defined elsewhere, to see if this is a POST request
   params = JSON.stringify(params);
 }

 amplify.request(
   resourceId: resource
   data: params
   success: success_cb
   error: error_cb
 );
}
Run Code Online (Sandbox Code Playgroud)

这适用于createread`删除,像 - 所以:

make_request('document_delete', {id: 1}, cb)
Run Code Online (Sandbox Code Playgroud)

但是,update因为内容是作为JSON传递的,所以URL替换不会按预期发生.

有没有办法{id}在上述方案中利用URL替换?

我能想到的唯一选择是将数据传递给URL编码的服务器.不幸的是,这有点问题,我希望能够保留使用类似CRUD的URL并保存JSON格式的数据(如果可能的话).

我们将不胜感激.

Dou*_*ner 9

您可以定义自己的请求类型,或者您可以只是监听request.ajax.preprocess并执行您的操作JSON.stringify- 这是 URL替换发生之后.

is_post是想必你现在,只要把在不同的地方相同的代码.这不是一个神奇的功能:)

 amplify.subscribe( "request.ajax.preprocess", function( defnSettings, settings, ajaxSettings ) {
    if ( is_post( defnSettings.resourceId ) ) {
         // This will still include the variable that matches the URL substitution:
         var _settings = $.extend( true, {}, defnSettings.data, settings.data, ajaxSettings.data );
         ajaxSettings.data = JSON.stringify( _settings );
    }
 });
Run Code Online (Sandbox Code Playgroud)

make_request将不再stringify:

function make_request(resource, params, success_cb, error_cb) {
 amplify.request(
   resourceId: resource
   data: params
   success: success_cb
   error: error_cb
 );
}
Run Code Online (Sandbox Code Playgroud)

然后您的请求可以正常运行:

make_request('document_update', {id: 1, title: "New Title" }, cb)
Run Code Online (Sandbox Code Playgroud)

重要:我编写预处理调用的方式,id即使它与URL匹配也不会删除(Amplify通常会删除匹配的键).如果您希望它id从字符串化的JSON中删除它,请将_settings赋值替换为:

_settings = ajaxSettings.data;
Run Code Online (Sandbox Code Playgroud)


Ger*_*mán 8

从Amplify 1.1.0开始,在定义资源时有一个dataMap选项.如果传递函数而不是散列对象,则放大将使用传递给请求的原始数据调用此函数.

在这个例子中,我将JSON.stringify传递给dataMap选项.

amplify.request.define("paqueteput", "ajax", {
    url: "/api/paquetes/{id}",
    dataType: "json",
    type: "PUT",
    contentType: "application/json; charset=utf-8",
    dataMap: JSON.stringify
});


// later we pass a raw object that gets stringified to JSON
var savePaquete = function(paquete, cb) {
    amplify.request({
         resourceId: "paqueteput",
         data: paquete,
         success: cb.sucess,
         error: cb.error
    });
};
Run Code Online (Sandbox Code Playgroud)

我唯一不喜欢的是放大从数据对象中删除URL中映射的键.在这种情况下,id属性将从最终的JSON中删除,尽管它存在于url中.

放大应该有一个选项来控制这种行为!