【发布时间】:2013-12-04 08:12:26
【问题描述】:
看看这两种方法并告诉我有什么区别。什么时候应该使用?
在第一种情况下,将创建实例,但在第二种情况下,ko.applyBindings() 时不会创建任何实例,所以这里是代码。
function MyViewModel() {
var self = this;
self.lastInterest = ko.observable();
self.places = ko.observableArray(['London', 'Paris', 'Tokyo']);
// The current item will be passed as the first parameter, so we know which place was hovered over
self.logMouseOver = function(place) {
self.lastInterest(place);
}
}
ko.applyBindings(new MyViewModel());
var viewModel = {
detailsEnabled: ko.observable(false),
enableDetails: function() {
this.detailsEnabled(true);
},
disableDetails: function() {
this.detailsEnabled(false);
}
};
ko.applyBindings(viewModel);
【问题讨论】:
-
另一个小问题是knockout js是google开发的库,因为当我去knockout js论坛时,url看起来像groups.google.com/group/knockoutjs。