如何覆盖Backbone.sync所以它增加了在年底apikey和用户名? [英] How to override Backbone.sync so it adds the apikey and username at the end?

查看:143
本文介绍了如何覆盖Backbone.sync所以它增加了在年底apikey和用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的骨干,tastypie,但我有最困难的时候得到它才能正常工作。在Tastypie,我使用ApiKeyAuthentication我的资源,所以每一个Ajax请求,我需要的apikey和用户名追加到请求的结束或发送上的用户名和API密钥添加附加头。

我想删除一个视图,并使用与骨干以下code它的型号:

  //从DOM中删除的目标更新视图
的removeItem:功能(){
  this.model.destroy({等待:真实的,成功:函数(){
    的console.log(成功);
  }错误:函数(){
    的console.log(错误);
  }});
},

函数执行后,浏览器尝试做以下网址一个GET请求:

 :8000 / API / V1 /更新/ 2 /

它不包括在最后的API_KEY或用户名,它具有在URL末尾的斜线。我认为这是试图用Backbone.oldSync做GET请求。我将如何使它所以同步确实包括在年底的用户名/ API密钥,并删除了结尾的斜线?

在所有其他的请求,我做了这么API密钥和用户名被追加到HTTP请求的末尾加入以下code到骨干网tastypie:

 如果(RESP&安培;!及(xhr.status === 201 || xhr.status === 202 || xhr.status === 204)){/ / 201创建202接受或204无内容;响应空。
  VAR位置= xhr.getResponseHeader('位置')|| model.id;
  返回$阿贾克斯({
       网址:位置+? +用户名=+ window.app.settings.credentials.username +&放大器; API_KEY =+ window.app.settings.credentials.api_key,
       成功:dfd.resolve,
       错误:dfd.reject,
    });
}


解决方案

让我们探讨的可能性。

使用头文件

Backbone.sync仍然只是使用jQuery的AJAX让您可以覆盖ajaxSend并使用标头一起发送的信息。

 的$(document).ajaxSend(功能(即XHR,期权)
{
    xhr.setRequestHeader(用户名,window.app.settings.credentials.username);
    xhr.setRequestHeader(API_KEY,window.app.settings.credentials.api_key);
});

使用Ajax选项

如果您需要发送的一个或两个位置的信息,记住摧毁更新保存方法只是快捷方式AJAX调用者。所以,你可以添加所有jQuery的阿贾克斯参数,以这些方法作为这样的:

  //从DOM中删除的目标更新视图
的removeItem:功能()
{
    this.model.destroy({
        等待:真实,
        成功:函数()
        {
            的console.log(成功);
        },
        错误:函数()
        {
            的console.log(错误);
        },
        数据:
        {
            用户名:window.app.settings.credentials.username,
            API_KEY:window.app.settings.credentials.api_key
        }
    });
}

覆盖jQuery的AJAX方法

根据您的需求,这可能是更好的实现(注意,这是没有生产code,你可能需要修改这个以满足您的需求,并在使用之前测试这个)

 (函数($){
    VAR _ajax = $阿贾克斯;
    $ .extend(
    {
        AJAX:功能(选择)
        {
            VAR数据= options.data || {};
            数据= _.defaults(数据,{
                用户名:window.app.settings.credentials.username,
                API_KEY:window.app.settings.credentials.api_key
            });
            options.data =数据;
            返回_ajax.call(这一点,选择);
        }
    });
})(jQuery的);

I am using backbone-tastypie, but I am having the toughest time getting it to work properly. In Tastypie, I am using ApiKeyAuthentication for my resources, so every ajax request, I need to append the apikey and username to the end of a request or send additional headers that add on the username and api key.

I am trying to remove a view and its model using backbone with the following code:

// Remove the goal update view from the DOM
removeItem: function() {
  this.model.destroy({wait: true, success: function() {
    console.log("success");
  }, error: function() {
    console.log("error");
  }});
},

After the function executes, the browser tries to do a GET request on the following URL:

:8000/api/v1/update/2/

It does not include the api_key or username at the end, and it has a trailing slash at the end of the url. I think it is trying to use Backbone.oldSync to do the GET request. How would I make it so the sync does include the username/api key at the end and removes the trailing slash?

In all of the other requests, I have made it so the api key and username is appended to the end of the http request by adding the following code to backbone-tastypie:

if ( !resp && ( xhr.status === 201 || xhr.status === 202 || xhr.status === 204 ) ) { // 201 CREATED, 202 ACCEPTED or 204 NO CONTENT; response null or empty.
  var location = xhr.getResponseHeader( 'Location' ) || model.id;
  return $.ajax( {
       url: location + "?" + "username=" + window.app.settings.credentials.username + "&api_key=" + window.app.settings.credentials.api_key,
       success: dfd.resolve,
       error: dfd.reject,
    });
}

解决方案

Let's explore the possibilities

Using headers

Backbone.sync still just uses jQuery ajax so you can override ajaxSend and use headers to send information along.

$(document).ajaxSend(function(e, xhr, options) 
{
    xhr.setRequestHeader("username", window.app.settings.credentials.username);
    xhr.setRequestHeader("api_key", window.app.settings.credentials.api_key);
});

Using Ajax Options

If you need to send the information in just one or two locations, remember that the destroy, fetch, update and save methods are just shortcuts to the ajax caller. So you can add all jQuery ajax parameters to these methods as such:

// Remove the goal update view from the DOM
removeItem: function ()
{
    this.model.destroy({
        wait: true,
        success: function () 
        {
            console.log("success");
        },
        error: function ()
        {
            console.log("error");
        },
        data: 
        {
            username: window.app.settings.credentials.username,
            api_key: window.app.settings.credentials.api_key
        }
    });
}

Overriding jQuery's ajax method

Depending on your needs, this might be the better implementation (note that this is no production code, you may need to modify this to fit your needs and test this before using it)

(function ($) {
    var _ajax = $.ajax;
    $.extend(
    {
        ajax: function (options)
        {
            var data = options.data || {}; 
            data = _.defaults(data, {
                username: window.app.settings.credentials.username,
                api_key: window.app.settings.credentials.api_key
            });
            options.data = data;
            return _ajax.call(this, options);
        }
    });
})(jQuery);

这篇关于如何覆盖Backbone.sync所以它增加了在年底apikey和用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆