简体   繁体   English

在点击功能中访问“ this”

[英]Access 'this' within Click function

Is it possible to access $(this) and perform actions such as hide() or toggle() this div in Knockout? 是否可以访问$(this)并在Knockout中执行此div hide()toggle()这个动作? I'm trying to access it via jQuery. 我正在尝试通过jQuery访问它。 Any alternative methods within the function is appreciated. 功能内的任何替代方法均应理解。

At the moment if I do this: 目前,如果我这样做:

click: function(){ console.log(this) }

I get the $data of my viewmodel. 我得到了我的视图模型的$data

I know other ways of handling this action via the model of knockout but I was wondering if this is possible? 我知道通过淘汰赛模型处理此操作的其他方式,但我想知道这是否可能?

  <div data-bind="click: function(){ $(this).hide() }" class="alert alert-secondary alert-dismissible fade show" role="alert" >
         some text here
    </div>

Is it possible to access $(this) and perform actions such as hide() or toggle() this div in Knockout? 是否可以访问$(this)并在Knockout中执行此div的hide()或toggle()这个动作?

Yes, but you should not do that. 是的,但是您不应该这样做。

Any jQuery code (beyond the $.ajax() utility functions) and any other code that does DOM interaction (modification, traversal, event handling, style changes) should stay completely out of both your viewmodel and your view. 任何jQuery代码(除了$.ajax()实用程序函数之外)以及任何其他进行DOM交互的代码(修改,遍历,事件处理,样式更改)都应该完全不包含在视图模型视图中。

  • Your viewmodel manages the state of your application, not its looks . 您的视图模型管理应用程序的状态 ,而不是外观
  • The way to manage the looks of the application is by binding viewmodel properties to view properties. 管理应用程序外观的方法是 viewmodel属性绑定到view属性。
  • The view should be fully dependent on the viewmodel, but the viewmodel should have zero dependencies on the view. 视图应该完全依赖于视图模型,但是视图模型应该对视图具有零依赖关系。
    • Adding code that calls jQuery .hide() in the viewmodel introduces a dependency to the view inside the viewmodel. 在视图模型中添加调用jQuery .hide()代码会在视图模型内部引入对视图的依赖。 Assume you change your view and .hide() is no longer the right thing to do. 假设您更改了视图, .hide()不再是正确的选择。 Now you have to change the viewmodel as well - without adding any actual functionality. 现在,您还必须更改视图模型-无需添加任何实际功能。
    • Adding code that calls jQuery .hide() in the view leaves the state inside the viewmodel in the dark about a change in how things are displayed. 添加在视图中调用jQuery .hide()代码会使视图模型内部的状态处于黑暗状态,从而无法显示事物。 You lose control over how the viewmodel looks and are gradually forced to add even more hacks like this. 您将失去对视图模型外观的控制,并逐渐被迫添加更多这样的hack。
  • Use the existing bindings (there are bindings all the basic interactions like showing or hiding elements, adding event handlers, interacting with form elements) or write new bindings for special behavior. 使用现有的绑定(存在所有基本交互的绑定,例如显示或隐藏元素,添加事件处理程序,与表单元素进行交互)或针对特殊行为编写新的绑定

So in your case, what you need is 所以您的情况是

  • a viewmodel property that tracks if the item should be visible 一个viewmodel属性,该属性跟踪项目是否应该可见
  • a function to set this property 设置此属性的函数
  • the visible binding visible绑定

In the case of a dialog, let's call that property isVisible and let it default to true . 对于对话框,让我们将该属性称为isVisible并将其默认设置为true

Viewmodel: 视图模型:

function Alert() {
    var self = this;

    self.isVisible = ko.observable(true);
    self.message = ko.observable("some text here");
    self.dismiss = function () { self.isVisible(false); };
}

View, wrapped for legibility: 视图,为清晰起见而包装:

<div
  data-bind="
    text: message,
    click: dismiss,
    visible: isVisible,
    text: message
  "
  class="alert alert-secondary alert-dismissible fade show"
  role="alert"
></div>

It's almost always better to handle logic on the view-model instead of cluttering your markup, however, you can access the element with "$element" instead of "this". 最好总是在视图模型上处理逻辑而不是使标记混乱,但是,您可以使用“ $ element”而不是“ this”来访问元素。 For a better example of how things should be done refer to Tomalak's excellent answer. 对于事情应该怎么做一个更好的例子是指托默勒格的出色答卷。

data-bind="click: function(){ $($element).hide() }"

Maybe you can pass the event and access event.target and do event.target.hide() on that. 也许您可以传递event并访问event.target并对其执行event.target.hide() Usually this kind of situation is handled by assigning this to some other variable 通常这种情况是由分配处理this其他一些变量

function sample(){
    var _this = this;
    function(){
        console.log(_this,this); // both are accessible here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM