I cannot do anything from the AWS.Request.send() callback - javascript

I'm using the AWS S3 Javascript SDK and i am trying to do some operations as using external functions or append some HTML code in a HTML div etc. all these things from my callback send() but nothing happens i get errors whereas i don't have any problem when i'm making these stuffs outside that send().
I have absolutely to write some specific code involving external functions inside this send() that just can't be made outside . How to resolve this issue ?
You can have a glance on the AWS.request.send() here

Related

Pass Go function to html/js button "onclick" response

I am using Go's "net/http" package to pass data between the html and the backend in Go. For example, we can used the location of an image like this:
<img src={{.MyPicture}} width=200 height=auto/>
We can do the same thing to pass in functions to the html and call them:
{{if .MyBool}}
{{.MyFunction}}
{{end}}
Now my question is: how do I set the response of a button to call my function? I would expect this to work, but it doesn't:
<button onclick={{.ShowMoreLinks}}>Show more!</button>
I get "[js] Declaration or statement expected." I've tried wrapping it in a script (both inline and in the header), but neither of these seem to work. JS can't handle the passed in variable.
Actually, you're using Go's template mechanism to prepare the HTML that you send to the browser. This only fills in placeholders and ultimately only produces a string (in this case HTML) that will be sent over the network to the browser for interpretation. It's the browser that can only handle interactivity with the user via events such as "onclick". Furthermore, these events have to be Javascript code, like a JS expression or a function call:
onclick="jsFunction()"
You could make your Go template provide the function to be called in the placeholder:
onclick="{{.JSFuncCall}}"
Here ".JSFuncCall" would have to evaluate to some JS function name (and the parentheses to make the call) that you must have defined in your JS client-side code. That Javascript function could then make an XHR call to the server at some specific URL that triggers a Go handler, runs the Go code you want to run, and then returns a response that you can then handle back in the JS function.

How can I access functions defined in Google Script Editor outside of it?

If I have a function written in Google Spreadsheets script editor that retrieves the data in the spreadsheet in JSON format, how can I access that function outside of the script editor in my own code? I want to access that JSON and manipulate it in my own code. Is there a way to do that using the Spreadsheets API? I format it in a specific way inside script editor so I can't just use the json-in-script provided. In the call (http://spreadsheets.google.com/feeds/feed/key/worksheet/public/basic?alt=json-in-script&callback=myFunc) there's a callback function for myFunc. Can I use the function I defined in the script editor to replace myFunc?
Following your comment that brings some details on your use case, there is a Google-Apps-Script feature specially designed to give access to some functions you wrote from within another script : is is called libraries and is fully described in the documentation.
EDIT, following 2cond comment:
Calling a GS function from a javascript (or any other language) script that is not a Google Script (GS) is not possible if you consider using it as a function...
but
what you can eventually do - depending on the data this function must handle - is to deploy a script as a webApp running as a service and call this service from your external app using the equivalent of an urlFetch (that's the service doing that in GS).
The service will have an url to which you can add parameters and it will return a result that you can use in your local app.
Of course this workflow has a few limitations and might quickly become complex but in many cases it is fully workable.
Note that the url you will have to use in the "versioned" one ending with .exec (Not sure this word is correct but I mean the published url that corresponds to a version of your script and not the ".dev" one that one can use to test a script in GS).
You'll find details about that in the documentation and on many other ressources, including SO. The url is typically something like this :
https://script.google.com/macros/s/AKfycbyw-2WtmF7wsd__________azjImbMWm5YrxB8/exec?someParameter=someValue&otherParam=otherVal // etc...

Can I avoid using global callback function when loading JSON-P?

I want to load my Google Calendar feed on my web page using JSON-P. To make the main content on the page load quicker, I inject the script tag for JSON-P into the head tag only after page is loaded. Google Calendar API returns a script with callback function, see example here.
Is it possible to avoid using global function as a callback function? I would like to wrap all the necessary code for injecting the tag and parsing the results inside one wrapper function.
What you can do is using a method of the global where you application lives.
MYAPP.method({...});
No. When you are using JSON-P calls, the server returns something like below:
callback({...})
After you use <script> element to reference this JSON-P call URL, browser will download the JavaScript code and execute it, which means to execute your callback function using server-provided data.
If you don't want callback function to pollute your global namespace, just add some namespace to your callback function, like com.mycode.callback.
The biggest advantage of using JSON-P is that it can work around Same Origin Policy in browser and you can easily use data from other domains.

Re-register javascript files after ajax callback complete

I have a button on my page that does an Ajax callback with an onclick javascript method.
I want to re-register ALL the Javascripts on the page after the ajax is completed. I have a common init method that I call on ajaxComplete with some functions called after ajax completion.
However, there are several javascripts, and in different files. Is it possible to get all the javascript files (based on the file names on the server) and re-register them onAjax complete?
I am using C#/.NET (.NET ajax) and if there's a way to register the files via code behind (on ajax complete).
Currently I have a javascript method that is called on ajax completion, is there any way to place this inside code-behind to re-register the javascripts?
Personally, I would load all the javascript I needed right off the bat. This alleviates complexity and makes small nuisances like naming conflicts easier to troubleshoot.
However -- if you still want to go with the dynamic loading route, then all you need to do is create a js function that dynamically loads the files you need (multiple ways to accomplish this) and register that function with the scriptmanager on the page for subsequent calling. If you're not keen on using the scriptmanager, then you'll need to call the aforementioned script-loading function you created from within your onAjaxComplete function.
For thoroughness, I should add that there are some really good dynamic script loading libraries available.

AJAX + dynamically adding java script function to the page

I am loading a portion of page using AJAX calls, which may contain script functions defined in them . Which are attached with the controls being loaded with different events. Now the problem is that when those events triggered i got the error "object not found" which indicate the function is not found/defined. While using Firebug i can see that the function is defined and available. So how can i make sure that browser can find the respective function.
I tried but either i am missing some thing or either its not working, Here is what i am doing
Page
--->Partial View A
----->Partial View B
Now Page Loads Partial A with Ajax Calls which further loads Partial B with Ajax Calls.Both Partial A & B contains few java script function that logically only associated with them not with master page.
The pages loads fine except the functions could not execute as "Object Not Found" comes in.
You should define functions using this syntax:
myFunction = function(foo) {}
not this syntax
function myFunction(foo) {}
The second form won't work when eval()'d (which is probably what's happening)
If you are using a framework or library you have to set a parameter in order to evaluate the script present in the response of the ajax request. It's usualy called evalScripts
evalScripts:true
You can also use the callbacks (success/error) of the request to trigger the events, so that it's easier to keep the code in one place and to avoid situations like this.
If you are using plain javasciprt and the XmlHttpObject, then you have to manually find all the script tags in your response and then eval() them.

Categories