baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript
https://baconjs.github.io
MIT License
6.47k stars 330 forks source link

AngularJS and Bacon #357

Closed xgrommx closed 9 years ago

xgrommx commented 10 years ago

Hello @raimohanska

I just tried create bridge between AngularJS and BaconJS. I have some AngularJS module like a:

var app = angular.module('app', []);
app.config(['$provide', function($provide) {
  $provide.decorator('$rootScope', ['$delegate', function($delegate) {
    Object.defineProperties($delegate.constructor.prototype, {
      '$fromBinder': {
        value: function(functionName, listener) {
          var scope = this;
          return Bacon.fromBinder(function(sink) {
            var args = arguments;
            scope[functionName] = function() {
              sink(new Bacon.Next(???));
            };

            return function() {
              delete scope[functionName];
            };
          });
        },
        enumerable: false
      }
    });

    return $delegate;
  }]);
}])

This work so: 1) We can use our click or another event function in angularjs directive like a:

<button ng-click="clicker()">Click me</button>

2) In our controller we can use it like a:

app.controller('MainCtrl', ['$scope', function($scope) {
      $scope.$fromBinder('clicker').map(1).$digest($scope, 'value');
}]);

This to work very well but I don't know what I should provide to new Bacon.Next(???)

For example I just passed a static value like a empty array.

Do you have some advice for me?

xgrommx commented 10 years ago

This is line which contains my problem https://github.com/xgrommx/angular-bacon-bridge/blob/master/app/angular-bacon-bridge.js#L40

phadej commented 10 years ago

Depends what you want to be in the resulting stream:

xgrommx commented 10 years ago

Hello @phadej.

Sure your question is correct. I guess that better way for it should will be so:

Bacon should return to Next method arguments which I passed through callback function.

For example: If my function has notation clicker(100) then it should return 100 or if clicker($event) it should return $event. Later I can change it with map function.

xgrommx commented 10 years ago

Now I just return an empty array.

xgrommx commented 10 years ago

Also could you please explain to me how I can use Bacon.when or Bacon.update?

For example I have several events and I want that my another event has been executed when all events will executed. How can I use Bacon.update or Bacon.when?

pencilcheck commented 8 years ago

I just do "$emit('eventname')" in ng-click, and attach an eventstream using $scope.$asEventStream, working great for me.