1

Knockout.js私はこれがうまくいかない例を作ろうとしていました。オブジェクトを作成する関数を作成しました

var makeproduto = function (id, nome, preco, quantidade) {
    this.Id = ko.observable(id);
    this.Nome = ko.observable(nome);
    this.Preco = ko.observable(preco);
    this.Quantidade = ko.observable(quantidade);

    this.ValorTotal = ko.computed(function () { 
        return this.Quantidade() * this.Preco();
    }, this);

    return this;
};

製品のエンティティを入力する別の関数

var productListTemp = function () {
    this.produtos = ko.observableArray([]);
    this.produtos.push(produto.makeproduto(1, 'Pão', 045, 100));
    this.produtos.push(produto.makeproduto(2, 'leite', 135, 100));
    this.produtos.push(produto.makeproduto(3, 'ovos', 035, 96));
    this.produtos.push(produto.makeproduto(4, 'guarana', 425, 100));
    this.produtos.push(produto.makeproduto(5, 'fanta', 425, 100));
    this.produtos.push(produto.makeproduto(6, 'coca cola', 500, 100));
    this.produtos.push(produto.makeproduto(7, 'torta pedaço', 215, 60));
    this.produtos.push(produto.makeproduto(8, 'torta inteira', 990, 10));
    this.produtos.push(produto.makeproduto(9, 'sorvete - frutale', 225, 100));
    this.produtos.push(produto.makeproduto(10, 'sorvete - magnum white / black', 500, 50));
    this.produtos.push(produto.makeproduto(11, 'sorvete - magnum gold', 600, 25));
    this.produtos.push(produto.makeproduto(12, 'bolo de cenora', 995, 100));
    return this.produtos();
};

そしてDataBind、画面上のどのデータにも取り組んでいません。

MountList = function () {
    var temp = productListTemp();
    this.vm = ko.observableArray(temp),
    this.quant == ko.computed(function () {
        return this.vm().length;
    }, this);
},

DatabindFunction = function () {
    ko.applyBindings(new MountList());
};

どこが間違っているのでしょうか?

4

3 に答える 3

1

関数newでオブジェクトを作成するには、キーワードを使用する必要があります。productListTemp

this.produtos.push(new produto.makeproduto(1, 'Pão', 045, 100));

this関数ポインターを呼び出すと、別のコンテキストwindowがあり、新しいオブジェクトの代わりにすべてのプロパティを追加します。

于 2013-02-04T16:15:39.633 に答える
0
    var makeproduto = function (id, nome, preco, quantidade) {
        this.Id = ko.observable(id);
        this.Nome = ko.observable(nome);
        this.Preco = ko.observable(preco);
        this.Quantidade = ko.observable(quantidade);

        this.ValorTotal = ko.computed(function () { 
            return this.Quantidade() * this.Preco();
        }, this);
    }
        , productListTemp = function () {
        this.produtos = ko.observableArray([]);
        this.produtos.push(new makeproduto(1, 'Pão', 045, 100));
        this.produtos.push(new makeproduto(2, 'leite', 135, 100));

        return this.produtos();
    };

また、プレーンな JSON から観察可能なプロパティを持つオブジェクトを作成する ko.mapping プラグインを検討することもできます。

于 2013-02-04T16:21:31.407 に答える