无法处理绑定错误敲除

时间:2015-08-13 06:31:33

标签: knockout.js knockout-mvc

如果我的代码对您来说很重要,那么只需看到已注释的代码,您就会明白错误。 我的HTML代码是:

<div data-bind="foreach: showAds">
        Title: <span data-bind="text: title"></span>
        description: <span data-bind="text: description"></span>
        postedby:<span data-bind="text: postedByName"></span>
        <div data-bind="foreach: showMobileAd">
            color: <span data-bind="text: color"></span>
            price: <span data-bind="text: price"></span>
            <span data-bind="html:isNegotiable"></span>
            condition: <span data-bind="text: isNew"></span>
        </div>

        <div data-bind="foreach:showComment">
            @*<textarea id="commentDescription" data-bind="value:newComment"></textarea>
            <input type="button" data-url="/api/comment" data-bind="click: addcomment" value="submit" />*@
            postedBy:<span data-bind="text: postedByName"></span><br />
            <span data-bind="text: description"></span><br />
            <span data-bind="text: time"></span><br />
            <div data-bind="foreach: showCommentReply">
                postedBy (reply):<span data-bind="text: postedByName"></span><br />
                <span data-bind="text: description"></span><br />
                <span data-bind="text: time"></span><br />
            </div>
        </div>
    </div>

我的js代码是:

function mobilead(data) {
    var self = this;
    data = data || {};
    self.color = data.color;
    self.price = data.price;
    if (data.isNew) {
        self.isNew = 'New';
    } else {
        self.isNew = 'Used'
    }
    if (data.isNegotiable) {
        self.isNegotiable = 'Negotiable';
    } else {
        self.isNegotiable= '<strike>Negotiable</strike>'
    }
}
function commentReply(data) {
    var self = this;
    data = data || {};
    self.description = data.description;
    self.postedByName = data.postedByName;
    self.postedById = data.postedById;
    self.time = data.time;
}
function comment(data) {
    var self = this;
    data = data || {};
    self.description = data.description;
    self.postedByName = data.postedByName;
    self.postedById = data.postedById;
    self.time = data.time;
    self.showCommentReply = ko.observableArray();
    if (data.commentReply) {
        var reply = $.map(data.commentReply, function (item) { return new commentReply(item); });
       //self.showcommentReply(reply);
    }
    self.newComment = ko.observable();
    self.addComment = function () {
        var com = new comment();
        com.adId = self.adId;
        com.description(self.newComment());
        return $.ajax({
            url: 'api/Comment',
            dataType: "json",
            contentType: "application/json",
            cache: false,
            type: 'POST',
            data: ko.toJSON(com)
        })
        .done(function (res) {
            self.showComment.splice(0, 0, new comment(res));
            self.newComment('');
        })
        .fail(function () {
            toastr.info("failed to post comment", "info");
        })
    }
}

function ad(data) {
    var self = this;
    data = data || {};
    self.title = data.title;
    self.description = data.description;
    self.postedByName = data.postedByName;
    self.showMobileAd = ko.observableArray();
    self.showComment = ko.observableArray();
    if (data.mobilead) {
        var mobad = $.map(data.mobilead, function (item) { return new mobilead(item); });
        self.showMobileAd(mobad);
    }
    if (data.comment) {
        var cmt = $.map(data.comment, function (item) { return new comment(item); });
        self.showComment(cmt); 
    }

}

function viewModel() {
    var self = this;
   self.showAds = ko.observableArray();
    self.loadad = function () {
        var adId = $("#adId").val();
        url_address = '/api/Electronic/' + adId;
        $.ajax({
            url: url_address,
            dataType: "json",
            type: 'GET'
        })
        .done(function (data) {
            var mappedads = $.map(data, function (item) { return new ad(item); });
            self.showAds(mappedads);
        })
        .fail(function () {
            Error("error");
        });
    }

    self.loadad();
    return self;
} ko.applyBindings(new viewModel());

错误在三条注释行上。(当然,只有当我取消注释这些行时才显示错误)

  1. 评论功能错误为self.showcommentReply is not a function
  2. HTML代码中的两个注释行错误为Uncaught ReferenceError: Unable to process binding "foreach: function (){return showComment }" Message: Unable to process binding "click: function (){return addcomment }" Message: addcomment is not defined
  3. 我的代码有什么问题?我该如何处理这两个错误?

1 个答案:

答案 0 :(得分:2)

如果您想查看视图和&amp;,请始终使用()约定viewModel同步和更改以反映任何一种方式

尝试修改

  

self.showAds = mappedads

  

self.showAds(mappedads)