Skip to content

Commit

Permalink
Switch main argument of submit from object to data
Browse files Browse the repository at this point in the history
  • Loading branch information
longouyang committed Mar 19, 2015
1 parent 2306ecc commit 3c4b515
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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.
Expand Down
26 changes: 13 additions & 13 deletions mmturkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,24 @@ 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));
});

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 = {},
Expand All @@ -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
Expand Down

0 comments on commit 3c4b515

Please sign in to comment.