How can I bind click event in repeater element in winjs?

Eriendel

I can create repeater by this way:

    <div class="gameField" id="gameField" data-win-control="WinJS.UI.Repeater" data-win-options="{ data: GameField.cells}">
        <div class="gameCell">
            <img data-win-bind="src: symbol GameField.cellSymbolToImg" width="100" height="100" />
        </div>
    </div>

and js

    var Cell = WinJS.Class.define(function(number) {
    this.number = number;
    this.symbol = "";
});

var getCellsList = function () {
    var cells = [];
    for (var i = 1; i < 10; i++) {
        var cell = new Cell(i);

        cells.push(cell);
    }
    return cells;
};

var cellsList = new WinJS.Binding.List(getCellsList());
WinJS.Namespace.define("GameField", {
    cells: cellsList,
    cellSymbolToImg: WinJS.Binding.converter(function (symbol) {
        return symbol == "" ? "" : "symbol_" + symbol + ".png";
    })
});

But how can I bind click event to some function so that I could access underlying cell object from within that function?

Kraig Brockschmidt - MSFT

This is straightforward to do using the function.bind method, and you can even have the handler directly in your Cell class.

var Cell = WinJS.Class.define(function(number) {
    var e = document.createElement("div"); //...and initialize however.
    this.element = e;
    e.winControl = this;

    this.number = number;
    this.symbol = "";
    //Also consider using pointerDown event here instead of click; might respond quicker
    e.addEventListener("click", this._click.bind(this));
}, {
    //Instance members
    _click: function (e) {
        //Your handler. "this" will be the instance of Cell that was clicked
    }
});

The this._click function, as you can see, refers to the instance member Cell._click. Calling this function's bind method is how you then specify the exact object to use as "this" inside that method. Without the call to .bind(this), the handler would still be invoked but "this" would be the global context and not the object you care about.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I bind the click event in ItemsControl of UWP?

How to bind click event to element is created on click event?

How i can emit event from directive by click outside of element?

How can i get mouse click event element in cocoa

How can I add an on click event on a div with multiple element inside

How can I get selected text on a element click event?

How can i bind an extra click event to a hyperlink using jQuery 1.4?

How can I bind click event to an angular2 template interpolation?

How can I trigger the click event of another element in ng-click using angularjs?

How can I prevent triggering an on click event when the actual mouse click didn't start on the target element?

How to prevent an a element's click event what bind by angular?

c# - how to generate click event in repeater?

jaxb: How can I bind nested element

In mithril-js, how can vnode bind to click event

How can I bind a onclick event to a checkbox INSTEAD of a change event?

How can I add click event to kendoGridCellTemplate

How can I trigger a JavaScript event click

How can I delay a click to scroll event?

How I can get a click event

How I can prevent the click event on a link which is nested on another element which also use onclick?

How can I add a click event to li element listview in Phonegap JQM?

Android listview: there is a header of listview, how can I add a click event to a textview element?

How can I add a double click event on a flatpickr-day element

How can I add a click event on an individual Element in a mapped array in react

How to bind a click event in iOS?

Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

Bind click event to D3 element

Protractor click element by repeater

How to bind event to element in iframe?