简体   繁体   English

如何从淘汰赛功能中获取价值

[英]How to get the value out of Knockout function

I'm using Knockout and I'm trying to build function that will return a value, The problem is that I'm computing the value inside internal function, and I don't find a way to get the value out of this function. 我正在使用Knockout,正在尝试构建将返回值的函数,问题是我正在计算内部函数中的值,但找不到从该函数中获取值的方法。

Thats what I have so far 那就是我到目前为止

JS JS

var vm = {
    myResponse: ko.observable(),
    computedValue: ko.observable()
};

vm.myResponse.subscribe(function (newValue) {
    myfunction(computedValue);
});


var myfunction = function (observableToUpdate) {
    var responses = request.responses();
    return changeUp.getUserInfo().then(function (userInfo) {
        var currentResponse = ko.utils.arrayFirst(responses, function (r) {
            return r.userName === userInfo.userName;
        });
        var responseindex = responses.indexOf(currentResponse);
        observableToUpdate({
            responeIndex: responseindex
        });
    });
};

On my html I'm using following lines 在我的html上,我使用以下行

<tbody data-bind="foreach: request.responses()">
<tr data-bind="css: { responder : $parent.vm}">
    <td>{{since()}}</td>
    <td>{{amount|number}}</td>
    <td>{{rate|number}}</td>
    <td>{{distance}}</td>
    <td>
        <a data-bind="click: action">{{_t('Details')}}</a>
</tr>
</tbody>

Currently non of the get the class responder. 当前非get类响应者。 i want to add it only if the condition in isMyResponse is true; 我只想在isMyResponse中的条件为true时添加它;

The only way to use asynchonous callbacks to set an observable is to use subscriptions. 使用异步回调设置可观察项的唯一方法是使用订阅。 A knockout computed expects a synchronous return value. 计算出的敲除期望有一个同步返回值。

You function finished execution before the "then" callbacks fire. 在“ then”回调触发之前,您的函数已完成执行。

var someObservable = ko.observable();
var computedValue = ko.observable();

someObservable.subscribe( function( newValue ) {
    changeup.getUserInfo().then( function(userInfo) {
        ...
        computedValue(myRespondIndex);
    });
} );

Also, "then" callback return value only influences the call chain of the promise. 同样,“然后”回调返回值仅影响promise的调用链。 If you throw an exception the fail callback will fire and any chained "then" callback will not be invoked. 如果引发异常,则将触发fail回调,并且不会调用任何链接的“ then”回调。 A returned value does not "cascade" through the callbacks. 返回的值不会通过回调“级联”。

For the UserID = 'undefined' , I think that the result of the first then function is being passed to the second then function. 对于UserID = 'undefined' ,我认为第一个then函数的结果将传递给第二个then函数。 The result of the second then function will be undefined as nothing is returned. 第二个then函数的结果将undefined因为未返回任何内容。 I think it should probably be something like this:- 我认为可能应该是这样的:

this.isMyResponse = function(index) {
    var test ;
    var responses = this.request.responses();
    var useriD = changeup.getUserInfo().then(function (userInfo) {
        var currentResponse = ko.utils.arrayFirst(responses, function (r) {
            return r.username === userInfo.username;
        });
        var myRespondIndex = responses.indexOf(currentResponse);
        test = myRespondIndex;
        console.log("inside" + test);
        return myRespondIndex;
    }).then  (function (index) {
        console.log("outside" + test);
        return index;
    });
    console.log("outside" + test);
    return test;
}

EDIT 编辑
The following jsFiddle was my attempt to confirm what Robert Stanley was telling me. 以下jsFiddle是我试图确认Robert Stanley在告诉我的内容。 In the end I implemented the solution how he suggested. 最后,我按照他的建议实施了解决方案。

http://jsfiddle.net/gonefishern/zdDh9/ http://jsfiddle.net/gonefishern/zdDh9/

As Robert Stanley pointed out to me, the following javascript will return a promise but the not values that I thought it was going to. 正如罗伯特·斯坦利(Robert Stanley)向我指出的那样,以下JavaScript将返回一个Promise,但不是我认为它要去的值。

var isMyResponse = function(observableToUpdate) {
    var responses = this.request.responses();
    return  changeup.getUserInfo().then(function (userInfo) {
        var currentResponse = ko.utils.arrayFirst(responses, function (r) {
            return r.username === userInfo.username;
         });
         return {
             responeIndex: responses.indexOf(currentResponse),
             userInfo: userInfo
         };
    });
}

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

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