Model View ViewModel
Login: <input type="email" data-bind="value: email" /><br />
Password: <input type="password" data-bind="value: password" /><br />
<button data-bind="click: register">Register</button>
function RegistrationViewModel() {
var self = this;
this.email = ko.observable("");
this.password = ko.observable("");
this.register = function() {
var model = {Login: self.email(), Password: self.password()};
$.ajax({url: "/Api/RegisterUser", type: "POST", data: model});
};
}
public class RegistrationDto {
public string Login {get; set;}
public string Password {get; set;}
}
Development order: 1. View, 2. ViewModel, 3. Ask server dev to implement Model. UI dev is master, server dev is slave.
ServiceAgent
this.register = function() {
var model = {Login: self.email(), Password: self.password()};
$.ajax({url: "/Api/RegisterUser", type: "POST", data: model});
};
Oops, I need to test it
function RegistrationViewModel() {
var self = this;
this.serviceAgent = new ServiceAgent();
this.email = ko.observable("");
this.password = ko.observable("");
this.register = function() {
var model = {Login: self.email(), Password: self.password()};
self.serviceAgent.registerUser(model);
};
}
function ServiceAgent() {
this.registerUser = function(model) {
return $.ajax({url: "/Api/RegisterUser", type: "POST", data: model});
};
}
var login = ko.observable("");
var val = login();
login("Admin");
var subscription = login.subscribe(function(newValue) {
/* ask server to check uniqueness */
});
subscription.dispose();
var products = ko.observableArray([]);
var arrayValue = products();
var value = products()[0];
products.push(product);
products.remove(product);
var fullName = ko.computed(function() {
return firstName() + " " + lastName();
});
var fullName = ko.computed({
read: function () {
return firstName() + " " + lastName();
},
write: function (value) {
var lastSpacePos = value.lastIndexOf(" ");
firstName(value.substring(0, lastSpacePos));
lastName(value.substring(lastSpacePos + 1));
}
});
ko.applyBindings(new RegistrationViewModel());
ko.bindingHandlers["slideFromRight"] = {
init: function(element, valueAccessor) {
// will be called when the binding is first applied to an element
},
update: function(element, valueAccessor) {
// will be called when the binding is first applied to an element,
// and again whenever the associated observable changes value.
}
};