我的 requirejs 模块相互引用和交互时遇到问题。
这是示例:jsFiddle
第一个模块将采用第二个模块的值,为什么 requirejs 会这样?
HTML:
<div id="first">
<input data-bind="value: user.Name" />
<input data-bind="value: user.Age" />
<button data-bind="click: editUser">Edit</button>
</div>
<div id="second">
<input data-bind="value: user.Name" />
<input data-bind="value: user.Age" />
<button data-bind="click: saveUser">Save</button>
</div>
JS:
define('vm',
[
'vm.edit',
'vm.save'
],
function(edit,save){
return{
save: save,
edit: edit
}
});
define('vm.save',function(){
var self = this;
self.user = {
Name: ko.observable(),
Age: ko.observable()
}
var saveUser = function() {
alert(self.user.Name() + " is age of " + self.user.Age());
}
return {
saveUser: saveUser,
user: user
}
});
define('vm.edit',function(){
var self = this;
self.user = {
Name: ko.observable(),
Age: ko.observable()
}
var editUser = function() {
alert(self.user.Name() + " is age of " + self.user.Age());
}
return {
editUser: editUser,
user: user
}
})
require(['vm'],function(vm){
ko.applyBindings(vm.save, document.getElementById("second"));
ko.applyBindings(vm.edit, document.getElementById("first"));
})