敲除数据绑定点击 [英] Knockout data-bind click

查看:129
本文介绍了敲除数据绑定点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解我公司使用的第三方网站上的按钮。

 < a class =btn btn-large btn-bluehref = JavaScript的:无效(0); 
data-bind =click:$ root.clickAction.bind($ data,ActionType)>
< span data-bind =text:Title>注册< / span>< / a>

我希望在我们公司内部网站的某个页面上提供此按钮,但我不熟悉与淘汰赛。我了解GET请求,所以如果按钮做了这样的事情,那么我会得到它。

  thirdparty.com?method=register& ; classId = 1234& userId = abcde 

是否可以将敲除按钮转换为GET或以某种方式在我们的内部网站上向第三方网站提供注册机制?
我肯定可以粘贴更多的源代码,因为我确定需要更多的源代码。



我尝试过使用Firefox开发者工具并查看网络流量。

解决方案

Knockout提供了几个对象来访问不同的级别上下文和$ root是他们的。 $ root对象表示根上下文中的主视图模型对象。例如,如果您的HTML元素在另一个绑定上下文中,比如在foreach中,并且您想在每次迭代中使用根视图模型的方法:

  var ViewModel = function(){this.actionTypes = ko.observableArray([{ActionType:Type A,Title:Title A}, {动作类型:类型B,标题:标题B},{动作类型:类型C,标题:标题C}]); this.clickAction = function(action){//你的ajax请求会在这里alert(action); }}; ko.applyBindings(new ViewModel());  

< script src =https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js>< / script>< div data-bind =foreach: actionTypes> < a class =btn btn-large btn-bluehref =javascript:void(0); data-bind =click:$ root.clickAction.bind($ data,ActionType)> < span data-bind =text:Title>注册< / span> < / a>< div>


I am trying to understand a button on a third party site my company uses. The button signs up a user for a class.

<a class="btn btn-large btn-blue" href="javascript:void(0);" 
        data-bind="click: $root.clickAction.bind($data, ActionType)">
        <span data-bind="text: Title">Sign up</span></a>

I was hoping to provide this button on a page in our company internal website, but I am not familiar with Knockout. I understand a GET request so if that button did something like this then I would get it.

thirdparty.com?method=register&classId=1234&userId=abcde

Is it even feasible to turn that knockout button into a GET or to somehow provide the signup mechanism on our internal sites to this 3rd party site? I can certainly paste more of the source as I'm sure more is needed.

I've tried using Firefox developer tools and viewing network traffic. I don't want to keep spinning my wheels if this isn't doable.

解决方案

Knockout provides several objects to access different levels of context and $root is of them. The $root object represents the main view model object in the root context. For instance, if your HTML element is inside another binding context, such as inside a foreach, and you want to use a root view model's method in each iteration:

 var ViewModel = function() {
    this.actionTypes = ko.observableArray([
        { ActionType: "Type A", Title: "Title A"},
        { ActionType: "Type B", Title: "Title B"},
        { ActionType: "Type C", Title: "Title C"}]);

    this.clickAction = function(action) {
        // your ajax request would go here
        alert(action);
    }
};

ko.applyBindings(new ViewModel());

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<div data-bind="foreach: actionTypes">
    <a class="btn btn-large btn-blue" href="javascript:void(0);" data-bind="click: $root.clickAction.bind($data, ActionType)">
         <span data-bind="text: Title">Sign up</span>
    </a>
<div>

这篇关于敲除数据绑定点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆