What is the meaning of this notation:
$("#linka").click(function() {
// some codes
}).fancybox({
// some options
});
What is difference between that and these codes:
$("#linka").click(function() {
// some codes
});
$("#linka").fancybox({
// some options
});
Thank you.
The end result will be the same, but the second code snippet has an unnecessary call to the jQuery function to re-select the element. Most jQuery methods return a jQuery object, with either the same or an updated set of elements, allowing other jQuery methods to be called afterwards.
It's called method chaining, and allows you to select element(s) once and interact with them using multiple methods.
With jQuery you can chain methods instead of having to re-define the selector each time. Both of the code samples above do exactly the same thing. The first method merely omits the unnecessary call to the jQuery function.
you'll get same result for both methods.
chain method concept is applied here with method one.here you don't have to declare the selector again.
in method two,you are declaring the selector twice.
chain method only allows you to not declare the selector again and again.you can bind as many method as you want using chain method.imagine you want hover method for same selector. traditionally,you'll declare the 3 methods with same selector thrice in them.using chain method:
$("#linka").click(function() {
// method1
}).fancybox({
// method2
}).hover(function(){//method3
},function(){
});
Related
I am trying to add an onClick event to a bunch of buttons on screen that I have Instantiated. But the function I am calling needs parameters. When I put the parameters the code doesn’t run but when I don’t it runs but the function doesn’t do what it is supposed to because it doesn’t have the right parameters. Any ideas?
public void PreviewButtonLook(Image previewPart, Sprite previewSprite)
{
previewPart.GetComponent<Image>().sprite = previewSprite;
}
button.GetComponent<Button>().onClick.AddListener(PreviewButtonLook(previewPart, options[i]);
onClick.AddListener() accepts a delegate of zero parameters.
You can create a zero-parameter anonymous function and pass it as a delegate.
This zero parameter anonymous function can call another function and pass it as many parameters as you want.
button.GetComponent<Button>().onClick.AddListener(delegate {
PreviewButtonLook(previewPart, options[i]);
});
But be aware of function closure.
This library I found that handles music playing has the following variable public.
void Function() onCompleted;
I want to change the icon of a button when the track is finished, so that it returns to a play icon.
I tried using musicPlayer.OnCompleted(() { **stuff** }); but that gives me a syntax error Too many positional arguments: 0 expected, but 1 found.
How do I subscribe on that event, or how do I check if OnCompleted has been called?
I am still pretty new to dart but can't wrap my head around this one. I tried subscribing like in Angular or looking up if there's a different syntax for it, but I am at a loss.
Presumably you have to set onCompleted to something, specifically a function taking no parameters and returning void.
It would be normal to provide something like this in the constructor. Is there a named, optional parameter for this? Alternatively, there may be a setter.
Let's assume there's a setter. You could write:
musicPlayer.onCompleted = (){/* do stuff*/};
Using OpenUI5/SAPUI5, per documentation on XML Binding Expressions, we have the ability to execute a function from the view.
new sap.m.CheckBox({
selected: "{= checkSelectedItems(${odata>CustomerId}) }"
})
In my controller behind the view:
checkSelectedItems: function(sCustomerId) {
return true;
}
In my view, I get the generic error as if it cannot find my function:
Uncaught TypeError: Cannot read property 'apply' of undefined
I've tried calling the function in several ways:
{= .checkSelectedItems() }
{= my.namespace.checkSelectedItems() }
I even tried adding a function in a script tag in my index page to see if it only has access to global functions, but I wasn't able to trigger that either. Suggestions? Am I misinterpreting the documentation?
Please see the JS Bin here : http://jsbin.com/sosotacihi/edit?html,output. I've commented out the CheckBox that has the issue, but if you put it in, you'll see the error.
You need to use formatter to call methods of controller from an XML View.
new sap.m.CheckBox({
selected: "{parts:['odata>CustomerId'], formatter:'.checkSelectedItems'}"
});
This can be applied to any event triggering attribute. The generic way to mentioned this is:
{parts:['<parameter1>', '<parameter2>', ...], formatter:'.<methodInController>'}
To reuse a controller function in an expression binding, the complex binding syntax works there as well:
selected="{= ${parts: [{path: 'myModel>property'}], formatter: '.myMethodInController'} === 'foo'}"
Currently, it only works when parts:[{path: ...}] is included. But of course, just because it works, doesn't mean we should use it. As you can see, such an expression binding can become quickly unreadable.
UI5 suggests to stick with a formatter function if the expression binding gets hard to read.
We recommend to use formatter functions instead of very complex and hard-to-read expressions.
Check out this documentation.
The syntax someFn(...) in an expression binding works only if someFn is one of the global symbols, such as Math.max(...) or isNaN(...).
UI5 Suggests to use Expression Binding instead of formatter functions. The Expression binding is mainly for XML views not for JS views.
I have a WinJS class defined as follows and would like to use a member function in the constructor:
WinJS.Class.define(
function() {
setInterval(myMemberFunction, 100);
},
{ // Member variables
myMemberFunction: function() {
// Do something
}
});
Unfortunately it looks like I can't resolve member functions in that manner in the constructor. This code all lives in a module so I could move myMemberFunction up the hierarchy and access it from the constructor, however the drawback is that "this" would no longer refer to the instance of my WinJS class. What's the recommended method for accessing instance members in a WinJS from the constructor?
Generally speaking, you refer to any method or property using "this," as in this.myProperty. In the case of event handlers, you need to make sure that the "this" that you see inside the handler is the instance "this". That's the purpose of the bind method of a function object. So you do this:
setInterval(this.myMemberFunction.bind(this), 100);
This makes sure that you bind the right "this" instance to the callback. Because I've seen this question pop up frequently (use of .bind is all over the Windows SDK samples), I wrote about this in more detail on http://www.kraigbrockschmidt.com/2012/11/28/purpose-function-dot-bind/.
Just to note, this is pure JavaScript; nothing particular to WinJS or Windows Store apps.
I have a Dart js-interop callback that in turn takes a javascript callback as an argument. The dart callback implementation looks like this:
void callBackToDartCode(String query, js.FunctionProxy completionCallback) {
js.context.completionCallback = completionCallback;
doSomethingAscyn(query).then(
(result) {
// hand the query result back to the javascript code
js.context.completionCallback(js.map(result));
});
This works. The key to making this work is to save the FunctionProxy in the js.context so that it is available when it comes time to execute it in the async "then" method. This line of code is important:
js.context.completionCallback = completionCallback;
If that's not done then the completeCallback is not retained and hence cannot be called when the async operation completes.
I have not seen examples like this and I am not sure I have really done this properly.
It raises questions:
How do I disassociate "completeCallback" from js.context after I've called it? Does it remain associated with js.context forever?
It appears there will be conflicting use of the name "completionCallback" within js.context if multiple async operations are in progress at the same time. That strikes me as a common problem. Does js-interop have a way to deal with that or is it my job to manage that?
With js-interop all proxies are scoped to prevent memory leaks. This means that Proxy will lost its JS object reference at the end of its associated scope. If scoped((){}) function is not use explicitely a lazy scope is initialized the first time an interop operation is done and the scope is automatically closed at the end of the current event loop. If you want to make a Proxy to live longer than its associated scope, you have to retain it. This can be done with js.retain(proxy). Once your proxy is no longer needed, you can release it with js.release(proxy).
Thus your code should be :
void callBackToDartCode(String query, js.FunctionProxy completionCallback) {
js.retain(completionCallback);
doSomethingAscyn(query).then(
(result) {
// hand the query result back to the javascript code
completionCallback(js.map(result));
// completionCallback is no longer used
js.release(completionCallback);
});
}
About your question about disassociate "completeCallback" from js.context you could have done it with js.deleteProperty(js.context, "completeCallback")