Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: marianoguerra/intercaljs
base: 948e15945d
...
head fork: marianoguerra/intercaljs
compare: 736d958013
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 104 additions and 2 deletions.
  1. +104 −2 src/intercal.js
View
106 src/intercal.js
@@ -1,6 +1,7 @@
(function ($) {
"use strict";
- var intercal;
+ var intercal, ajax = $.ajax, ajaxChecks = [],
+ methods = ['GET', 'POST', 'PUT', 'DELETE'];
function buildDeferreds(onces, obj, external, isTopLevel) {
var key, value, names, i, name;
@@ -385,6 +386,28 @@
return ajaxRequest;
}
+ // does nothing
+ function nop() {
+ }
+
+ // wrap console functions if available, otherwise create a dummy api
+ // that does nothing
+ function buildLogWrapper(log) {
+ function addIfExists(obj, name) {
+ if (obj && obj[name] && $.isFunction(obj[name])) {
+ log[name] = obj[name];
+ } else {
+ log[name] = nop;
+ }
+ }
+
+ addIfExists(console, "log");
+ addIfExists(console, "error");
+ addIfExists(console, "warn");
+ addIfExists(console, "debug");
+ addIfExists(console, "info");
+ }
+
intercal = function (data) {
data = data || {};
@@ -397,11 +420,16 @@
resources = data.resource || {},
// callbacks to fire on resource requests
resourceCallbacks,
+ // log wrapper around console if available or silently ignore them
+ log = {},
// the object to be returned
obj = {
"once": {},
"on": {},
- "resource": {}
+ "resource": {},
+ // data passed to constructor or an empty object
+ "data": data.data || {},
+ "log": log
},
resetCallback,
// external deferred API
@@ -460,6 +488,8 @@
// build deferreds for the first time
reset();
+ buildLogWrapper(log);
+
obj.api = {
once: buildOnceAPI,
on: buildOnAPI
@@ -480,6 +510,20 @@
obj.template = $.intercal.template;
obj.now = $.intercal.now;
+ obj.util = {
+ "onSuccess": function (onSuccess, onError) {
+ return function (success) {
+ var args = $.makeArray(arguments).slice(1);
+
+ if (success) {
+ onSuccess.apply(null, args);
+ } else if ($.isFunction(onError)) {
+ onError.apply(null, args);
+ }
+ };
+ }
+ };
+
return obj;
};
@@ -757,6 +801,64 @@
this.message = message;
};
+ function fakeAjax(url, settings) {
+ var i, check, dfd;
+
+ for (i = 0; i < ajaxChecks.length; i += 1) {
+ check = ajaxChecks[i];
+
+ if (check.check(url, settings)) {
+ dfd = $.Deferred();
+ check.returns(url, settings, dfd);
+ return dfd;
+ }
+ }
+
+ return ajax(url, settings);
+ }
+
+ intercal.ajax = {
+ "is": {}
+ };
+
+ intercal.ajax.mock = function (check, returns) {
+ ajaxChecks.push({check: check, returns: returns});
+
+ if ($.ajax === ajax) {
+ $.ajax = fakeAjax;
+ }
+ };
+
+ intercal.ajax.unmock = function () {
+ $.ajax = ajax;
+ };
+
+ intercal.ajax.is.method = function (method, path, contentType) {
+ return function (url, settings) {
+ var isPath;
+
+ if (path instanceof RegExp) {
+ isPath = path.test(url);
+ } else {
+ isPath = url === path;
+ }
+
+ if (isPath && settings.type === method) {
+ if (contentType == null || settings.contentType === contentType) {
+ return true;
+ }
+ }
+
+ return false;
+ };
+ };
+
+ $.each(methods, function (i, method) {
+ intercal.ajax.is[method.toLowerCase()] = function (path, contentType) {
+ return intercal.ajax.is.method(method, path, contentType);
+ };
+ });
+
$.intercal = intercal;
return intercal;

No commit comments for this range

Something went wrong with that request. Please try again.