| Page 1 of 1 | [ 6 posts ] |
Marco AsbreukYUI Contributor
|
Hi,
I'm using a common login-feature (username-password). After the visitor enters its logincode, script-code must be inserted and executed. This has to be non-cacheable, because the visitor can log-out and login again with different loginlevel (administrator for example). This has to lead to different injected scriptcode. The code will be generated dynamically on the server. I thought of 3 possible ways, but all fail me: 1. using Y.Get.script(url), where url has a timestamp (to prefend caching). Problem is: I have got no reference of the YUI instance in the returned code. 2. using IO-module with a timestamped url. Problem is: onSuccess returnes o.responseText as clear text. I don't know how to execute this as script code. 3. using a custom module: Here I suffer the problem that the loader won't reload the module over and over again. It is cached. I thought of timestamping the url, but within the configurationobject this seems not possible. I also tried the force-field within the configurationobject. But this doesn't seem to force the module to be loaded all over again. So my question is: which way is the best way to use. And how to get it work. Any advise will be helpful. Marco Asbreuk. |
|
Hey Marco,
You're in the right track. All 3 methods can work with a little bit of tweaking. And I will propose a 4th method which I believe is the right thing to do. But first, I just want to confirm that this JS code is not hosted in a CDN, hence it will not be cached by the browser. Correct? 1. you can always have a global reference to Y, specially if you control your APP. 2. you can use dispatcher-like method to inject a chunk of online js in the current page, in this case the response from the io call. 3. you can set up your Y config to load the module every time you call Y.use('my-module' ...). There is a configuration for that: YUI_config = { force: 'my-module' }; more info here: http://developer.yahoo.com/yui/3/api/co ... erty_force 4. (proposal) Now, functionalities (feature code) should not be loaded multiple times, you will be wasting bandwidth, instead you should do some sort of data driven functionality. And jsonp is the natural solution for that. You load a chunk of data over the wire, and this data will be passed to a function defined within the Y closure. JSONP is now part of the YUI core modules. It supports cross domain and it will never cache the content. Best Regards, Caridy |
Marco AsbreukYUI Contributor
|
Hi Caridy,
First: indeed, the code is not on cdn: I can make it uncachable. Reading your reply, I agree the 4th option is best. I didn't take a look into jsonp yet. Anyway, still need a solution of how to execute js from the callback-function. So when an administrator logs in, something else should happen comparing to a normal member. I thing the solution would be something like: Code: function handleJSONP(response) { Y.executeScript(response.scriptToExecute); // obviousely Y.executeScript isn't the wright functioncall // when administrator, response.scriptToExecute == 'Y.use('myMemberModule', function(Y){doMakeAllObjectsDeletable();})'; // when not administrator, response.scriptToExecute == 'Y.use('myMemberModule', function(Y){doMakeSomeObjectsVisible();})'; } Y.jsonp(url, handleJSONP); You talked about dispatcher-like method. How does this work? (By the way, YUI_config = { force: 'my-module' }; didn't prevent my custom module from loading every timeā¦) Kind regards, Marco. |
Marco AsbreukYUI Contributor
|
Caridy,
Probably, when executing script, I also need the global reference to Y. Perhaps a stupid question, but I don't know how to get this: calling YUI or Y outside the instance doesn't work... Could you give me a glue? Thanks, Marco. |
|
Hey Marco,
JSONP is actually a solution to avoid execution, the browser does that for you, and you will get a reference to the json object. Just do this: Y.jsonp(url, function(data) { // data is the json object you pass to the callback method at the server side. // You still have access to Y here too }); Your api should response with something like this in php: Code: <?php echo $_GET['callback'] . ' ( ' . json_encode( $data ) . ' ) '; ?> Internally, JSONP module adds an script tag in the page, and set the url to: http://your.domain.com/api?callback=YUI ... 4187597423 Internally, the library will create a global function under this reference: YUI.Env.JSONP.yui_3_3_0_1_1294184187597423 Which means that the browser will parse and execute the response from your API as a regular script, calling YUI.Env.JSONP.yui_3_3_0_1_1294184187597423, and passing data structure as the first parameter. This dynamic function will be responsible for calling the callback function that you pass as a second argument on Y.jsonp. That's all about jsonp. Best Regards, Caridy |
Marco AsbreukYUI Contributor
|
Hi Caridy,
Wow, didn't figure that one out yet. This will help me out. Thanks a lot! Kind regards, Marco. |
| Page 1 of 1 | [ 6 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group