Small refactor of the URL replacement code #14

Merged
merged 2 commits into from May 30, 2011
Jump to file or symbol
Failed to load files and symbols.
+16 −7
Diff settings

Always

Just for now

View
@@ -89,19 +89,21 @@ amplify.request.define = function( resourceId, type, settings ) {
(function( amplify, $, undefined ) {
-var xhrProps = [ "status", "statusText", "responseText", "responseXML", "readyState" ];
+var xhrProps = [ "status", "statusText", "responseText", "responseXML", "readyState" ],
+ rurlData = /\{([^\}]+)\}/g;
amplify.request.types.ajax = function( defnSettings ) {
defnSettings = $.extend({
type: "GET"
}, defnSettings );
return function( settings, request ) {
- var regex, xhr,
+ var xhr,
url = defnSettings.url,
data = settings.data,
abort = request.abort,
ajaxSettings = {},
+ mappedKeys = [],
aborted = false,
ampXHR = {
readyState: 0,
@@ -135,13 +137,18 @@ amplify.request.types.ajax = function( defnSettings ) {
if ( typeof data !== "string" ) {
data = $.extend( true, {}, defnSettings.data, data );
- $.each( data, function( key, value ) {
- regex = new RegExp( "{" + key + "}", "g");
- if ( regex.test( url ) ) {
- url = url.replace( regex, value );
- delete data[ key ];
+
+ url = url.replace( rurlData, function ( m, key ) {
+ if ( key in data ) {
+ mappedKeys.push( key );
+ return data[ key ];
}
});
+
+ // We delete the keys later so duplicates are still replaced
+ $.each( mappedKeys, function ( i, key ) {
+ delete data[ key ];
+ });
}
$.extend( ajaxSettings, defnSettings, {
View
@@ -557,6 +557,7 @@ test( "data merging", function() {
}, "default data passed through" );
};
amplify.request.define( "test", "ajax", {
+ url: "",
data: {
foo: "bar",
bar: "baz",
@@ -577,6 +578,7 @@ test( "data merging", function() {
}, "data merged" );
};
amplify.request.define( "test", "ajax", {
+ url: "",
data: {
foo: "bar",
bar: "baz",