English 中文(简体)
KnookoutJs: 从模型实例获取约束元素
原标题:KnockoutJs: Get the bound element from a model instance

是否完全有可能获得数据(模型)实例必须具备的相应要素(或要素)?

例如,我在ViewModel 属性中拥有一系列个人物品仓库。

我将视图模式绑在它所形成的视图上,例如:

<div class="people" data-bind="template: { foreach: people }">
    <a href="#" class="person" data-bind="text: name"></a>
</div>

然后通过jQuery 将一些事件处理器捆绑起来:

$container.on( click ,  .person , function(e){
    e.preventDefault();
    self.showPerson( ko.dataFor(this) );
});

在我的 < code> showPerson 方法中,我将保留对模型的引用。 我/ 可以/ 也可以/ 也保留对元素的引用, 但如果我不需要, 我不想这样做 。

self.showPerson = function(person) {
    // can i get the corresponding element from the  person  model?
};

有谁有想法吗?

最佳回答

您的语法和使用 $container 的 jquery 和 person 的第二个参数对我来说并不熟悉, 但在您的点击处理器中, 您的点击处理器中不是被点击的元素吗? 您能否将此也传递到您的 showPperson 方法?

$container.on( click ,  .person , function(e){
    e.preventDefault();
    self.showPerson( ko.dataFor(this), this );
});

self.showPerson = function(person, element) {
    // can i get the corresponding element from the  person  model?
};

我不知道有什么办法可以从我的头顶上得到一个可观测到的元素,但可能有几个不同的元素。你可以有一个名称的文本框,在一个横线上显示名称,在计算时使用,有订户,在另一个绑定的计算中使用名称(.)的长度。

也就是说,如果您使用击倒的调试版本,您可以看到您的可见效果有一个 属性,该属性可能具有您要查找的属性。 缩写版本是一个单字符。

问题回答

您可以简单地创建一个自定义约束

    // data-bind="element: observable"
    // sets observable to element ..
    ko.bindingHandlers.element = {
        init: function(element, valueAccessor) {
            var target = valueAccessor();
            target(element);
        }
    };

在您的视图模型中,创建一个字段以存储元素:

person.el = ko.observable(null);

然后在你的 html 模板...

<div data-bind="element: el"> .... </div>

为什么不移动在您的 foreach 中绑定的单击事件?

在此情况下, 您的 showPerson () 函数将拥有 person 的正确数据

编辑:

对不起,但我猜我错过了 你问题的核心:

self.showPerson = function (person, event) {
     element = event.srcElement
     ...
}
self.showPerson = function(data, event) {
  // event.currentTarget is the DOM element
  // $(event.currentTarget) gives the jQuery element
}

我认为,最佳做法是直接与视觉模型上的功能相关,而不是附加操作员。

< a href=" "https://stackoverflow.com/ questions/13804705/knockout-js-knockout-js-click- contact- to-parent- opident- in- foreach" > 点击对父函数的连接





相关问题
How to go from DOM node to viewModel object?

When the drop function is called back, this is set to the droppable DOM node (the target) and ui.draggable is the DOM node which was dragged. Is there an idiomatic way of getting the model object ...

making fields observable after ajax retrieval in knockout.js

I am wondering how I can make certain fields observables in knockout.js that I get from an ajax call without having to define the whole object in my viewmodel. Is this possible? Here is what I have ...

ASP.NET MVC Validation with jQuery $.ajax

I have a situation where I am sending data to a Controller via jQuery $.ajax, the data is a serialized json string. (MVC 3.0) It binds it fine - my controller receives the results and they are ...

Knockout.js and MVC

Just started playing with knockout.Js which is a fantastic framework Steve s really done well with that one. One thing I can t seem to do at the minute is impliment it with my Html Helpers. So for ...

Anyone using Knockoutjs with asp.net-mvc in anger? [closed]

I find it very interesting and have a prototype working based on Steve s mvc sample and another small sample from this thread. Using json.net to deserialize within the post action as I couldn t ...

热门标签