knockout.js - Subscribing to values from another viewmodel in knockout js -



knockout.js - Subscribing to values from another viewmodel in knockout js -

i'm trying split view models multiple reuseable view models. have 1 view model contains several dropdowns , 1 button.

var topview = function () { self.dropdowna = ko.observablearray(); self.selecteddda = ko.observable(); self.dropdownb = ko.observablearray(); self.selectedddb = ko.observable(); $.getjson("someapiurl", function (result) { ko.mapping.fromjs(result, {}, self); }); //this builds dropdowna $self.selecteddda.subscribe(function(newvalue) { $.getjson("anotherapi"+newvalue, function (result) { ko.mapping.fromjs(result, {}, self); }); }; // builds dropdownb $self.buttonclicked = function() { alert("i clicked!"); } }

my main viewmodel looks this:

var mainview = function () { var self = this; var topview = ko.observable({ topview: new topview() }); // how selected values topview 1 time user clicks button??? }

how subscribe dropdowna , dropdownb selected values mainview??? please help! give thanks you!

you not need create topview observable long don't want exchange completeley. can create property of mainview , access in bindings:

<button data-bind="click:topview.buttonclicked">click me, i&#39;m button!</button>

topview stays (after fixed using of self , $self without defining them)

mainview that:

var mainview = function () { var self = this; self.topview = new topview(); }

jsfiddle example

knockout.js knockout-mapping-plugin

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -