简体   繁体   English

在敲除js中使用'with'进行数据绑定后,如何访问另一个原型对象

[英]How do I access another prototype object after data binding using 'with' in knockout js

I'm using knockoutjs and have an object like this: 我正在使用基因敲除js,并有一个像这样的对象:

var home = function(){
    this.title = 'Home',
    this.vm = {
        names: ko.observableArray(),
        metadata: {
            startDate: ko.observableArray()
        }
    }
};

home.prototype.create = function(){
   alert('creating');
};

home.prototype.addNewPerson = function(){
    alert(this);
    this.create();
};

return home;

Then in my HTML, I use the with binding: 然后在我的HTML中,使用with绑定:

<div data-bind='with: vm.metadata'>
    <input data-bind='value: startDate' />
    <button data-bind='click: $parent.addNewPerson />
</div>
  • this isn't my exact code, but a simplified version 这不是我的确切代码,而是简化版本

When the user clicks on the button in this situation, this will be my metadata object. 在这种情况下,当用户单击按钮时, this将是我的元数据对象。 So I'll get an undefined error, because metadata doesn't have a create method. 因此,我将得到一个未定义的错误,因为元数据没有创建方法。

If I don't use the with binding and instead bind like this: <input data-bind='value: vm.metdatadata().startDate'/> 如果我不使用with绑定,而是这样绑定: <input data-bind='value: vm.metdatadata().startDate'/>

Then when the user clicks, I get my entire object, and I can call this.create(); 然后,当用户单击时,我得到了整个对象,然后可以调用this.create();

  1. Is this expected behavior? 这是预期的行为吗?
  2. If it is, how can I access my main module in the addNewPerson method while still using the with binding? 如果是这样,如何在仍然使用with绑定的情况下如何在addNewPerson方法中访问我的主模块?

This is the expected behavior of the click binding: this will be set the current "item" so in your case the metadata object. 这是click绑定的预期行为: this将设置为当前的“项目”,因此在您的情况下为metadata对象。

There are multiple ways to solve this: 有多种解决方法:

You can use bind function (Knockout comes with its own version if your browser does not nattily supports it) to fix the value of this to your parent object in your view: 您可以使用bind功能 (剔除带有自己的版本,如果你的浏览器不支持nattily它)的值设置为修复this在你看来你的父对象:

<div data-bind='with: vm.metadata'>
    <input data-bind='value: startDate' />
    <button data-bind='click: $parent.addNewPerson.bind($parent) />
</div>

Or you can do the same in your viewmodel level (the syntax looks a little bit funny because of the usage of the prototype): 或者,您也可以在视图模型级别中执行相同的操作(由于使用了原型,其语法看起来有些可笑):

var home = function(){
    this.title = 'Home',
    this.vm = {
        names: ko.observableArray(),
        metadata: {
            startDate: ko.observableArray()
        }
    }
    this.addNewPerson = this.addNewPerson.bind(this);
};


home.prototype.addNewPerson = function(){
    alert(this);
    this.create();
};

Or you can use the event delegation pattern described in the article: Revisting Event Delegation in Knockout.js 或者,您可以使用文章中描述的事件委托模式: 在Knockout.js中重新存在事件委托

You can also check out Ryan Niemeyer's nice video: devLink 2013 - Knockout.js Tips and Tricks where the second tip is about Controlling “this” 您还可以观看Ryan Niemeyer的精彩视频: devLink 2013-Knockout.js技巧和窍门 ,其中第二个技巧是关于控制“ this”的

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

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