问题
I was trying baconjs's tutorial. https://baconjs.github.io/tutorials.html#content/tutorials/2_Ajax
But, I got the error at "Bus.plug"
var cart = ShoppingCarEt([])
var cartView = ShoppingCartView(cart.contentsProperty)
var newItemView = NewItemView()
cart.addBus.plug(newItemView.newItemStream)
Error:
Uncaught Error: not an Observable : [object Object]
shopBundle.js:145 assertObservable
shopBundle.js:2650 Bus.plug
I use follows
- baconjs@0.7.53
- jquery@2.1.3
- bacon-jquery-bindings@0.2.8
- webpack 1.7.3
What am I doing wrong? Thanks.
Edit: 2015/3/25
Cause is that newItemView.newItemStream is not Observable.
(newItemView.newItemStream instanceof Bacon.Observable
returns false.)
And newItemView.newItemStream is EventStream
EventStream {takeUntil: function, sampledBy: function, combine: function, flatMapLatest: function, fold: function…}
Isn't all EventStream an Observable?
I have made newItemStream as follows:
var $button = $('#addButton');
var $nameField = $('#nameText');
var newItemProperty = Bacon.$.textFieldValue($nameField);
var newItemClick = $button.asEventStream('click');
var newItemStream = newItemProperty.sampledBy(newItemClick);
Following is work fine. It was my miss when first time question.
/* And, I try more simple code. It has same error. */
var someStream = Bacon.interval(1000).map(function() {
return new Date().getTime();
});
var bus = new Bacon.Bus();
bus.log();
bus.plug(someStream);
回答1:
It was caused by "bacon-jquery-bindings"(https://www.npmjs.com/package/bacon-jquery-bindings)
var Bacon = require('baconjs');
var $ = jQuery = require("jquery");
Bacon.$ = require("bacon-jquery-bindings"); <-
It seems to overwrite asEventStream function.
We should use "bacon.jquery"(https://www.npmjs.com/package/bacon.jquery)
来源:https://stackoverflow.com/questions/29090514/bacon-js-bus-plug-uncaught-error-not-an-observable-object-object