用knockout寫前端複雜js邏輯的確很方便,並且html界面也很清爽。javascript
在ko中對於複雜的業務邏輯我會給viewmodel建立一些子model對象,可是viewmodel跟子model怎樣相互調用呢?html
//viewModel var viewModel = function (data) { var self = this; self.User = ko.observable(new model(data.User, self));//賦值爲一個新對象 self.Remark = ko.observable(data.Remark); self.Submit=function(obj){ //調用子對象屬性 var name = self.User().Name(); //調用子對象方法 self.User().Validate(); } self.TestFun = function(){ //邏輯 } } //子model var model = function (data, parentModel) { var self = this; self.Id = ko.observable(data.Id); self.Name = ko.observable(data.Name); self.Validate = function(){ //邏輯 } self.Save = function(obj){ //調用父對象屬性 var remark = parentModel.Remark(); //調用父對象方法 parentModel.TestFun(); } }