Skip to content
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

Xml http request support #251

Merged
merged 7 commits into from
Nov 27, 2017
Merged
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
49 changes: 28 additions & 21 deletions src/rivescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -392,27 +392,34 @@ class RiveScript

# Load a file using ajax. DO NOT CALL THIS DIRECTLY.
_ajaxLoadFile: (loadCount, file, onSuccess, onError) ->
# Make the ajax request with jQuery. TODO: don't use jQuery.
$.ajax
url: file
dataType: "text"
success: (data, textStatus, xhr) =>
@say "Loading file #{file} complete."

# Parse it!
@parse file, data, onError

# Log that we've received this file.
delete @_pending[loadCount][file]

# All gone?
if Object.keys(@_pending[loadCount]).length is 0
if typeof(onSuccess) is "function"
onSuccess.call undefined, loadCount
error: (xhr, textStatus, errorThrown) =>
@say "Ajax error! #{textStatus}; #{errorThrown}"
if typeof(onError) is "function"
onError.call undefined, textStatus, loadCount

xhr = new XMLHttpRequest();
xhr.open "GET", file, true
xhr.onreadystatechange = () =>

if xhr.readyState is 4
if xhr.status in [0, 200]

@say "Loading file #{file} complete."

# Parse it!
@parse file, xhr.responseText, onError

# Log that we've received this file.
delete @_pending[loadCount][file]

# All gone?
if Object.keys(@_pending[loadCount]).length is 0
if typeof(onSuccess) is "function"
onSuccess.call undefined, loadCount

else
@say "Ajax error! #{xhr.statusText}; #{xhr.status}"
if typeof(onError) is "function"
onError.call undefined, xhr.statusText, loadCount

xhr.send null


# Load a file using node. DO NOT CALL THIS DIRECTLY.
_nodeLoadFile: (loadCount, file, onSuccess, onError) ->
Expand Down