无法在组件内获取或更新可观察对象

时间:2016-07-31 21:16:23

标签: knockout.js

我尝试使用淘汰赛组件返回人员列表并将新人添加到列表中。我使用param传递Web服务调用的值。一切都很好负载。我使用不同的参数值加载其中几个组件。

但是,如果我想在创建新用户时重置参数,我无法重置,例如 this.newadminemail('')因为它显示this.newadminemail is not a function

我错误的是这甚至是使用组件的正确方法。

define(["knockout", "jsconfig", "jquery", "text!./channeladmins.html"], function(ko, jsconfig, $, channelAdminUserTemplate) {
    function channeladminuserViewModel(params) {

        this.newadminemail = ko.observable();
        this.channel = params.ref_id;
        this.usertype = ko.observable();
        this.usertypes = ko.observableArray(['channeladmin', 'user']);
        this.accesstoken = localStorage.getItem("accesstoken");
        this.permissionlist = ko.observableArray([]);

        // Get all the information
        this.loadpermissions = function(id) {
            this.jqxhr = $.getJSON(apiURL + 'adminpermissions?accesstoken=' + this.accesstoken + '&channelID=' + id, this.permissionlist);
            this.jqxhr.complete(function() {});

        }

        this.permissionlist.subscribe(function(newValue) {
            if (window.console && console.log) {
                console.log(++this.triggeredCount, "Email triggered with new value", newValue);
            }
        });

        this.newadminemail.subscribe(function(newValue) {
            if (window.console && console.log) {
                console.log(++this.triggeredCount, "user triggered with new value", newValue);
            }
        });

        this.newchanneladmin = function(email) {

            if (validateEmail(email)) {

                this.posturl = apiURL + 'adminpermissions?accesstoken=' + this.accesstoken + '&channelID=' + params.ref_id;

                this.request = $.ajax({
                    dataType: "json",
                    url: this.posturl,
                    method: "POST",
                    data: {
                        "user_email": email,
                        "channel_id": params.ref_id,
                        "permissons": "channeladmin",
                    },
                    success: function(res) {
                        // I cannot refresh this function console says this.loadpermissions is           //not a functionsuccess.         
                        this.loadpermissions(params.ref_id);
                    },
                    error: function(res) {
                        alert(res.responseText);
                    }
                });
            } else {
                return;
            }
        }
        this.loadpermissions(params.ref_id);
    }
    return {
        viewModel: channeladminuserViewModel,
        template: channelAdminUserTemplate
    };
});

0 个答案:

没有答案