简体   繁体   English

保存/提交时,如何从余烬中的表单中删除空元素?

[英]How to remove empty elements from a form in ember, when saving/submitting?

Let's say I have an action in my controller like: 假设我在控制器中有一个操作,例如:

actions: {
  save: function() {
    var self = this;
    this.get('model').save().then(function() {
      self.transitionToRoute('surveys');
    }, $.noop);
}

The model "hasMany" elements of type "element" (another model, with just a field "title"), that I can access from the controller as: 我可以通过以下方式从控制器访问类型为“ element”的模型“ hasMany”元素(另一个模型,只有一个字段“ title”):

var elements = this.get('elements');

I want to remove the elements with empty title before I save. 我想在保存之前删除标题为空的元素。 I've tried iterating over the "elements" with forEach and filter, but none of my approaches have worked so far. 我尝试使用forEach和filter迭代“元素”,但是到目前为止,我的方法都没有奏效。

Any suggestions? 有什么建议么? Thank you! 谢谢! :) :)

The problem was residing in the way I was deleting the elements. 问题在于我删除元素的方式。

Instead of calling destroyRecord() on the elements, the solution is to call removeObject() on the elements array, with the element we want to remove as an option. 解决方案不是在元素上调用destroyRecord(),而是在elements数组上调用removeObject(),并将要删除的元素作为选项。

Such as: 如:

var elements = this.get('elements');

elements.forEach(
  function (element) {
    if(element && ! element.get('title')){
      elements.removeObject(element);
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM