瀏覽代碼

Fix collection nested javascript

Current javascript does not work well when using nested form as replace(/__name__ replaces all occurrences of __name__, even in prototype-data child tags. I used parent id to avoid nested replacement
netounet 12 年之前
父節點
當前提交
0d3c6ea556
共有 1 個文件被更改,包括 9 次插入1 次删除
  1. 9 1
      Resources/public/base.js

+ 9 - 1
Resources/public/base.js

@@ -156,7 +156,15 @@ var Admin = {
 
             var container = jQuery(this).closest('[data-prototype]');
             var proto = container.attr('data-prototype');
-            proto = proto.replace(/__name__/g, container.children().length);
+            // Set field id
+            var idRegexp = new RegExp(container.attr('id')+'___name__','g');
+            proto = proto.replace(idRegexp, container.attr('id')+'_'+container.children().length);
+            
+            // Set field name
+            var parts = container.attr('id').split('_');
+            var nameRegexp = new RegExp(parts[parts.length-1]+'\\]\\[__name__','g');
+            proto = proto.replace(nameRegexp, parts[parts.length-1]+']['+container.children().length);
+            jQuery(proto).insertBefore(jQuery(this).parent());
             jQuery(proto).insertBefore(jQuery(this).parent());
             
             jQuery(this).trigger('sonata-collection-item-added');