我一直在玩 Knockout,我不知道如何处理这个场景。
基本上我有一个员工列表,您可以从选择控件中为该员工选择一个职位。我使用 jquery 创建了一个小弹出窗口,以便用户可以添加新位置。然后,这将更新基础页面中的选择。我可以调用 viewmodel 的 addPosition 方法,它使用一些测试数据添加一个新位置,但我无法理解如何从弹出窗口的文本字段中的值填充它。
这是我的小提琴
http://jsfiddle.net/ricobano/HA5uz/3/
<div id="employeeWrapper">
<div data-bind="foreach: employees">
<div>
<label>Full Name</label>
<input type="text" data-bind="value: fullName" />
<label>Position</label>
<select id="slTest" data-bind="options: $root.Positions, value:position, optionsText:'name'"></select>
<label>Salary:</label><span data-bind="text: position().formatted"></span>
</div>
</div>
<button id="button" data-bind="click: addEmployee">Add Employee</button>
<button id="AddPosition" data-bind="click: open">Add Position</button>
</div>
<div data-bind="dialog: {autoOpen: false, title: 'Dialog test' }, dialogVisible: isOpen">
<div>
<label>Name:</label><input type="text"/>
</div>
<div>
<label>salary:</label><input type="text"/>
</div>
<button data-bind="click: addPosition">Add</button>
</div>
这是我的视图模型
ko.bindingHandlers.dialog = {
init:function(element, valueAccessor, allBindingsAccessor){
var options = ko.utils.unwrapObservable(valueAccessor()) || {};
setTimeout(function() {
options.close = function() {
allBindingsAccessor().dialogVisible(false);
};
$(element).dialog(options);
}, 0);
},
update: function(element, valueAccessor, allBindingsAccessor) {
var shouldBeOpen = ko.utils.unwrapObservable(allBindingsAccessor().dialogVisible),
$el = $(element),
dialog = $el.data("uiDialog") || $el.data("dialog")
//don't call open/close before initilization
if (dialog) {
$el.dialog(shouldBeOpen ? "open" : "close");
}
}
};
function Employee(fullname, position) {
var self = this;
self.fullName = ko.observable(fullname);
self.position = ko.observable(position);
};
function Position(data) {
var self = this;
self.id = ko.observable(data.id);
self.name = ko.observable(data.name);
self.salary = ko.observable(data.salary);
self.formatted = ko.computed(function(){
return "£" + self.salary();
});
};
function EmployeeVM() {
var self = this;
self.employees = ko.observableArray();
self.Positions = ko.observableArray([]);
self.isOpen = ko.observable(false);
var PositionsJSON = {
json: $.toJSON([{
"id": 1,
"name" : "No position",
"salary" : 0
},{
"id": 2,
"name" : "Web Developer",
"salary" : 15000
},{
"id": 3,
"name" : "Manager",
"salary" : 30000
}])
};
var data = {
json: $.toJSON([{
"fullName": "Richard Banks",
"position": {
"id": 2
}
}, {
"fullName": "Dave Grohl",
"position": {
"id": 3
}
}, {
"fullName": "bobby rahul",
"position": {
"id": 3
}
}])
};
$.when(
$.ajax({
url: "/echo/json/",
data: PositionsJSON,
type: "POST"
}),
$.ajax({
url: "/echo/json/",
data: data,
type: "POST"
})
).done(function(positionArgs, EmployeeArgs){
var ps = $.map(positionArgs[0], function(item){
return new Position(item);
});
self.Positions(ps);
$.each(EmployeeArgs[0], function (i, item) {
var p = ko.utils.arrayFirst(self.Positions(), function(p){
return p.id() == item.position.id;
});
var e = new Employee(item.fullName, p);
self.employees.push(e);
});
});
self.addPosition = function(){
self.Positions.push(new Position({id:"4", name:"Director", salary:"To much"}));
self.isOpen(false);
};
self.addEmployee = function(){
self.employees.push(new Employee("", self.Positions[0]));
};
self.open = function(){
self.isOpen(true);
};
}
ko.applyBindings(new EmployeeVM());