视图模型函数不是淘汰中的函数错误

时间:2016-08-17 08:37:34

标签: javascript knockout.js

我有这个视图模型,

define(['text!./form.html', 'knockout', '../../home', '../../users'],  
  function(template, ko,homeModel,userModel) {

    function  registerUserViewModel() {
        var self = this;
        ...
        self.registerUser = function(){
            ...
            $.ajax({
                url: 'server/registerUser.php',
                type: 'post',
                data: {"data":credentials},
                success: function (result) {
                    if(result == "Successful"){
                        homeModel.viewModel.addUser("Jun","5:30");
                    }else{
                        alert(result);
                    }
                }
            });
        }
        return self;
    }
    return {
        viewModel: registerUserViewModel,
        template: template
    };
});

这是我的homeViewModel,

define(["knockout", "text!./home.html", "../../users"],  
  function (ko, homeTemplate, userModel) {
    function homeViewModel(params) {
        var self = this;
        ...
        self.users = ko.observableArray([]);
        $.ajax({
            url: 'server/getUsers.php',
            type: 'post',
            data: {"data":id},
            success: function (result) {
                var row = JSON.parse(result)
                for(var i=0;i<row.length;i++){
                    self.users.push(new userModel.userModel(row[i]["name"],row[i]["time"],row[i]["id"]));
                }
            }
        });
        self.addUser = function(name,time){
            self.users.push(new userModel.userModel(name,time));
        }
        return self;
    }
    return { viewModel: homeViewModel, template: homeTemplate };
});

当我转到页面时, 有一个错误,

  

homeModel.viewModel.addUser不是函数。

但我为self.addUser分配了一个功能。我试过了homeModel.addUser,但仍然无法正常工作。

有没有其他方法可以简单地将新的userModel添加到self.users数组?

0 个答案:

没有答案