Created
June 3, 2010 03:23
-
-
Save mattpodwysocki/423397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="mootools.js"></script> | |
<script src="mootools-1.2.4.4-more.js"></script> | |
<script src="rx.js"></script> | |
<script src="rx.mootools.js"></script> | |
<script> | |
window.addEvent('domready', function() { | |
var options = { | |
url: "http://search.twitter.com/search.json", | |
data: { rpp : "100", q : "4sq.com" }, | |
callbackKey: "callback" | |
}; | |
// Works | |
var req3 = new Request.JSONP(options); | |
req3.asObservable.Subscribe(function(data) { alert(data.results[0].text); }); | |
var req2 = Rx.Observable.MooToolsJSONPRequest(options); | |
req2.Subscribe(function(data) { alert(data.results[0].text); }); | |
// Doesn't work | |
var req1 = new Request.JSONP(options); | |
req1.toObservable().Subscribe(function(data) { alert(data.results[0].text); }); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() | |
{ | |
var root; | |
if (typeof ProvideCustomRxRootObject == "undefined") { | |
root = this.Rx; | |
} | |
else { | |
root = ProvideCustomRxRootObject(); | |
} | |
var observable = root.Observable; | |
observable.MooToolsJSONPRequest = function(options) { | |
var subject = new root.AsyncSubject(); | |
var request = null; | |
try { | |
options.onSuccess = function(data) { | |
subject.OnNext(data); | |
subject.OnCompleted(); | |
}; | |
options.onFailure = function(xhr) { | |
subject.OnError({ kind: "failure", xhr: xhr }); | |
}; | |
options.onException = function(headerName, value) { | |
subject.OnError({ kind: "exception", headerName: headerName, value: value }); | |
}; | |
request = new Request.JSONP(options); | |
request.send(); | |
} | |
catch(err) { | |
subject.OnError(err); | |
} | |
var refCount = new root.RefCountDisposable(root.Disposable.Create(function() { | |
if(request) { | |
request.cancel(); | |
} | |
})); | |
return observable.CreateWithDisposable(function(subscriber) { | |
return new root.CompositeDisposable(subject.Subscribe(subscriber), refCount.GetDisposable()); | |
}); | |
} | |
Request.JSONP.implement({ | |
toObservable : function() { | |
var subject = new root.AsyncSubject(); | |
var self = this; | |
try { | |
this.options.onSuccess = function(data) { | |
subject.OnNext(data); | |
subject.OnCompleted(); | |
}; | |
this.options.onFailure = function(xhr) { | |
subject.OnError({ kind: "failure", xhr: xhr }); | |
}; | |
this.options.onException = function(headerName, value) { | |
subject.OnError({ kind: "exception", headerName: headerName, value: value }); | |
}; | |
this.send(); | |
} | |
catch(err) { | |
subject.OnError(err); | |
} | |
var refCount = new root.RefCountDisposable(root.Disposable.Create(function() { | |
self.cancel(); | |
})); | |
return observable.CreateWithDisposable(function(subscriber) { | |
return new root.CompositeDisposable(subject.Subscribe(subscriber), refCount.GetDisposable()); | |
}); | |
}, | |
asObservable : function() { | |
return observable.MooToolsJSONPRequest(this.options); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The property this.options is considered private. You should use this.setOptions({ key: value }) to change an option. However, since you're just adding events you should be calling this.addEvents() directly. http://gist.github.com/423646