Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's explore the possibilities</p> <p><strong>Using headers</strong></p> <p>Backbone.sync still just uses jQuery ajax so you can override ajaxSend and use headers to send information along.</p> <pre><code>$(document).ajaxSend(function(e, xhr, options) { xhr.setRequestHeader("username", window.app.settings.credentials.username); xhr.setRequestHeader("api_key", window.app.settings.credentials.api_key); }); </code></pre> <p><strong>Using Ajax Options</strong></p> <p>If you need to send the information in just one or two locations, remember that the <code>destroy</code>, <code>fetch</code>, <code>update</code> and <code>save</code> methods are just shortcuts to the ajax caller. So you can add <a href="http://api.jquery.com/jQuery.ajax/" rel="noreferrer">all jQuery ajax parameters</a> to these methods as such:</p> <pre><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"); }, data: { username: window.app.settings.credentials.username, api_key: window.app.settings.credentials.api_key } }); } </code></pre> <p><strong>Overriding jQuery's ajax method</strong></p> <p>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)</p> <pre><code>(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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload