forked from ga-wdi-boston/jquery-ajax-post
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
430bdd0
commit 4dc9101
Showing
12 changed files
with
187 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
tmp | ||
dist | ||
vendor.js | ||
specs.js | ||
*bundle.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,6 @@ | |
"nonew": true, | ||
"strict": "global", | ||
"undef": true, | ||
"unused": true | ||
"unused": true, | ||
"varstmt": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
'use strict'; | ||
|
||
// const libraryApi = require('./library-api'); | ||
// const ui = require('./ui'); | ||
// user require with a reference to bundle the file and use it in this file | ||
// var example = require('./example'); | ||
|
||
// On document ready | ||
$(() => { | ||
}); | ||
// use require without a reference to ensure a file is bundled | ||
require('./example'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Forms | ||
|
||
Developers should use `getFormFields` to retrieve data from html forms for API | ||
requests. | ||
|
||
`getFormFields` only retrieves data from form elements with a name attribute. | ||
|
||
The object returned can be used to validate the form data. | ||
|
||
**in `api.js`** | ||
|
||
```js | ||
'use strict'; | ||
|
||
const ajaxDefaults = { | ||
url: 'http://localhost:3000', | ||
}; | ||
|
||
const myRequest = (data, success, fail) => { | ||
$.ajax(Object.assign({ method: 'POST', data }, ajaxDefaults)) | ||
.done(success) | ||
.fail(fail); | ||
}; | ||
|
||
module.exports = { | ||
myRequest, | ||
}; | ||
``` | ||
|
||
**in `ui.js`** | ||
|
||
```js | ||
'use strict'; | ||
|
||
const success = (data) => { | ||
// handle success | ||
}; | ||
|
||
const failure = (err) => { | ||
// handle failure | ||
}; | ||
|
||
module.exports = { | ||
success, | ||
failure, | ||
} | ||
``` | ||
|
||
**in `index.js`** | ||
|
||
```js | ||
'use strict'; | ||
|
||
const getFormFields = require('../../lib/get-form-fields'); | ||
const api = require('./api'); | ||
const ui = require('./ui'); | ||
|
||
$(() => { | ||
$('#my-form').on('submit', function (e) { | ||
let data = getFormFields(this); | ||
e.preventDefault(); | ||
api.myRequest(data, ui.success, ui.failure); | ||
}); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
{ | ||
"options": { | ||
"config": ".jscsrc", | ||
"esnext": true, | ||
"verbose": true | ||
"config": ".jscsrc" | ||
}, | ||
|
||
"status": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const addFormField = function addFormField(target, names, value) { | ||
let name = names.shift(); | ||
let next = names[0]; | ||
if (next === '') { // name is an array | ||
target[name] = target[name] || []; | ||
target[name].push(value); | ||
} else if (next) { // name is a parent key | ||
target[name] = target[name] || {}; | ||
addFormField(target[name], names, value); | ||
} else { // name is the key for value | ||
target[name] = value; | ||
} | ||
|
||
return target; | ||
}; | ||
|
||
const getFormFields = (form) => { | ||
let target = {}; | ||
|
||
let elements = form.elements || []; | ||
for (let i = 0; i < elements.length; i++) { | ||
let e = elements[i]; | ||
if (!e.hasAttribute('name')) { | ||
continue; | ||
} | ||
|
||
let type = 'TEXT'; | ||
switch (e.nodeName.toUpperCase()) { | ||
case 'SELECT': | ||
type = e.hasAttribute('multiple') ? 'MULTIPLE' : type; | ||
break; | ||
case 'INPUT': | ||
type = e.getAttribute('type').toUpperCase(); | ||
break; | ||
} | ||
|
||
let names = e.getAttribute('name').split('[') | ||
.map((k) => k.replace(/]$/, '')); | ||
|
||
if (type === 'MULTIPLE') { | ||
for (let i = 0; i < e.length; i++) { | ||
if (e[i].selected) { | ||
addFormField(target, names.slice(), e[i].value); | ||
} | ||
} | ||
} else if ((type !== 'RADIO' && type !== 'CHECKBOX') || e.checked) { | ||
addFormField(target, names, e.value); | ||
} | ||
} | ||
|
||
return target; | ||
}; | ||
|
||
module.exports = getFormFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
{ | ||
"name": "ga-wdi-boston.jquery-ajax-get", | ||
"name": "ga-wdi-boston-js-template", | ||
"version": "0.0.2", | ||
"private": true, | ||
"license": "MIT", | ||
"dependencies": { | ||
"jquery": "^2.2.2" | ||
"jquery": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.7.4", | ||
"babel-core": "^6.10.4", | ||
"babel-loader": "^6.2.4", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
"bootstrap-sass": "^3.3.6", | ||
"clone": "^1.0.2", | ||
"css-loader": "^0.23.1", | ||
"expose-loader": "^0.7.1", | ||
"file-loader": "^0.8.5", | ||
"grunt": "^0.4.5", | ||
"grunt-concurrent": "^2.2.1", | ||
"grunt-contrib-jasmine": "^1.0.0", | ||
"file-loader": "^0.9.0", | ||
"grunt": "^1.0.1", | ||
"grunt-concurrent": "^2.3.0", | ||
"grunt-contrib-jasmine": "^1.0.3", | ||
"grunt-contrib-jshint": "^1.0.0", | ||
"grunt-jscs": "^2.8.0", | ||
"grunt-jsonlint": "^1.0.7", | ||
"grunt-nodemon": "^0.4.1", | ||
"grunt-jscs": "^3.0.1", | ||
"grunt-jsonlint": "^1.1.0", | ||
"grunt-nodemon": "^0.4.2", | ||
"grunt-open": "^0.2.3", | ||
"grunt-webpack": "^1.0.11", | ||
"handlebars": "^4.0.5", | ||
"handlebars-loader": "^1.2.0", | ||
"handlebars-loader": "^1.3.0", | ||
"html-loader": "^0.4.3", | ||
"load-grunt-config": "^0.19.1", | ||
"load-grunt-config": "^0.19.2", | ||
"node-libs-browser": "^1.0.0", | ||
"node-sass": "^3.4.2", | ||
"sass-loader": "^3.2.0", | ||
"node-sass": "^3.8.0", | ||
"sass-loader": "^4.0.0", | ||
"style-loader": "^0.13.1", | ||
"time-grunt": "^1.3.0", | ||
"url-loader": "^0.5.7", | ||
"webpack": "^1.12.14", | ||
"webpack": "^1.13.1", | ||
"webpack-dev-server": "^1.14.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
const example = require('../assets/scripts/example'); | ||
|
||
describe('Example', function () { | ||
it('is true', function () { | ||
expect(example).toBe(true); | ||
}); | ||
}); |