使用Breeze.js保存数据

问题描述:

嗯,它类似于如何通过breezejs调用任意服务来保存更改,但我没有明确的答案。

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.

我有一个 Knockout.js / Breeze.js / MVC4并且没有实体框架

我的要求-单击保存按钮时,我需要调用API,然后使用Breeze将数据传递到相应的Controller。

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?

我创建了Breeze管理器,如下所示:-

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.

[已更新]

最后,对于具有Knockout和Breeze.js的ToDo应用对此进行了很好的解释这里。我不明白- SaveChanges $如何单击保存时将调用c $ c>(控制器的)。

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);
    }

如果我需要调用其他API方法(如'api / Test / MyMethod'

What i need to do if i need to call some another API Method like 'api/Test/MyMethod'?

[更新2] @Jay Traband 。在您回复后,我想到了更新我的问题。

[Update 2] @Jay Traband. After you reply, i thought of updating my question.

我已经通过此发行说明链接其中指出:-

I have gone thru this link of release note which states:-


添加了命名保存 。默认情况下,EntityManager.saveChanges方法
将保存请求发送到名为 SaveChanges的端点。使用命名的
保存,您可以定位另一个服务器端点,例如另一个Web API控制器上的
任意命名的操作方法。
在SaveOptions类上有两个新属性。一个resourceName(用于
的动作方法)和一个dataService(用于定位另一个
控制器)。假设您要保存所有待处理的更改,则
可以写

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我的JS文件代码为-

I have my JS file code as -

debugger;
            var option = new breeze.SaveOptions({ resourceName: 'myCustomSave' })
            return manager.saveChanges(null, option)

和我的控制器为:-

[HttpPost]
public SaveResult MyCustomSave(JObject saveBundle)
{
    return null;
}

现在-在控制台中抛出没有错误它不会调用我的控制器方法 d。

Now- This throws no error in console and it does not calls my Controller method.

您可以将Breeze用于的端点更改为通过在SaveChanges调用的SaveOptions中指定资源名称来保存更改。

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);

然后在服务器上

[HttpPost]
public SaveResult MyCustomSaveEndpoint(JObject saveBundle) {
  ContextProvider.BeforeSaveEntitiesDelegate = MyCustomSaveLogic;
  return ContextProvider.SaveChanges(saveBundle);
}

Breeze文档提供了有关实现BeforeSaveEntities方法的更多信息。

The Breeze documentation has more information as to implement your BeforeSaveEntities method.

此方法是保存Breeze实体时要使用的方法,因为它在保存后处理更新客户端缓存并提供对保存在客户端上的内容的预先保存验证。如果您只想将Breeze不需要了解的数据发布到服务器上,那么就完全绕过Breeze并直接通过ajax发布这些数据。

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.