RxJS in AngularJS Services -


i'm learning rxjs , trying implement in 1 of existing applications. first thing trying remove rootscope.broadcast/emit methods , replace them behaviorsubjects. has worked fine if events subscribed inside controller. however, if try subscribe events in service, never fire. can move exact same subscription component/controller/etc , works fine.

is there reason or should able , doing wrong?

update 1

i have event service maintains events run @ top of application so:

(function () { "use strict"; angular.module("app.common").service("eventservice", eventservice);   function eventservice($window) { "nginject";  var defaultbehaviorsubjectvalue = null;  var service = {   activate: activate,   onlogin: new rx.behaviorsubject(defaultbehaviorsubjectvalue),                     //onnext authservice.login   onlogout: new rx.behaviorsubject(defaultbehaviorsubjectvalue),                    //onnext authservice.logout      isonline: new rx.behaviorsubject(defaultbehaviorsubjectvalue)                    //onnext authservice.logout          };  return service;  function activate() {   service.onlogin.subscribe(function (userdata) {     console.info("user logged in");   });    service.onlogout.subscribe(function (userdata) {     console.info("logging user out");     dispose();   });    $window.addeventlistener("offline", function () {     console.info("lost internet connection, going offline");     service.isonline.onnext(false);   }, false);    $window.addeventlistener("online", function () {     console.info("regained internet connection, going online");     service.isonline.onnext(true);   }, false); }  function dispose() {   angular.foreach(service, function (event, index) {     if (service && service[event] && service[event].isdisposed === false) {       service[event]();     }   }); } 

} })();

i have service waiting onlogin event fire. code simliar to:

(function () { angular.module("app.data").service("offlineprojectdataservice", offlineprojectdataservice);  function offlineprojectdataservice(definitions, metadataservice, userservice, eventservice) {     "nginject";      var onloginsubscription = eventservice.onlogin.subscribe(function (isloaded) {         if (isloaded) {             activate();         }     });      var service = {         activate: activate     }      return service;      function activate(){         //..some stuff     } } 

})();

the problem i'm having onlogin event not firing activate method not being called. if move exact same subscription line other controller in app, fire. don't think there wrong syntax.

unless of course i'm missing here painfully obvious somebody.

use rxjs in service

build service rxjs extensions angular.

<script src="//unpkg.com/angular/angular.js"></script> <script src="//unpkg.com/rx/dist/rx.all.js"></script> <script src="//unpkg.com/rx-angular/dist/rx.angular.js"></script> 
var app = angular.module('myapp', ['rx']);  app.factory("dataservice", function(rx) {   var subject = new rx.subject();    var data = "initial";    return {       set: function set(d){         data = d;         subject.onnext(d);       },       get: function get() {         return data;       },       subscribe: function (o) {          return subject.subscribe(o);       }   }; }); 

then subscribe changes.

app.controller('displayctrl', function(dataservice) {   var $ctrl = this;    $ctrl.data = dataservice.get();   var subscription = dataservice.subscribe(function onnext(d) {       $ctrl.data = d;   });    this.$ondestroy = function() {       subscription.dispose();   }; }); 

clients can subscribe changes dataservice.subscribe , producers can push changes dataservice.set.

the demo on plnkr.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -