Permalink
Browse files

* Explicitly need eval() around $.async again.

  • Loading branch information...
1 parent e8c3fcb commit 1b234c29cc62154c9bc926f01229bebd77bfa145 @audreyt committed Jun 10, 2012
Showing with 30 additions and 20 deletions.
  1. +3 −3 README.md
  2. +2 −2 example.js
  3. +15 −8 jscex-jquery.js
  4. +1 −1 src/example.ls
  5. +9 −6 src/jscex-jquery.ls
View
6 README.md
@@ -7,7 +7,7 @@ jscex-jquery
$ = require('./jscex-jquery');
// Define an Async function with a special $await keyword in it
-var fetch = $.async(function(url) {
+var fetch = eval $.async(function(url) {
// $await takes anything that returns a Promise
var it = $await( $.get(url) );
// ...do something with it...
@@ -21,10 +21,10 @@ fetch('http://.../').done(...).fail(...).always(...);
# Description
This module exports `$.Jscex` as a jQuery plugin, providing an
-`$.async` helper that turns a regular function into one that
+`$.async` helper that compiles a regular function into one that
returns a Promise object.
-Functions defines with `$.async` has access to an extra `$await`
+Functions defined with `$.async` has access to an extra `$await`
keyword, which can implicitly wait for other Promise objects.
Please see `example.js` for a sample usage, and type `make demo`
View
4 example.js
@@ -5,7 +5,7 @@
console.log(it);
console.log('');
};
- fetch = $.async(function(url){
+ fetch = eval($.async(function(url){
var it, a, title, text;
it = $await($.get(url));
a = $(it).find('h2:first a');
@@ -16,7 +16,7 @@
title: title,
text: text
};
- });
+ }));
say('[Demo: Fetching invalid host, expecting ENOENT...]');
fetch('http://blog.zhaojie.error').fail(function(it){
return say(it.responseText);
View
23 jscex-jquery.js
@@ -1,23 +1,30 @@
(function(){
- var Jscex, $, XMLHttpRequest, AsyncBuilder, __ref;
- Jscex = require('jscex');
- require('jscex-jit').init(Jscex);
- require('jscex-async').init(Jscex);
+ var $, XMLHttpRequest, Jscex, AsyncBuilder, __ref;
if (typeof module != 'undefined' && module !== null) {
module.exports = $ = require('jquery');
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
$.support.cors = true;
$.ajaxSettings.xhr = function(){
return new XMLHttpRequest;
};
+ Jscex = require('jscex');
+ require('jscex-jit').init(Jscex);
+ require('jscex-async').init(Jscex);
} else {
($ != null
? $
- : $ = typeof jQuery != 'undefined' && jQuery !== null
- ? jQuery
- : {}).Deferred == null && (function(){
+ : $ = (__ref = this.$) != null
+ ? __ref
+ : (__ref = this.jQuery) != null
+ ? __ref
+ : {}).Deferred == null && (function(){
throw new Error("$.Deferred not available -- Please include jQuery 1.5+");
}());
+ Jscex == null && (Jscex = (__ref = this.Jscex) != null
+ ? __ref
+ : (function(){
+ throw new Error("Jscex not available -- Please include jscex.min.js");
+ }()));
}
/* Convert a Promise (Q, jQuery, Dojo) object into a Task */
((__ref = Jscex.Async).Binding || (__ref.Binding = {})).fromPromise = function(p){
@@ -82,7 +89,7 @@
deferred Promise object representing its result.
*/
$.async = function(cb){
- return eval(Jscex.compile('async-jquery', cb));
+ return Jscex.compile('async-jquery', cb).replace(/(Jscex.builders\["async-jquery"\])/, '$.$1');
};
/* Turn off Jscex logging by default */
Jscex.logger.level = 999;
View
2 src/example.ls
@@ -1,7 +1,7 @@
$ = require './jscex-jquery'
say = !-> console.log it; console.log ''
-fetch = $.async (url) ->
+fetch = eval $.async (url) ->
it = $await $.get url
a = $(it) .find 'h2:first a'
title = a.text!
View
15 src/jscex-jquery.ls
@@ -1,14 +1,14 @@
-Jscex = require \jscex
-require \jscex-jit .init Jscex
-require \jscex-async .init Jscex
-
if module?
module.exports = $ = require \jquery
{ XMLHttpRequest } = require \xmlhttprequest
$.support.cors = yes
$.ajaxSettings.xhr = -> new XMLHttpRequest
+ Jscex = require \jscex
+ require \jscex-jit .init Jscex
+ require \jscex-async .init Jscex
else
- ($ ?= jQuery ? {}).Deferred ? throw new Error "$.Deferred not available -- Please include jQuery 1.5+"
+ ($ ?= @$ ? @jQuery ? {}).Deferred ? throw new Error "$.Deferred not available -- Please include jQuery 1.5+"
+ Jscex ?= @Jscex ? throw new Error "Jscex not available -- Please include jscex.min.js"
/* Convert a Promise (Q, jQuery, Dojo) object into a Task */
Jscex.Async.@Binding.fromPromise = (p) ->
@@ -49,7 +49,10 @@ Jscex.modules.\async-jquery = true
Once invoked, we implicitly start the task, and return a
deferred Promise object representing its result.
*/
-$.async = (cb) -> eval Jscex.compile \async-jquery, cb
+$.async = (cb) -> Jscex.compile(\async-jquery, cb).replace(
+ /(Jscex.builders\["async-jquery"\])/
+ '$.$1'
+)
/* Turn off Jscex logging by default */
Jscex.logger.level = 999

0 comments on commit 1b234c2

Please sign in to comment.