Skip to content

Instantly share code, notes, and snippets.

@bhameyie
Last active December 18, 2015 15:08
Show Gist options
  • Save bhameyie/5801864 to your computer and use it in GitHub Desktop.
Save bhameyie/5801864 to your computer and use it in GitHub Desktop.
Using Coffeescript with KnockoutJS
class LoginFormViewModel
constructor: -> #from my many attempts, things works out best when you set them up from the constructor
@email= ko.observable("")
@pass= ko.observable("")
@logon= => #notice the fat arrow here
$.post '/Account/Login',
userId: @email()
password: @pass()
(data) -> $('body').append "Successfully posted to the page."
window.app = window.app || {}
window.app.LoginFormViewModel = LoginFormViewModel #exporting this guy is important as it makes it usable by external scripts
(function() {
var LoginFormViewModel;
LoginFormViewModel = (function() {
function LoginFormViewModel() {
var _this = this;
this.email = ko.observable("");
this.pass = ko.observable("");
this.logon = function() {
$.post('/Account/Login', {
userId: _this.email(),
password: _this.pass()
});
return function(data) {
return $('body').append("Successfully posted to the page.");
};
};
}
return LoginFormViewModel;
})();
window.app = window.app || {};
window.app.LoginFormViewModel = LoginFormViewModel;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment