Проблемы с областью Javascript - PullRequest
1 голос
/ 09 октября 2009

Я расширяю dojox.data.JsonRestStore в dojo и хочу предоставить свою собственную фиксированную схему. это getUsername не будет работать, потому что оно не ссылается на текущее хранилище данных Посмотрите на этот код:

/**
 * @author user
 */
dojo.provide("cms.user.UserAuthenticationStore");

dojo.require("dojox.data.JsonRestStore");

dojo.declare("cms.user.UserAuthenticationStore", [dojox.data.JsonRestStore], {
    schema: {
        prototype: {
            getUsername: function(){
                return ???.getValue(this, "username");
            }
        }
    }
});

Можете ли вы сказать мне, что заменить ??? с
EDIT:
Вот код, который работает, но он ужасен, может кто-нибудь сказать мне, как это исправить?

/**
 * @author user
 */
dojo.provide("cms.user.UserAuthenticationStore");

dojo.require("dojox.data.JsonRestStore");

dojo.declare("cms.user.UserAuthenticationStore", [dojox.data.JsonRestStore], {
    schema: {
        prototype: {}
    },
    constructor: function(){
        var that = this;
        this.schema.prototype.getUsername = function(){
            return that.getValue(this, "username");
        }
    }
});

Ответы [ 2 ]

1 голос
/ 09 октября 2009

Вместо:

this.schema.prototype.getUsername = function() {
  return ???.getValue(this, "username");
}

Вы можете попробовать:

this.schema.prototype.getUsername = dojo.hitch(this, "getValue", <this>, "username");

, где "<this>" - это переменная, используемая в качестве первого параметра функции getValue.В противном случае ваш "that" не так уж и страшен, но люди обычно называют его "self" или как-то так.

Редактировать:

Может быть, это будет работать?Быстрый и грязный способ создания новой схемы.В противном случае вы можете создать другой компонент, который определяет вашу собственную схему отдельно.Затем вы можете просто создать «новую MySChema ()» в качестве «схемы» var.

dojo.declare("cms.user.UserAuthenticationStore", [dojox.data.JsonRestStore], {
    self: this,
    schema:  new (function() {
                this.getUsername = function () { return self.getValue(this, "username"); }
             }
    })();
});
0 голосов
/ 09 октября 2009

Вот правильный способ сделать это:

/**
 * @author user
 */
dojo.provide("cms.user.UserAuthenticationStore");

dojo.require("dojox.data.JsonRestStore");

dojo.declare("cms.user.UserAuthenticationStore", [dojox.data.JsonRestStore], {
    schema: {
        prototype: {
            store: null,
            getUsername: function(){
                return this.store.getValue(this, "username");
            }
        }
    },
    constructor: function(){
        this.schema.prototype.store = this;
    },
    login: function(){

    },
    logout: function(){

    }
});
...