How to use the can-connect/helpers/.extend function in can-connect

To help you get started, we’ve selected a few can-connect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github canjs / can-connect / src / fixture / fixture.js View on Github external
can.fixture["-" + types[1] + "Update"] = methods.update;
					can.fixture["-" + types[1] + "Destroy"] = methods.destroy;
					can.fixture["-" + types[1] + "Create"] = methods.create;
				}
			};
		} else {
			filter = make;
			var initialItems = count;
			reset = function(){
				items = initialItems.slice(0);
			};
		}
		

		// make all items
		helpers.extend(methods, {
			findAll: function (request) {
				
				request = request || {};
				//copy array of items
				var retArr = items.slice(0);
				request.data = request.data || {};
				//sort using order
				//order looks like ["age ASC","gender DESC"]
				(request.data.order || [])
					.slice(0)
					.reverse().forEach(function (name) {
						var split = name.split(" ");
						retArr = retArr.sort(function (a, b) {
							if (split[1].toUpperCase() !== "ASC") {
								if (a[split[0]] < b[split[0]]) {
									return 1;
github canjs / can-connect / src / fixture / fixture.js View on Github external
update: function (request, response) {
				var id = getId(request),
					item = findOne(id);

				if(typeof item === "undefined") {
					return response(404, 'Requested resource not found');
				}

				// TODO: make it work with non-linear ids ..
				helpers.extend(item, request.data);
				response({
					id: id
				}, {
					location: request.url || "/" + getId(request)
				});
			},
github canjs / can-connect / src / fixture / fixture.js View on Github external
create: function (settings, response) {
				var item = typeof make === 'function' ? make(items.length, items) : {};

				helpers.extend(item, settings.data);

				// If an ID wasn't passed into the request, we give the item
				// a unique ID.
				if (!item.id) {
					item.id = currentId++;
				}

				// Push the new item into the store.
				items.push(item);
				response({
					id: item.id
				}, {
					location: settings.url + "/" + item.id
				});
			}
		});
github canjs / can-connect / src / fixture / fixture.js View on Github external
item.id = currentId++;
				}

				// Push the new item into the store.
				items.push(item);
				response({
					id: item.id
				}, {
					location: settings.url + "/" + item.id
				});
			}
		});
		reset();
		// if we have types given add them to can.fixture

		return helpers.extend({
			getId: getId,
			find: function (settings) {
				return findOne(getId(settings));
			},
			reset: reset
		}, methods);
	},
	rand: function randomize(arr, min, max) {
github canjs / can-connect / src / fixture / fixture.js View on Github external
// Otherwise, it is a function and we add the fixture data type so the
		// fixture transport will handle it.
	} else {
		//!steal-remove-start
		log("using a dynamic fixture for " + settings.type + " " + settings.url);
		//!steal-remove-end

		// TODO: make everything go here for timing and other fun stuff
		// add to settings data from fixture ...
		if (settings.dataTypes) {
			settings.dataTypes.splice(0, 0, "fixture");
		}

		if (data && originalOptions) {
			originalOptions.data = originalOptions.data || {};
			helpers.extend(originalOptions.data, data);
		}
	}
},
	// A helper function that takes what's called with response
github canjs / can-connect / src / fixture / fixture.js View on Github external
}
		}, can.fixture.delay);

		// Otherwise just run a normal can.ajax call.
	} else {
		//debugger;
		var xhr = new XHR();
		
		// copy everything on this to the xhr object that is not on `this`'s prototype
		for(var prop in this){
			if(!( prop in XMLHttpRequest.prototype) ) {
				xhr[prop] = this[prop];
			}
		}
		//helpers.extend(xhr, this);
		helpers.extend(xhr, settings);
		this._xhr = xhr;
		xhr.open(settings.type, settings.url);
		return xhr.send(data);
	}
};
github canjs / can-connect / src / fixture / fixture.js View on Github external
return;
		}
		settings.fixture = fixture;
		overwrites.push(settings);
		// If a fixture isn't provided, we assume that settings is
		// an array of fixtures, and we should iterate over it, and set up
		// the new fixtures.
	} else {
		helpers.each(settings, function (fixture, url) {
			$fixture(url, fixture);
		});
	}
};
var replacer =  /\{([^\}]+)\}/g;

helpers.extend(can.fixture, {
	// Find an overwrite, given some ajax settings.
	_similar: function (settings, overwrite, exact) {
		if (exact) {
			return canSet.equal(settings, overwrite, {
				fixture: function(){ return true; }
			});
		} else {
			return canSet.subset(settings, overwrite, can.fixture._compare);
		}
	},
	// Comparator object used to find a similar overwrite.
	_compare: {
		url: function (a, b) {
			return !!$fixture._getData(b, a);
		},
		fixture: function(){
github canjs / can-connect / src / fixture / fixture.js View on Github external
xhr: function (xhr) {
		return helpers.extend({}, {
			abort: can.noop,
			getAllResponseHeaders: function () {
				return "";
			},
			getResponseHeader: function () {
				return "";
			},
			open: can.noop,
			overrideMimeType: can.noop,
			readyState: 4,
			responseText: "",
			responseXML: null,
			send: can.noop,
			setRequestHeader: can.noop,
			status: 200,
			statusText: "OK"
github canjs / can-connect / src / helpers / ajax.js View on Github external
module.exports = function (o) {
	var xhr = $.xhr(), timer, n = 0;
	var deferred = helpers.deferred();

	o = helpers.extend({ userAgent: "XMLHttpRequest", lang: "en", type: "GET", data: null, dataType: "application/x-www-form-urlencoded" }, o);
	if (o.timeout) {
		timer = setTimeout(function () {
			xhr.abort();
			if (o.timeoutFn) {
				o.timeoutFn(o.url);
			}
		}, o.timeout);
	}
	xhr.onreadystatechange = function () {
		if (xhr.readyState === 4) {
			if (timer) {
				clearTimeout(timer);
			}
			if (xhr.status < 300) {
				if (o.success) {
					o.success($._xhrResp(xhr));