无法解析Knockout.js中的绑定

时间:2013-04-18 07:06:00

标签: knockout.js

面对共同的“约束问题”。以下是viewmodel

    function DetailListViewModel() {

    this.details = new Details();
    this.productdetails = ko.observableArray([]);

    this.show = function (item) {
        $.getJSON("Products.json", {}, function (data) {
            this.productdetails.push(this.details.init(data));
        });
    };
    }

    function Details() {
    this.author = ko.observable();
    this.text = ko.observable();
    this.init = function (temp) {
        return {
            author: temp.Author,
            text: temp.Text
        };
    };
    }

    var TaskListViewModel = {    
    tasks: ko.observableArray([]),
    addTask: function () {
    self.tasks.push(new Task({ BomID: this.BomID() }, { CreatedBy: this.CreatedBy() }));
    },
    ShowProductInfo: function (item) {       
        DetailListViewModel.show(item);
    }
    };


    $(function () {    
    $.getJSON("Tasks.json", function (allData)       {
            TaskListViewModel.tasks.push(toKoObserable($.parseJSON(allData.GetDataResult).RESULT.DOCUMENT));
        ko.applyBindings(TaskListViewModel);
        var DetailListViewModel = new DetailListViewModel();
        ko.applyBindings(DetailListViewModel);
    });

    function toKoObserable(blog) {
        return {
            BomID: ko.observable(blog[0].BOMID),
            CreatedBy: ko.observable(blog[0].CreatedBy)
        };
    }
});

我收到此错误

Uncaught Error: Unable to parse bindings.
Message: ReferenceError: author is not defined;
Bindings value: text:author 

我的HTML是:

<h3  data-bind="text:author">/h3>

1 个答案:

答案 0 :(得分:0)

我很确定如果你改变你的细节实现,一切都应该像你期望的那样工作。我不确定为什么您的详细信息列表包含单个详细信息实例,然后您创建一个可重复使用该对象的可观察的详细信息数组。从设计的角度来看,这似乎并不正确。你的init也没有返回可观察的值。尝试改变这些方面的内容:

function Details(author, text) {
    this.author = ko.observable(author);
    this.text = ko.observable(text);
}

然后当您填充详细信息列表时,您将执行以下操作:

function DetailListViewModel() {
    this.productdetails = ko.observableArray([]);

    this.show = function (item) {
        $.getJSON("Products.json", {}, function (data) {
            this.productdetails.push(new Details(data.Author, data.Text));
        });
    };
}