Methods for Knockout Event pass parameters

Source: Internet
Author: User

It is not possible to use functions to pass parameters directly in knockout, which will cause the function to be called at initialization time, for example:

<span style= "FONT-SIZE:14PX;" ><div data-bind= "Click:changeeditor ($index)" ></div></span>
will cause the function to be called when it is initialized, clicking on the event Changeeditor () function, which clearly violates the original intention.

There are 2 ways to implement parameter delivery:

1. Method One: Use function wrapping

<div data-bind= "event: {click:function (data, event) {changeeditor (' param1 ', ' param2 ', Data, Event)}}" >    Mous E over me</div>

Click the event response function and set a layer, call the Chageeditor function, in the original Changeeditor () function call to pass in parameters.


2. Method Two: Use the BIND function

<button data-bind= "event: {click:changeEditor.bind ($data, ' param1 ', ' param2 ')}" >    click me</button>

When using this method to pass parameters, the $data is formalized, cannot be changed, and can be followed by a number of parameters, such as param1,param2.

3, the parameter transfer of the exact province

<div data-bind= "event: {mouseover:myfunction}" >    Mouse over me</div> <script type=  "text/ JavaScript ">    var viewModel = {        myfunction:function (data, event) {            if (event.shiftkey) {                //do Something different when user have shift key down            } else {                //do normal action            }}}    ;    Ko.applybindings (ViewModel);</script>
data and event two parameters are always passed by default even if the function in the binding definition does not have any parameters, and data refers to the current ViewModel object, which is the event object.

Note: When passing parameters in bind mode, data and event two parameters are still passed by default, the newly added parameters are ranked first in use , for example, the following definitions:

<span style= "FONT-SIZE:14PX;" ><div data-bind= "Click:changeEditor.bind ($data, $index)" ></div></span>
when used, index is the first parameter.

function Changeeditor (Index,data, event) {alert (' Parameters: ' + index + ', ' + data+ ', ' + event '); alert (arguments.length); var tmp = data; Tmp.headertext = ' OK!!! '; tmp.editing = true;//Columns2[idx] (TMP);}



Methods for Knockout Event pass parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.