javascript 如何使用超过 $data 作为参数的 Angular-Xeditable 的 onBeforeSave / onAfterSave 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23418753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Angular-Xeditable's onBeforeSave / onAfterSave methods with more than $data as parameter
提问by Michael Oryl
When first using Angular-Xeditable with my application, I ran into a problem trying to figure out how to save an x-editable change to an object that was accessed through an ng-repeat loop.
当第一次在我的应用程序中使用 Angular-Xeditable 时,我遇到了一个问题,试图找出如何将 x 可编辑更改保存到通过 ng-repeat 循环访问的对象。
The documentation primarily focuses on using onbeforesave and onaftersave for validation, and while it does show that it can be used to save things, the examples don't show how to pass anything more than $data (or $index) to your onbeforesave/onaftersave method. The samples show saving something like $scope.person, which is fine if you have one item.
该文档主要侧重于使用 onbeforesave 和 onaftersave 进行验证,虽然它确实表明它可用于保存内容,但示例并未展示如何将 $data(或 $index)以外的任何内容传递给您的 onbeforesave/onaftersave方法。示例显示保存诸如 $scope.person 之类的内容,如果您有一个项目,这很好。
But what if the 3rd person in a list of 30 was edited? You certainly don't want to have to save them all. How do you save just the object that was edited instead of everything in the array?
但是如果编辑了 30 人名单中的第三个人呢?您当然不想将它们全部保存。如何只保存已编辑的对象而不是数组中的所有内容?
回答by Michael Oryl
The Angular-Xeditable docs don't make it quite clear, but it appears that $data (and $index) are just injected values and that you can pass pretty much anything you want to the save/validate methods. So what you ought to be doing is passing the object itself (or maybe just its id).
Angular-Xeditable 文档并没有说得很清楚,但看起来 $data(和 $index)只是注入的值,你可以将几乎任何你想要的东西传递给 save/validate 方法。所以你应该做的是传递对象本身(或者可能只是它的 id)。
Given the following markup:
鉴于以下标记:
<div ng-repeat="p in people">
<a href="#" editable-text="p.name" onaftersave="updatePerson(p)">{{p.name}}</a>
</div>
You'd then have the following code in your controller:
然后,您的控制器中有以下代码:
$scope.updatePerson = function(person) {
var ret = PersonService.save(person); // or whatever save mechanism you use
// You need to return a error string if save fails, true otherwise
if (ret !== null) { // You have to make sure this logic works for your save service
return ret;
}
return true;
}
With this, you can then handle the save any way you like. You aren't bound to using just the $data or $index values that Angular-Xeditable supplies. You could just as easily do something like this:
有了这个,你就可以以任何你喜欢的方式处理保存。您不必只使用 Angular-Xeditable 提供的 $data 或 $index 值。你可以很容易地做这样的事情:
<div ng-repeat="p in people">
<a href="#" editable-text="p.name" onaftersave="updatePerson(p.id, p.name, p.dohickey)">{{p.name}}</a>
</div>
And update only the name and dohickey fields for the person with the supplied ID value. You would, of course, have to change the updatePerson() method to instead expect an ID as the first param and the name and dohickey as 2nd and 3rd params.
并仅更新具有提供的 ID 值的人员的姓名和 dohickey 字段。当然,您必须更改 updatePerson() 方法,以将 ID 作为第一个参数,将名称和 dohickey 作为第二个和第三个参数。
Also note that if you use onBeforeSave instead of onAfterSave as I did here, p.name won't yet have the new value and you'd need to go back to using $data to get the new value.
另请注意,如果您像我在这里所做的那样使用 onBeforeSave 而不是 onAfterSave,则 p.name 还没有新值,您需要返回使用 $data 来获取新值。