2016-05-16 15 views

答えて

1

必要なものに応じてこれを行う方法はいくつかあります。

例1

var ViewModel = function() { 
    var self = this; 
    self.Id = ko.observable(16); 
    self.name: ko.observable("SUkhi") 
}; 

var model = new ViewModel(); 
ko.applyBindings(model); 

// this will create models with the default values 

例2(私のお気に入り)

var ViewModel = function (ctor) { 
    var self = this; 
    var default: { 
     id = 16 
     name: "SUkhi" 
    } 
    self.Id = ko.observable(); 
    self.name: ko.observable() 

    /// if using pure JS 
    if(!!ctor){ 
     for(var i in ctor){ 
      if(ctor.hasOwnProperty("i") && self.hasOwnProperty("i"){ 
       if(ko.isSubscribable(self[i])) { // check if it is observable 
        self[i](ctor[i]) 
       } 
       else { 
        self[i] = ctor[i]; 
       } 
      } 
     } 
    } 
    // end pure JS 

    /// if using jquery 
    $.extend(self, ctor); 
    // end jquery 
}; 

var model = new ViewModel(); // or 
var model = new ViewModel({ Id: 5, Name: "Whateva"}) 
ko.applyBindings(model); 
メモリから書き込ま

が、そのすべてが

関連する問題