I have the following code:
$.getJSON('getAllTerminals.json', renderTerminalsOnMapAndFitBounds.bind({index:globalRequestCounter++, navigateToTypedText:true}))
...
function renderTerminalsOnMapAndFitBounds(data, updateSelectedTerminals) {
renderTerminalsOnMap.call(this,data);
fitBounds();
if(this.navigateToTypedText === true){
navigateMapToTypedAdress();
}
if (updateSelectedTerminals) {
$.getJSON('getSelectedTerminals', {}, function (json) {
window.MARC.addTerminalPage.terminalsSelected = json;
update();
initPage();
});
}
}
Can you advise me how to make that all works as now but to renderTerminalsOnMapAndFitBounds was passed updateSelectedTerminals as true ?
No, you cannot use bind to partially apply non-initial parameters (and there's no flip). Just use a function expression:
$.getJSON('getAllTerminals.json', function(data) {
renderTerminalsOnMapAndFitBounds.call({
index:globalRequestCounter++,
navigateToTypedText:true
}, data, true);
});
If you have to use bind, either change the parameter order of renderTerminalsOnMapAndFitBounds, or make it accept that updateSelectedTerminals parameter as a property of the this object.
Related
I'm developing a jQuery plugin, and I want to know if there's any possibility to get the second parameter without set the first parameter.
MCVE:
https://jsfiddle.net/2fgb5agL/
//jQuery
$.fn.myPlugin = function(param1,param2) {
return this.each( function() {
if (param2) {
alert("param2 ok");
}
});
};
//Call the plugin
$("#call").click(function() {
$(this).myPlugin("", "xyz");
});
What I mean is: it's possible to call $(element).myPlugin("xyz") and expect the plugin recognize a defined string (ex. "xyz") and then 'do something' (call other function).
So, I don't gonna need to call $(element).myPlugin(undefined,"xyz") for get the second param without setting the first one.
Thanks for reading.
p.s.: What I want to achieve is a cleaner code.
Unless the parameter types are strictly different, you can't even create a distinction inside the function.
Is usual to use a single object parameter as argument like so:
//jQuery
$.fn.myPlugin = function(options) {
return this.each( function() {
if (options.param2) {
alert("param2 ok");
}
});
};
$("#call").click(function() {
$(this).myPlugin({ param1: "", param2: "xyz" });
});
This way you can chose which argument to pass for your plugin. For example: $(this).myPlugin({ param2: "xyz" });
In an object I have a method where I want to get some information from a server(JSON format). I want to add this data to my object where the function is (By using a setter).
The problem is that this isn't my object but the jquery callback. How could/should I solve this?
function anObject() {
$.get(URL, doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
You can use bind:
$.get(URL,doTheCallback.bind(this));
The bind() method creates a new function that, when called, has its
this keyword set to the provided value, with a given sequence of
arguments preceding any provided when the new function is called.
Or assign your scope in a variable like:
function anObject() {
var that = this;
$.get(URL, doTheCallback);
function setExample(example) {
that.example = example;
}
function doTheCallback(data) {
that.setExample(data.results[0].example);
}
}
If you switched to $.ajax, you can use the context option.
function anObject() {
$.ajax(URL, {context: this}).done(doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
bender: function () {
var context = {
pageName: 'Chocolatte candy'
},
partials = {
header: this.globalPartials.header,
tabbar: this.globalPartials.tabbar
};
addBackbuttonIphone();
$(this.el).html(templates["monkey"].render(context, partials));
return this;
}
});
return monkeyView;
});
in another location i have a js file that has the following function
function addBackbuttonIphone () {
context.backButton="#more";
}
If i just add context.backButton="more" in var context directly it works . However if i use the addBackButtonIphone function to do the same it does not work . I am sure that this function is being called however, it is not giving the "#more" value to context.backButton.
Please help
Thanks
modify your function signature to accept an argument called context, like this:
function addBackbuttonIphone(context) {
context.backButton="#more";
}
then pass context into your function, like this:
addBackbuttonIphone(context);
I want to pass one parameter to a function called ForPaste().
My function is given below:
var regex = /^[A-Za-z0-9ĀĒĪŌŪāēīōū\.\-\~\`\'' ]*$/;
var SalaryRegex = /^[A-Za-z0-9\,\.\/\$ ]*$/;
$.fn.ForPaste = function () {
return this.each(function () {
$(this).bind('input propertychange', function () {
var value = $(this).val();
if (!regex.test(value)) {
$(this).val("");
}
});
});
};
This function is in a common JS file. It is called on individual pages. I want to test the regex depending on the parameter passed. So can I know the method about to call the ForPaste() function with the parameter.e.g $("#Text1").Forpaste('FromForm1'); and I get this FromForm1 in the ForPaste() function.
You define a formal parameter for your function.
// formal parameter--v
$.fn.ForPaste = function (the_value) {
alert( the_value ); // displays the argument passed
// rest of your code
};
Whatever value was passed to ForPaste() will be referenced by the_value. (Of course you can change the name to any valid identifier.)
not sure what you are trying to do, because the answer seems so obvious. Will this do?
$.fn.ForPaste = function (theName) {
if (theName.test(//)){
return this.each(function () {
....
});
}
return false
};
you can access the parameters by looking at arguments which isn't an array, but seems to be one. from within the function you can do an var theName=arguments[0] to get the value of the first parameter
I can't seem to get back on track with this one. I simply put a function in a variable and want to call it later, providing it with a parameter:
var logic = function(itemId) {
console.log(itemId);
};
jQuery("#flipright").click(function() { logic.apply(1); } );
This prints "undefinded".
What am I missing?
Simply call logic(1).
If you want to pass a context, you can use call or apply :
logic.apply(context, [1]);
// or
logic.call(context, 1);
You should use apply or call if you want to pass a context to another function - meaning that the this keyword in the called function will refer to whatever context you are passing to it.
Here's a scenario :
var logic = function(itemId) {
console.log(this,itemId);
};
jQuery("#flipright").click(function() {
// output to console the current jquery object and "1"
logic.call(this,1);
});
Make it:
jQuery("#flipright").click(function() { logic(1); } );
ref for apply: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply