如何从Knockout函数中获取值

时间:2021-10-13 09:58:37

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

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. i want to add it only if the condition in isMyResponse is true;

目前没有获得班级响应者。我想只在isMyResponse中的条件为真时添加它;

2 个解决方案

#1


1  

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. If you throw an exception the fail callback will fire and any chained "then" callback will not be invoked. A returned value does not "cascade" through the callbacks.

此外,“then”回调返回值仅影响promise的调用链。如果抛出异常,将触发失败回调,并且不会调用任何链接的“then”回调。返回的值不会通过回调“级联”。

#2


0  

For the UserID = 'undefined', I think that the result of the first then function is being passed to the second then function. The result of the second then function will be undefined as nothing is returned. I think it should probably be something like this:-

对于UserID ='undefined',我认为第一个then函数的结果被传递给第二个函数。第二个then函数的结果将是未定义的,因为没有返回任何内容。我认为应该是这样的: -

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. In the end I implemented the solution how he suggested.

编辑以下jsFiddle是我试图证实罗伯特·斯坦利告诉我的内容。最后,我实施了他的建议解决方案。

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.

正如罗伯特斯坦利向我指出的那样,以下javascript将返回一个承诺,但不是我认为它的价值。

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
         };
    });
}

#1


1  

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. If you throw an exception the fail callback will fire and any chained "then" callback will not be invoked. A returned value does not "cascade" through the callbacks.

此外,“then”回调返回值仅影响promise的调用链。如果抛出异常,将触发失败回调,并且不会调用任何链接的“then”回调。返回的值不会通过回调“级联”。

#2


0  

For the UserID = 'undefined', I think that the result of the first then function is being passed to the second then function. The result of the second then function will be undefined as nothing is returned. I think it should probably be something like this:-

对于UserID ='undefined',我认为第一个then函数的结果被传递给第二个函数。第二个then函数的结果将是未定义的,因为没有返回任何内容。我认为应该是这样的: -

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. In the end I implemented the solution how he suggested.

编辑以下jsFiddle是我试图证实罗伯特·斯坦利告诉我的内容。最后,我实施了他的建议解决方案。

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.

正如罗伯特斯坦利向我指出的那样,以下javascript将返回一个承诺,但不是我认为它的价值。

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
         };
    });
}