KnockoutJs从Json webservice更新视图模型单个observable

时间:2013-10-19 10:36:40

标签: javascript ajax json knockout.js

淘汰新手,Js的新人,我是一个点网开发者,试图创建一个淘汰登录屏幕,发布数据很好,接收响应OK,只是无法弄清楚如何更新单个observable,请帮忙。 ..

function AppViewModel() {
this.email = ko.observable("Bergmail.com");
this.password = ko.observable("Password");
this.logonresult = ko.observable("logon resul");
//computed
this.Computedresult = ko.computed(function () {
    return this.email() + " " + this.password();
}, this);
    //behavious
this.Loginajax = function () {

    var self=this
    $.ajax("Indi-Dal-Json.asmx/CheckLogon" ,{
        data: ko.toJSON({ email: this.email,password: this.password}),
        type: "post", contentType: "application/json",
        success: function (ajaxjsondata) {
            {
                // var parsedjson = JSON.parse(result.b);
                //AppViewModel.logonresult = (result.d);
                self.logonresult = ajaxjsondata.d;
            }

        }
    });
   };
 }

2 个答案:

答案 0 :(得分:1)

self.logonresult是一个可观察的,所以你需要调用它,而不是用新值覆盖它的引用。

更改

self.logonresult = ajaxjsondata.d;

self.logonresult(ajaxjsondata.d);

http://knockoutjs.com/documentation/observables.html#reading_and_writing_observables

答案 1 :(得分:0)