Created
April 16, 2015 14:29
-
-
Save grofit/883ffb4419babb156dee to your computer and use it in GitHub Desktop.
Some MVVM class based approach thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imagine this is a starting page where the user creates their account | |
function CreateUserViewModel() | |
{ | |
// could be done via IoC to improve testability and reuseability | |
var someAjaxService = {}; // Imagine it does something | |
this.user = new User(); | |
var saveDataToServer = function() { | |
var data = ko.toJS(this.user); | |
someAjaxService.Post("/users", data); | |
} | |
this.createUser = function() { | |
if(this.user.isValid()) { | |
saveDataToServer(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function User() { | |
this.id = ko.observable(0); | |
this.name = ko.observable("default").extend({required: true, minLength: 10, maxLength: 30); | |
this.email = ko.observable("not valid").extend({required: true, email: true }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assume this view model is on a page and the user is viewing their account | |
function ViewUserViewModel() | |
{ | |
var someAjaxService = {}; // Imagine it does something | |
this.user = new User(); | |
var updateUserData = function(userData) { | |
ko.merge(this.user, userData); | |
} | |
this.populateUserFromServer = function(userId) { | |
someAjaxService.Get("/users", userId) | |
.then(updateUserData); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment