Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use fetch instead of $.ajax #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
justin808 merged 1 commit into shakacode:master from mapreal19:fetch-for-async-calls
Sep 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion client/.jscsrc
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"preset": "airbnb",
"fileExtensions": [".js", ".jsx"],
"excludeFiles": ["build/**", "node_modules/**"]
"excludeFiles": ["build/**", "node_modules/**"],
"disallowQuotedKeysInObjects": false
}
12 changes: 5 additions & 7 deletions client/assets/javascripts/actions/CommentActionCreators.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ export function submitCommentFailure(error) {

export function fetchComments() {
return dispatch => {
return CommentsManager.fetchComments().then(
comments => dispatch(fetchCommentsSuccess(comments)),
error => dispatch(fetchCommentsFailure(error))
);
return CommentsManager.fetchComments()
.then(comments => dispatch(fetchCommentsSuccess(comments)))
.catch(error => dispatch(fetchCommentsFailure(error)));
Copy link
Member

@justin808 justin808 Aug 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};
}

Expand All @@ -58,9 +57,8 @@ export function submitComment(comment) {
return dispatch => {
dispatch(incrementAjaxCounter());
return CommentsManager.submitComment(comment)
.then(
_comment => dispatch(submitCommentSuccess(_comment)),
error => dispatch(submitCommentFailure(error)))
.then(commentFromServer => dispatch(submitCommentSuccess(commentFromServer)))
.catch(error => dispatch(submitCommentFailure(error)))
.then(() => dispatchDecrementAjaxCounter(dispatch));
};
}
42 changes: 28 additions & 14 deletions client/assets/javascripts/utils/CommentsManager.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
import $ from 'jquery';

const API_URL = 'comments.json';

// fetch won't reject on HTTP error status
// https://github.com/github/fetch#handling-http-error-statuses
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}

const error = new Error(response.statusText);
error.response = response;
throw error;
}

function parseJSON(response) {
return response.json();
Copy link
Member

@justin808 justin808 Aug 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this does a parsing and throws if there is an error.

}

const CommentsManager = {

/**
* Retrieve comments from server using AJAX call.
*
* @returns {Promise} - jqXHR result of ajax call.
* @returns {Promise} - response of fetch request.
*/
fetchComments() {
return Promise.resolve($.ajax({
url: API_URL,
dataType: 'json',
}));
return fetch(API_URL).then(checkStatus).then(parseJSON);
Copy link
Member

@justin808 justin808 Aug 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. what if the server is down?
  2. what if there is some other error?

Copy link
Member

@justin808 justin808 Aug 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The caller is responsible for handling the promise.

Copy link
Member

@justin808 justin808 Aug 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see how others use a global polyfill for fetch, but I think this is really awkward.

I'd rather see something like what lodash has.

The way that webpack has to use both the import and export loaders is a real brain twister. I get it and am posting to the forum on how this works.

However, this sort of goes against my philosophy of "Don't make me think".

Looking for more discussion on this one. @alexfedoseev ? @skv-headless ?

},

/**
* Submit new comment to server using AJAX call.
*
* @param {Object} comment - Comment body to post.
* @returns {Promise} - jqXHR result of ajax call.
* @returns {Promise} - response of fetch request.
*/

submitComment(comment) {
return Promise.resolve($.ajax({
url: API_URL,
dataType: 'json',
type: 'POST',
data: {comment: comment},
}));
return fetch(API_URL, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({comment}),
}).then(checkStatus).then(parseJSON);
},
};

Expand Down
3 changes: 2 additions & 1 deletion client/package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"redux-promise": "^0.5.0",
"redux-thunk": "^0.1.0",
"sleep": "^3.0.0",
"webpack": "^1.10.5"
"webpack": "^1.10.5",
Copy link
Member

@justin808 justin808 Sep 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexfedoseev @mapreal19 I'm pretty sure some of these dependencies are not needed for the rails deployment. It would be good to see see if the hot only dependencies were moved to devDependencies.

The problem with the build is copied here:

1) Add new comment Horizonal Form behaves like Form submits form
Failure/Error: visit root_path
Capybara::Poltergeist::JavascriptError:
 One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).
 Error: Cannot find module "whatwg-fetch"
 Error: Cannot find module "whatwg-fetch"
 at http://127.0.0.1:49150/assets/application.js:14672 in webpackMissingModule
 at http://127.0.0.1:49150/assets/application.js:14672
 at http://127.0.0.1:49150/assets/application.js:14645 in __webpack_require__
 at http://127.0.0.1:49150/assets/application.js:14665
 at http://127.0.0.1:49150/assets/application.js:16345
Shared Example Group: "Form" called from ./spec/features/comments_spec.rb:36
# /home/rof/cache/bundler/ruby/2.2.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/browser.rb:323:in `command'
# /home/rof/cache/bundler/ruby/2.2.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/browser.rb:31:in `visit'
# /home/rof/cache/bundler/ruby/2.2.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/driver.rb:95:in `visit'
# /home/rof/cache/bundler/ruby/2.2.0/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
# /home/rof/cache/bundler/ruby/2.2.0/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/features/comments_spec.rb:28:in `block (2 levels) in <top (required)>'
# ./spec/rails_helper.rb:43:in `block (3 levels) in <top (required)>'
# /home/rof/cache/bundler/ruby/2.2.0/gems/database_cleaner-1.4.1/lib/database_cleaner/generic/base.rb:15:in `cleaning'
# /home/rof/cache/bundler/ruby/2.2.0/gems/database_cleaner-1.4.1/lib/database_cleaner/base.rb:92:in `cleaning'
# /home/rof/cache/bundler/ruby/2.2.0/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/rof/cache/bundler/ruby/2.2.0/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `call'
# /home/rof/cache/bundler/ruby/2.2.0/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:42:in `block (2 levels) in <top (required)>'

CC: @dylangrafmyre

"whatwg-fetch": "^0.9.0"
},
"devDependencies": {
"babel-eslint": "^4.0.5",
Expand Down
2 changes: 1 addition & 1 deletion client/webpack.common.config.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {

// the project dir
context: __dirname,
entry: ['./assets/javascripts/App'],
entry: ['whatwg-fetch', './assets/javascripts/App'],

// In case you wanted to load jQuery from the CDN, this is how you would do it:
// externals: {
Expand Down

AltStyle によって変換されたページ (->オリジナル) /