【问题标题】:Getting error in knockout在淘汰赛中出现错误
【发布时间】:2012-10-08 05:24:15
【问题描述】:

我收到以下错误:

错误:无法解析绑定。 消息:ReferenceError:UpdateStatus 未定义; 绑定值:选中:状态,禁用:状态,单击:UpdateStatus

这是我的 javascript 代码

function WebmailViewModel() {
// Data
var self = this;
self.days = ['2012-10-01', '2012-10-02', '2012-10-03', '2012-10-04', '2012-10-05', '2012-10-06', '2012-10-07'];
self.choosenDateId = ko.observable();
self.choosenDateGoal = ko.observable();
self.choosenGoalId = ko.observable();

self.UpdateNote = ko.computed(function () {
    $.ajax({
        type: "POST",
        url: 'SinglePageApp.aspx/UpdateNote',
        data: "{goalId:9423}",
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            alert(result.d);
        }
    });
});

self.UpdateStatus = ko.computed(function () {
    $.ajax({
        type: "POST",
        url: 'SinglePageApp.aspx/UpdateStatus',
        data: "{goalId: 9423}",
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            alert(result.d);
        }
    });
});

// Behaviours    
self.gotoDay = function (days) { location.hash = days };

// Client-side routes    
Sammy(function () {

    this.get('#:days', function () {          
        self.choosenDateId(this.params.days);

        debugger;
        $.ajax({
            type: "POST",
            url: 'SinglePageApp.aspx/GetGoals',
            data: "{goalDate:'" + this.params.days + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                self.choosenDateGoal(msg.d);

                alert("success");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
                alert(errorThrown);
            }
        })
    });
    this.get('', function () { this.app.runRoute('get', '#2012-10-04') });
}).run();
};

ko.applyBindings(new WebmailViewModel());

提前致谢

【问题讨论】:

  • 请添加 html 标记。

标签: knockout.js sammy.js


【解决方案1】:

我相信你的问题是你在错误的意义上使用了计算的 observables。如果你想调用一个函数(如你所愿,因为你将它绑定到点击绑定)只需将其声明为

self.<function Name> = function(< passed variables>){
    //Code to be done when this function is called.
};

其中的计算意味着更多地用作变量。因此,如果只有传递给计算的函数,则将其视为只读变量。你可以指定一个可读写的计算,但是你必须提供一个读写函数,比如:

self.<computed Name> = ko.computed(function(){
    read: function () {
        // return how you want this computed to be displayed.
    },
    write: function (value) {
        // How do you want this to be saved.
    },
});

此外,计算的 observables 意味着在函数中使用现有的 observables。这样,每当在计算的 observable 中使用的 observable 被更新时,都会调用计算的函数。有关示例和更多信息,请参阅 computed observable documentation

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 2015-07-17
    • 2015-07-16
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多