问题
Well, it can be similar to How to call an arbitrary service for saving changes with breezejs, but I didn't get a clear cut answer.
I have a Application with Knockout.js/Breeze.js/MVC4 and No Entity Framwork
My requirement
- I need to call an API when save button is clicked and pass the data to appropriate Controller using Breeze.
I have my button click event as:-
//Saving Profile Details
fnSave_click = function () {
//This profileModel will have the updated data which needs to be pushed to backend
var jsonData = {};
jsonData.fName = profileModel.fName;
jsonData.lname = profileModel.lname;
// Previously i used to make an Ajax call and save the data like below commented code:-
/*
$.ajax({
type: "POST",
url: 'api/profile/ChangeProfileDetails',
dataType: "json",
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(jsonData)
}).done(function (data) {
}).fail(function (request, error) {});
*/
//How do I pass my jsonData to Controller?? Is there any parameter option??
return manager.saveChanges()
.then(function () {
console.log("changes saved");
})
.fail(function () {
console.log("changes failed");
});
}
.......................
Now, I am not getting it... Where will i define my Controller Method Name? and How will i pass this data?
I have created my Breeze manager as below:-
// Start - Created breeze data service and manager for query on profile controller
var dataService = new breeze.DataService({
serviceName: "api/Profile",
hasServerMetadata: false // don't ask the server for metadata,
});
var manager = new breeze.EntityManager({
dataService: dataService
});
I have started with Breeze day before. So please let me know if I am doing something wrong.
[Updated]
Lastly, for ToDo app with Knockout and Breeze.js
which was well explained here .I am not getting -- how does this SaveChanges
(of controller) gets called when Save is clicked.
function saveChanges() {
return manager.saveChanges()//How does this calls SaveChanges method of controller
.then(function () { logger.success("changes saved"); })
.fail(saveFailed);
}
What i need to do if i need to call some another API Method like 'api/Test/MyMethod'
?
[Update 2] @Jay Traband
. After you reply, i thought of updating my question.
I have gone thru this link of release note which states:-
Added "Named Saves". By default the EntityManager.saveChanges method sends a save request to an endpoint called "SaveChanges". With "Named Save", you can target a different server endpoint such as an arbitrarily named action method on another Web API controller. There are two new properties on the SaveOptions class. A resourceName (for the action method) and a dataService (for targeting a different controller). Assuming that you want to save all pending changes, you could write
var so = new SaveOptions({
resourceName: "myCustomSave"
});
// null = 'all-pending-changes'; saveOptions is the 2nd parameter
myEntityManager.SaveChanges(null, so);
I tried like that,
I have my JS file code as -
debugger;
var option = new breeze.SaveOptions({ resourceName: 'myCustomSave' })
return manager.saveChanges(null, option)
and my Controller as :-
[HttpPost]
public SaveResult MyCustomSave(JObject saveBundle)
{
return null;
}
Now- This throws no error in console and it does not calls my Controller method.
回答1:
You can change the endpoint that Breeze uses to save changes by specifying a resource name in the SaveOptions of a SaveChanges call. Something like this
// Client
var so = new SaveOptions({ resourceName: "MyCustomSaveEndpoint" });
// listOfEntities may be null in which case all added/modified/deleted entities will be sent
return myEntityManager.saveChanges(listOfEntities, so);
and then on the server
[HttpPost]
public SaveResult MyCustomSaveEndpoint(JObject saveBundle) {
ContextProvider.BeforeSaveEntitiesDelegate = MyCustomSaveLogic;
return ContextProvider.SaveChanges(saveBundle);
}
The Breeze documentation has more information as to implement your BeforeSaveEntities method.
This approach is what you want to use if you are saving Breeze entities, because it handles updating the client side cache after save and provides presave valiidation of what is being saved on the client. If you just want to post data to the server that Breeze doesn't need to know about on the client, then just bypass Breeze completely and post this data directly via ajax.
来源:https://stackoverflow.com/questions/17183341/saving-data-using-breeze-js