Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 敲除-如何从嵌套函数访问父属性?_Javascript_Knockout.js_Nested Attributes - Fatal编程技术网

Javascript 敲除-如何从嵌套函数访问父属性?

Javascript 敲除-如何从嵌套函数访问父属性?,javascript,knockout.js,nested-attributes,Javascript,Knockout.js,Nested Attributes,拜托,有人能帮我吗 执行create方法时出现以下错误: *Uncaught TypeError: Object #<Object> has no method 'baseUri'* BASE_URI是在我的母版页中定义的一个全局变量,我需要它,因为我有多个嵌套视图模型(我从这段代码中剪切了它们),每个baseUri都是BASE_URI+“一些字符串值”的组合。无论如何,我也需要访问项,以便更新列表中显示的值 谢谢 我有一种感觉当你调用产品时,这个不是你想象的那样。创建。尝试

拜托,有人能帮我吗

执行create方法时出现以下错误:

*Uncaught TypeError: Object #<Object> has no method 'baseUri'*

  • BASE_URI是在我的母版页中定义的一个全局变量,我需要它,因为我有多个嵌套视图模型(我从这段代码中剪切了它们),每个baseUri都是BASE_URI+“一些字符串值”的组合。无论如何,我也需要访问,以便更新列表中显示的值

谢谢

我有一种感觉
当你调用
产品时,这个
不是你想象的那样。创建
。尝试使用
.bind
显式设置上下文:

<form id="createProducts" data-bind="submit: products.create.bind($data)">

正如安德鲁·惠特克(Andrew Whitaker)已经提到的,“这”将不是您所期望的上下文。我的解决方案是存储“this”引用(例如称为“self”)并使用它而不是“this”:

您还可以明确指出应该在什么上下文中调用函数:

<form id="createProducts" data-bind="submit: function () { products.create($root.products); }">

products: {
    create: function(self) {
        ...
    }

产品:{
创建:功能(自我){
...
}

在这种情况下,$root应该指向您的视图模型,因此您将传递products对象的实例以创建函数。

有两种方法可以确保绑定事件时此
是正确的

        $.post(this.baseUri(), $(formElement).serialize(), null, "json")
            .done(function (o) {                    
                alert("The Product " + o.Name + " was created.");
                this.items.push(o);
            }.bind(this));
第一种是使用
bind

<form id="createProducts" data-bind="submit: products.create.bind(products)">
第二种方法是对回调函数使用
bind

        $.post(this.baseUri(), $(formElement).serialize(), null, "json")
            .done(function (o) {                    
                alert("The Product " + o.Name + " was created.");
                this.items.push(o);
            }.bind(this));

您好,Andrew!我测试了您的建议,但仍然没有更改。我阅读了一篇关于此上下文问题的文章。根据第5项(),~this~将表示调用方的上下文。本例中的表单,这就是为什么我的函数找不到项或baseUri属性的原因。文章建议应用“.bind(viewModel)”(在我的代码中为mm)如果您使用
.bind($root)
,该怎么办?您好@Slawek!我很感激您第一次提出的让html数据绑定尽可能简单的建议。但不幸的是,错误仍然存在(第二个解决了问题,但增加了数据绑定的复杂性。今晚我将返回此项目。谢谢!谢谢Michael!第一个选项部分解决了问题。this.baseUri现在可用,但this.items未定义。可能是因为它在.done内。)(function.Correct.我已经扩展了答案来处理这个问题。完美的Michael!我尝试了,但是我把绑定放在了错误的地方(
})。bind
而不是
}。bind
)。非常感谢!
<form id="createProducts" data-bind="submit: products.create.bind(products)">
<form id="createProducts" data-bind="submit: function() { products.create(); }">
        var self = this;
        $.post(this.baseUri(), $(formElement).serialize(), null, "json")
            .done(function (o) {                    
                alert("The Product " + o.Name + " was created.");
                self.items.push(o);
            });
        $.post(this.baseUri(), $(formElement).serialize(), null, "json")
            .done(function (o) {                    
                alert("The Product " + o.Name + " was created.");
                this.items.push(o);
            }.bind(this));