Javascript 击倒困难

Javascript 击倒困难,javascript,knockout.js,Javascript,Knockout.js,我试着用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(quan

我试着用
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());
};

哪里有错?

您必须使用
new
关键字在
productListTemp
函数中创建对象:

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

当您仅调用函数
时,此
指针有另一个上下文-
窗口
,您将所有属性添加到其中,而不是添加新对象。

您必须使用
new
关键字在
productListTemp
函数中创建对象:

this.produtos.push(new produto.makeproduto(1, 'Pão', 045, 100));
    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();
    };
当您只调用函数
时,此
指针有另一个上下文-
窗口
,您将所有属性添加到它而不是新对象

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

也可以考虑Ko.Primulink插件,它可以从普通JSON.</P>创建具有可观察属性的对象。


也可以考虑Ko.Primulink插件,用普通JSON创建可观察属性的对象。

确保你去掉额外的'=’/P>

this.quant = ko.computed(function () {
            return this.vm().length;
        }, this);
您也可以在淘汰3.2.0及更高版本中使用纯计算 它将提供性能和内存优势

this.quant = ko.pureComputed(function () {
            return this.vm().length;
        }, this);

一定要把多余的“=”去掉

this.quant = ko.computed(function () {
            return this.vm().length;
        }, this);
您也可以在淘汰3.2.0及更高版本中使用纯计算 它将提供性能和内存优势

this.quant = ko.pureComputed(function () {
            return this.vm().length;
        }, this);

从何处调用
DatabindFunction
?您的视图是什么样子的?我在route javascript类中调用了DataBind函数。。。当我调用firts链接时…从何处调用
DatabindFunction
?您的视图是什么样子的?我在route javascript类中调用了DataBind函数。。。当我调用firts链接时……另外,从构造函数函数中删除
返回此
,如果您想使用构造函数
makeproduto
而不使用
新建
,您应该添加代码
if(!(makeproduto的此实例))返回新的makeproduto(id、nome、preco、quantidade)
作为函数的第一行
makeproduto
。此外,如果要使用构造函数
makeproduto
而不使用
new
则应添加代码
如果(!(makeproduto的此实例))返回新的makeproduto(id、nome、preco、quantidade)作为函数的第一行
makeproduto
。我发现我的错误在Route类中。。。我尝试使用sammy.js“this.$element().append(”当我在页面上直接添加HTML代码时,大部分都不起作用。有人能帮助我吗?我怎么能用敲除SAMMe+加载外部模板?你只需要使用插件并将其配置正确。它与萨米无关。也可以考虑在SPA上观看John Papa课程。你可以很容易地找到它。它覆盖Y。我们的问题以及=)我发现我的错误在Route类中。。。我尝试使用sammy.js“this.$element().append(”当我在页面上直接添加HTML代码时,大部分都不起作用。有人能帮助我吗?我怎么能用敲除SAMMe+加载外部模板?你只需要使用插件并将其配置正确。它与萨米无关。也可以考虑在SPA上观看John Papa课程。你可以很容易地找到它。它覆盖Y。我们的问题以及在=)之后您将遇到的问题)