OSDN Git Service

add: success inspire
[pettanr/pettanr.git] / app / assets / javascripts / peta / element_nestable_content.js.coffee
index a00224e..a5e658d 100644 (file)
@@ -13,25 +13,80 @@ class Peta.ElementNestableContent extends Peta.Content
     _.each @elements_items(), (item) =>\r
       item.boosts(level) if item\r
   \r
-  replace_elements: () ->\r
-    _.each @my_class().child_models(), (element_model) =>\r
-      attrs = @element_items(element_model)\r
-      return if !attrs\r
-      if @my_class().my_manifest().associations.is_has_one(element_model)\r
-        empty_child = new element_model(attrs)\r
-        cached_child = Pettanr.cache.restore(empty_child)\r
-        Pettanr.cache.store(empty_child) if !cached_child\r
-        child = cached_child || empty_child\r
-        child.replace_elements()\r
-        @attributes[element_model.item_name()] = child  # eventless setting\r
-      else\r
-        list = _.map attrs, (attr) =>\r
-          empty_child = new element_model(attr)\r
-          cached_child = Pettanr.cache.restore(empty_child)\r
-          Pettanr.cache.store(empty_child) if !cached_child\r
-          child = cached_child || empty_child\r
-          child.replace_elements()\r
+  replaced_attributes: (options = {}) ->\r
+    attributes = {}\r
+    _.each @attributes, (attribute, name) =>\r
+      replaced = if _.isArray(attribute)\r
+        # has many association\r
+        model = @my_class().my_manifest().associations.has_many[name].model()\r
+        _.map attribute, (attr) =>\r
+          child = if options.hold\r
+            @replace_and_hold_element(model, attr)\r
+          else\r
+            @replace_element(model, attr)\r
+          child.attributes = child.replaced_attributes(options)\r
           child\r
-        @attributes[element_model.table_name()] = list  # eventless setting\r
-      \r
+      else if _.isObject(attribute)\r
+        # has one association\r
+        model = @my_class().my_manifest().associations.has_one[name].model()\r
+        child = if options.hold\r
+          @replace_and_hold_element(model, attribute)\r
+        else\r
+          @replace_element(model, attribute)\r
+        child.attributes = child.replaced_attributes(options)\r
+        child\r
+      else\r
+        attribute\r
+      attributes[name] = replaced\r
+    attributes\r
+  \r
+  replace_element: (model, attr) ->\r
+    empty_child = new model(attr)\r
+    cached_child = Pettanr.cache.restore(empty_child)\r
+    Pettanr.cache.store(empty_child) if !cached_child\r
+    cached_child || empty_child\r
+  \r
+  replace_and_hold_element: (model, attr) ->\r
+    empty_child = new model(attr)\r
+    cached_child = Pettanr.cache.restore(empty_child)\r
+    item = if cached_child\r
+      cached_child\r
+    else\r
+      Pettanr.cache.store(empty_child) \r
+    holden = item.hold()\r
+    holden.attributes = attr\r
+    holden\r
+  \r
+  hold: () ->\r
+    super()\r
+  \r
+  fix: () ->\r
+    super()\r
+    @fix_elements()\r
+  \r
+  release: () ->\r
+    super()\r
+    @release_elements()\r
+  \r
+  release_elements: () ->\r
+    _.each @attributes, (elements, name) =>\r
+      if _.isArray(elements)\r
+        # has many association\r
+        model = @my_class().my_manifest().associations.has_many[name].model()\r
+        _.each elements, (element) =>\r
+          element.release()\r
+      else if _.isObject(elements)\r
+        # has one association\r
+        elements.release()\r
+  \r
+  fix_elements: () ->\r
+    _.each @attributes, (elements, name) =>\r
+      if _.isArray(elements)\r
+        # has many association\r
+        model = @my_class().my_manifest().associations.has_many[name].model()\r
+        _.each elements, (element) =>\r
+          element.fix()\r
+      else if _.isObject(elements)\r
+        # has one association\r
+        elements.fix()\r
   \r