简体   繁体   English

为什么不能访问对象的属性?

[英]Why can't I access the property of the object?

I'm trying to access the property " providerAccountId " from the " accountsCardModel " object, but for some reason, it's undefined. 我正在尝试从“ accountsCardModel ”对象访问属性“ providerAccountId ”,但是由于某种原因,它是未定义的。 I'm using Kendo Observable object, and get method usually works. 我正在使用Kendo Observable对象,通常可以使用get方法。 I even tried using the dot notation to access the property stored within the object (no luck). 我什至尝试使用点表示法访问存储在对象中的属性(不走运)。

The following line, the console throws the error "Cannot read property 'get' of undefined" 在以下行中,控制台将引发错误“无法读取未定义的属性'get'”

provAccId: accountsCardModel.get("providerAccountId")

I don't know how Kendo internally works. 我不知道剑道内部如何运作。 Perhaps, kendo tries to execute the code before I want it to, but that's unlikely. 也许,kendo会在我想要的时候尝试执行代码,但这不太可能。 Perhaps it's an hoisting issue. 也许这是一个悬而未决的问题。

 var accountsCardModel = kendo.observable({ providerAccountId: "", datasource: new kendo.data.DataSource({ transport: { read: { url: MobileStorage.getServerURL() + "/cabinet/wicket/bookmarkable/com.web.services.AccountsService", dataType: "json", type: "POST", data: { op: "read", provAccId: accountsCardModel.get("providerAccountId") }, beforeSend: function(jqXHR){ Utils.xhr.queue(jqXHR); }, complete: function(jqXHR) { Utils.xhr.dequeue(jqXHR); } } }, schema: { parse: function(response) { console.log(response); } } }), showAccountDetails: function(providerAccountId){ this.set("providerAccountId", providerAccountId); } }); 

Well, I found a solution: 好吧,我找到了一个解决方案:

Traced the call stack, and it's frustrating to see the Kendo executing my lines behind the scene without my knowledge. 跟踪了调用堆栈,看到Kendo在我不知情的情况下在幕后执行我的命令令人沮丧。 Instead of sending read: parameters, I called it as a function and it worked. 我没有发送read:参数,而是将其作为函数调用,并且可以正常工作。

Before: 之前:

    datasource: new kendo.data.DataSource({
    transport: {
        read: {
            url: MobileStorage.getServerURL() + "/cabinet/wicket/bookmarkable/com.web.services.AccountsService",
            dataType: "json",
            type: "POST",
            data: {
                op: "read",
                provAccId: accountsCardModel.get("providerAccountId")
            },
            beforeSend: function(jqXHR){
                Utils.xhr.queue(jqXHR);
            },
            complete: function(jqXHR) {
                Utils.xhr.dequeue(jqXHR);
            }
        }
    },
    schema: {
        parse: function(response) {
            console.log(response);
        }
    }
}),

Afer: 以后:

    datasource: new kendo.data.DataSource({
    transport: {
        read: function(options) {
            $.ajax({
                url: MobileStorage.getServerURL() + "/cabinet/wicket/bookmarkable/com.web.services.AccountsService",
                dataType: "json",
                type: "POST",
                data: {
                    op: "read",
                    provAccId: accountsCardModel.providerAccountId
                },
                success: function(result) {
                  options.success(result);
                },
                error: function(result) {
                  options.error(result);
                },
                beforeSend: function(jqXHR){
                    Utils.xhr.queue(jqXHR);
                },
                complete: function(jqXHR) {
                    Utils.xhr.dequeue(jqXHR);
                }
            });
        }
    },
    schema: {
        parse: function(response) {
            console.log(response);
            //return response.providerAccounts[0]; // Load only online account, omit autologin
        }
    }
})

And it worked! 而且有效!

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

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