javascript - Knockout subscribe/event type system without observable? -


i want make use of subscribe() function of knockout js manually trigger event @ point.

i make observable() , everytime put guid in there trigger scubscribe.

is there cleaner way within knockout js have typical event-like structure?

edit ok, apparently can use observable.valuehasmutated() - might a bit cleaner using guid.

example

this behaviour i'm looking for:

function car() {     var self = this;      self.onopendoor = ko.observable();      self.opendoor = function()     {         // using observable / valuehasmutated feels bit hacky         // there other way use underlying subscribe() system?         self.onopendoor.valuehasmutated();     } }   var car = new car();  // multiple subscribers car.onopendoor.subscribe(function() {     console.log('do something'); }) car.o**nopendoor.subscribe(function() {     console.log('do else'); })  car.opendoor(); 

i aware not default 'knockout' way stuff - not question about.

update

after @royj's reference niemeyer's blog went solution:

function car() {     var self = this;      self.onopendoor = new ko.subscribable();      self.opendoor = function()     {         self.onopendoor.notifysubscribers();     } } 

update if you're looking clarity, can use notifysubscribers instead of valuehasmutated. might want take @ base type of observables, ko.subscribable.

i this:

var vm = {     ///...definitions...     opencount: ko.observable(0),     opendoor: function () {         vm.opencount(vm.opencount()+1);     } };  vm.opencount.subscribe(function () {     ///...do });  vm.opencount.subscribe(function () {     ///...do else });  ko.applybindings(vm); 

demo http://jsfiddle.net/uoqdfhdb/2/


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -