- You must use
.then()
instead of.done()
to modify the payload. - You should let your
createCustomer
method return the modified payload.
Your module definition becomes:
function createCustomer() {
return $.getJSON('Customer/Create')
.then(function (response) {
return new CreateCustomerViewModel(response);
});
}
return {
createCustomer: createCustomer
};
Use as:
customerCreator.createCustomer()
.done(function (model) {
// yay! let's use this model
})
.fail(function () {
console.error("Cannot create customer");
});
You should also move the .fail()
handling outside, as high up in the application as possible, so that the wording in your error message can have a more accurate context.
P.S. As a nitpick, shouldn't you be using a POST request instead of a GET to create an entity?