From 3c4b5157bd4020e0f4385b4e9e83b9dadf8d089d Mon Sep 17 00:00:00 2001 From: Long Ouyang Date: Thu, 19 Mar 2015 16:13:00 -0700 Subject: [PATCH] Switch main argument of submit from object to data --- README.md | 18 +++++++++--------- mmturkey.js | 26 +++++++++++++------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 257095f..9bb3c50 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,17 @@ It creates a global JavaScript object, `turk`, that has these five properties: * `previewMode`: true if we're currently in the external HIT preview mode. * `turkSubmitTo`: the Mechanical Turk submission server (either production or sandbox). -These properties get read from `location.href`. +These properties get read from `location.href` (e.g., `page.html?assignmentId=foo&workerId=baz&...`) If they aren't in `location.href`, the script also tries `document.referer`, so you can conceivably split a task across two different pages. -If `previewMode` is true, all other properties are empty strings. +When these properties aren't found, they are set to empty strings (except for `previewMode`, which is true if `assignmentId` is `ASSIGNMENT_ID_NOT_AVAIALBLE`, otherwise false). -mmturkey provides a single method, `turk.submit(object, [unwrap])`, which takes one required argument, `object`, and an optional argument, `unwrap`. +mmturkey provides a single method, `turk.submit(data, [unwrap])`, which takes one required argument, `data`, and an optional argument, `unwrap`. -`object` is an object containing keys and values. -If `object` is empty or not supplied, mmturkey responds with an error. -mmturkey will submit this object (potentially "unwrapped" -- see below) via a POST request to the externalSubmit URL declared in `turk.turkSubmitTo`. +`data` is an object containing keys and values. +If `data` is empty or not supplied, mmturkey responds with an error. +mmturkey will submit `data` (potentially "unwrapped" -- see below) via a POST request to the externalSubmit URL declared in `turk.turkSubmitTo`. -`unwrap`: Best illustrated with an example. Suppose that `object` is this: +`unwrap`: Best illustrated with an example. Suppose that `data` is this: ```js { @@ -37,7 +37,7 @@ By default, `unwrap=false`, which means that this will get submitted to Turk: } ``` -Note that what gets submitted to Turk has only a single key, `data`, whose value is the JSON representation of `object`. +Note that what gets submitted to Turk has only a single key, `data`, whose value is the JSON representation of `data`. When `unwrap` is true, the data submitted to Turk looks like this: ```js @@ -49,7 +49,7 @@ When `unwrap` is true, the data submitted to Turk looks like this: } ``` -Now, the submitted data has four keys, just like `object`; `object` has been "unwrapped". +Now, the submitted data has four keys, just like `data`; `data` has been "unwrapped". However, this unwrapping only goes one level deep -- note that the values for `demographics` and `trials` are JSON strings. Unwrapping is most useful when the data you want to submit is relatively flat. Ideally, with data that is one level deep, you can directly translate your data into something like a csv file. diff --git a/mmturkey.js b/mmturkey.js index b9c8e1b..84e8bb3 100644 --- a/mmturkey.js +++ b/mmturkey.js @@ -161,7 +161,7 @@ turk = turk || {}; var url = window.location.href, src = param(url, "assignmentId") ? url : document.referrer, keys = ["assignmentId","hitId","workerId","turkSubmitTo"]; - + keys.map(function(key) { turk[key] = unescape(param(src, key)); }); @@ -169,16 +169,16 @@ turk = turk || {}; turk.previewMode = (turk.assignmentId == "ASSIGNMENT_ID_NOT_AVAILABLE"); // Submit a POST request to Turk - turk.submit = function(object, unwrap) { - var keys = getKeys(object); - - if (typeof object == "undefined" || keys.length == 0) { - alert("mmturkey: you need to pass an object (i.e., actual data) to turk.submit() "); + turk.submit = function(data, unwrap) { + var keys = getKeys(data); + + if (typeof data == "undefined" || keys.length == 0) { + alert("mmturkey: you need to pass a non-empty object to turk.submit()"); return; } - + unwrap = !!unwrap; - + var assignmentId = turk.assignmentId, turkSubmitTo = turk.turkSubmitTo, rawData = {}, @@ -194,13 +194,13 @@ turk = turk || {}; if (unwrap) { // Filter out non-own properties and things that are functions keys.map(function(key) { - rawData[key] = object[key]; - addFormData(form, key, JSON.stringify(object[key])); + rawData[key] = data[key]; + addFormData(form, key, JSON.stringify(data[key])); }); - + } else { - rawData["data"] = object; - addFormData(form, "data", JSON.stringify(object)); + rawData["data"] = data; + addFormData(form, "data", JSON.stringify(data)); } // If there's no turk info