Expose knockout ViewMode from function to another function - javascript

I have next situation...
For some reasons I need to bind knockout ViewModel inside function and call it on specific terms.
this is my code:
if (... some conditions ...) {
var polugodiste = $("#polugodiste").val();
ApplyBindingsIzostanak(polugodiste);
$('#flip-min').change(function () {
IzostanakViewModel.selectedPolugodiste(parseInt($(this).val()));
IzostanakViewModel.GetIzostanci();
});
}
and function:
function ApplyBindingsIzostanak(polugodiste)
{
var Izostanak = function (cas, tekst) {
this.Cas = cas;
this.Tekst = tekst;
};
var IzostanakViewModel = {
selectedStatus: ko.observable(),
selectedPolugodiste: ko.observable(polugodiste),
ucenikIzostanakList: ko.observableArray([]),
GetIzostanci: function () {
.. do some code ...
}
};
ko.applyBindings(IzostanakViewModel);
}
Binding is working, but I get error when I try calling IzostanakViewModel inside my if, it says IzostanakViewModel is not defined.
Can I and how expose IzostanakViewModel from function and use it inside if statement?
NOTE*
I could try something like this:
add this code to ApplyBindingsIzostanak():
window.foo = function() {
IzostanakViewMode.GetIzostanci();
}
and then call it from if statement, but maybe there is better solution...

IzostanakViewModel is a variable within the ApplyBindingsIzostanak() function. Why don't you just return it so you have a reference to it?
function ApplyBindingsIzostanak(polugodiste)
// ...
return IzostanakViewModel;
}
var IzostanakViewModel = ApplyBindingsIzostanak(polugodiste);
$('#flip-min').change(function () {
IzostanakViewModel.selectedPolugodiste(parseInt($(this).val()));
IzostanakViewModel.GetIzostanci();
});

Related

Trying to access a injected dependency outside controller method

Is there possible to access a injected dependency on controller outside on it?
function clienteCreateController(ClientesService, recuperarEndereco) {
var vm = this;
vm.pesquisarCep = pesquisarCep;
}
function pesquisarCep(cep) {
recuperarEndereco.find(cep)
.success(function(data) {
parseEndereco(data).bind(this);
})
.error(function(err) {
// showAlertDanger(vm, 'Cep inválido.');
console.log(err);
});
}
I'm calling the method from a button click.
Thanks
I do not think you can access the parameter recuperarEndereco in the outside function pesquisarCep. Because inside the execution context in function pesquisarCep, the variable is recuperarEndereco is not declared at all. Same as this JSFiddle: http://jsfiddle.net/dLhmozf3/. It reports error:
function outsite () {
console.log('param: ' + param);
}
var f = function (param) {
var me = outsite;
me();
};
f();
You need to define the outside function like: function pesquisarCep(cep, recuperarEndereco) { .... And call it like pesquisarCep(cep, recuperarEndereco).

function expression with namespace

Actually I am trying to have a namspace [to avoid global variables] without object creation and trying to do the below. But, the execution of the methods data.sum and data.calc.mult with alerts is not getting called. Instead the empty declarations inside data are called. Can I anyone help me know why this is happening?
var data = {
name: "",
sum: function() {},
calc : {
mult: function() {}
}
};
data.sum();
data.calc.mult();
data.sum = function () {
alert('sum');
};
data.calc.mult = function () {
alert('mult');
};
If you put your data.sum() after data.sum function redefinition, you will get the alert box.
Define it like this (function definition within object definition):
var data = {
sum: function () {
alert('sum');
},
mult: function () {
alert('mult');
}
};
data.sum();
data.mult();

combine multiple functions with same code in jquery

Yes, I have thoroughly searched google and did not find anything that suits my requirement.
The code i have so far is at the link below:
http://jsfiddle.net/ZKwTY/4/
There are multiple onchange events which call almost the same code, i would like to combine them maybe in a comma separated fashion to call it only once.
something like this
(on1Change, on2Change, on3Change): function () {
this.loadData();
}
is this possible??
Note: these functions are bound to the controls via a framework over which i do not have control, i need to create these functions and the framework would bind these to the respective controls
or you can create your object like this
var ol = {
on1Change: this.loadData,
on2Change: this.loadData,
on3Change: this.loadData,
on4Change: this.loadData,
loadData: function () {
this.loadData1();
this.loadData2();
},
loadData1: function () {
alert('hi from loadData1');
},
loadData2: function () {
alert('hi from loadData2');
}
};
Then if you want to do it once, then declare a object
var ol = {
loadData: function () {
this.loadData1();
this.loadData2();
},
loadData1: function () {
alert('hi from loadData1');
},
loadData2: function () {
alert('hi from loadData2');
}
};// end of object
ol.on1Change = ol.on2Change = ol.on3Change = ol.on4Change = ol.loadData;
add all propteries dynamically after object declaration
use bind()
$("selector").bind(on1Change, on2Change, on3Change): function () {
this.loadData();
}.....
you can try somethig like this http://jsfiddle.net/s4VVY/
i.e. add methods after object create
[1,2,3,4,5].forEach(function(it){ol["on"+it+"Change"] = function(){this.loadData()}})
UPDATE
may be this help
var ol = (function(){
var o = {
loadData: function () {
this.loadData1();
this.loadData2();
},
loadData1: function () {
alert('hi from loadData1');
},
loadData2: function () {
alert('hi from loadData2');
}
}
o.on1Change=o.on2Change=o.on3Change=o.on4Change=function(){ this.loadData();};
return o;
})()
also you can make function bindFunc
function bindFunc(){
var obj = arguments[0],
handler = arguments[1],
properties = Array.prototype.slice.call(arguments,2);
for(var i in properties){
obj[properties[i]] = handler;
}
}
and call as
bindFunc(o,function(){this.loadData();},"on1Change","on2Change","on3Change","on4Change")

JS/ use object which define outside the function

I have the next function in JS:
function status(){
this.functionA = function(){}
//Some others function and fields
}
and I have another function:
function create(root){
var server = libary(function (port) {
//Here some functions
});
var returnValue = {
current:status(),
cur:function(port){
current.functionA();
}}
return returnValue;
}
when I call current.functionA(), It says that current is undefined. How can I call functionA()?
When you have a function-constructor like status(), you'll need to call new for it. I've revised part of your code here.
var returnValue = {
current: new status(),
cur:function(port){
current.functionA();
}}
return returnValue;
}
Just to differentiate; create() doesn't need a new statement because you're actually creating and returning an object to refer to, inside of the function.
function status(){
this.functionA = function(){alert("functionA");}
}
function create(root){
var returnValue = {
current:status.call(returnValue),
cur:function(port){ this.functionA(); }.bind(returnValue)
}
return returnValue;
}
create().cur(999);
I corrected your issue using the JavaScript "call" and "bind" methods which are part of the Function prototype.

Issue while defining jQuery functions

Trying to define a couple of functions like so:
user = (function() {
var friends_list = (function() {
$.get('/ajax/user/friends_list', function(data) {
......
So I can later on call them when need it like so user.friends_list() but for now, the only thing I get is this following error:
TypeError: Object function () {
var friends_list = (function() {
$.get(....
I just don't know where else to look, any suggestions?
You need to create user as an object, in your case the friends_list is a closure method, it will be availble outside the function
user = {
friends_list : function(){
....
}
}
make a user object and not function
var user = {
friends_list : function(){
$.get('/ajax/user/friends_list', function(data) {
......
}
}
and call it like.. user.friends_list()
fiddle here
You're using a closure here, so friend_list is invisible on the outside of user.
If you want to use closures, to hide some variables, to best way to export friend_list would be:
(function(){
var somePrivateVariable;
window.user = {};
window.user.friend_list = function() {
// make use of somePrivateVariable...
};
})();
user = function() {
this.friends_list = function() {
$.get('/ajax/user/friends_list', function(data) {
......
});
};
return this;
};
Above should also work.
reference http://www.w3schools.com/js/js_objects.asp
You can check out this link
Here is the code:
var global = {};
global.sayHello = function (x){
//do your code here
console.log('Hello ' + x );
};
global.sayHello('Kevin');
user = new function(){
var private_variable;
function private_method(){}
this.global_variable = '';
this.global_method = function(){};
}

Categories