OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / assets / javascripts / peta / element_nestable_content.js.coffee
index ca8f905..ef01b55 100644 (file)
@@ -13,19 +13,30 @@ class Peta.ElementNestableContent extends Peta.Content
     _.each @elements_items(), (item) =>\r
       item.boosts(level) if item\r
   \r
-  get: (name) ->\r
-    val = super(name)\r
-    return val if !(typeof val == 'object')\r
-    attrs = val\r
-    r = null\r
-    _.each @my_class().child_models(), (element_model) =>\r
-      n = @my_class().my_manifest().associations.child_element_name(element_model)\r
-      if n == name\r
-        if @my_class().my_manifest().associations.is_has_one(element_model)\r
-          r = new element_model(attrs)\r
-        else\r
-          r = _.map attrs, (attr) =>\r
-            new element_model(attr)\r
-    r\r
+  replaced_attributes: () ->\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 = @replace_element(model, attr)\r
+          child.attributes = child.replaced_attributes()\r
+          child\r
+      else if _.isObject(attribute)\r
+        # has one association\r
+        model = @my_class().my_manifest().associations.has_one[name].model()\r
+        child = @replace_element(model, attribute)\r
+        child.attributes = child.replaced_attributes()\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