OSDN Git Service

fix: fetch err
[pettanr/pettanr.git] / app / assets / javascripts / peta / element_nestable_content.js.coffee
index c90638e..43c61ea 100644 (file)
 class Peta.ElementNestableContent extends Peta.Content\r
   element_items: (element_model) ->\r
     n = @my_class().my_manifest().associations.child_element_name(element_model)\r
-    attrs = @get(n)\r
-    if attrs\r
-      if @my_class().my_manifest().associations.is_has_one(element_model)\r
-        new element_model(attrs)\r
-      else\r
-        _.map attrs, (attr) ->\r
-          new element_model(attr)\r
+    @get(n)\r
   \r
   elements_items: () ->\r
-    _this = this\r
-    r = _.map @my_class().child_models, (element_model) ->\r
-      _this.element_items(element_model)\r
-    _.flatten(r)\r
+    r = _.map @my_class().child_models(), (element_model) =>\r
+      @element_items(element_model)\r
+    _.compact(_.flatten(r))\r
   \r
   boosts: (level) ->\r
     super(level)\r
-    _.each @elements_items(), (item) ->\r
-      item.boosts(level)\r
+    _.each @elements_items(), (item) =>\r
+      item.boosts(level) if item\r
+  \r
+  dig_elements: () ->\r
+    items = []\r
+    @_dig_elements(items, @attributes)\r
+    items\r
+  \r
+  _dig_elements: (items, attributes) ->\r
+    _.each attributes, (attribute, name) =>\r
+      a = @my_class().my_manifest().associations\r
+      if a.has_many[name]\r
+        _.each attribute, (attr) =>\r
+          items.push(attr)\r
+          Array.prototype.push.apply(items, attr.dig_elements())\r
+      if a.has_one[name]\r
+        items.push(attribute)\r
+        Array.prototype.push.apply(items, attribute.dig_elements())\r
+  \r
+  load_elements: (options) ->\r
+    @_load_elements(@dig_elements(), options)\r
+  \r
+  _load_elements: (items, options) ->\r
+    if _.isEmpty(items)\r
+      options.success.call(options.context, this)\r
+      return\r
+    item = items.shift()\r
+    item.fetch({\r
+      success: (model, response, opt) => \r
+        @_load_elements(items, options)\r
+      error: (item, response, opt) =>\r
+        options.fail.call(options.context, response, opt)\r
+    })\r
+  \r
+  decoded_attributes: () ->\r
+    attributes = {}\r
+    _.each @attributes, (attribute, name) =>\r
+      a = @my_class().my_manifest().associations\r
+      decoded = if a.has_many[name]\r
+        # has many association\r
+        model = a.has_many[name].model()\r
+        _.map attribute, (attr) =>\r
+          child = @cache_child(model, attr)\r
+          child.attributes = child.decoded_attributes()\r
+          child\r
+      else if a.has_one[name]\r
+        # has one association\r
+        model = a.has_one[name].model()\r
+        child = @cache_child(model, attribute)\r
+        child.attributes = child.decoded_attributes()\r
+        child\r
+      else\r
+        attribute\r
+      attributes[name] = decoded\r
+    attributes\r
+  \r
+  replaced_attributes: (options = {}) ->\r
+    attributes = {}\r
+    _.each @attributes, (attribute, name) =>\r
+      console.log name\r
+      console.log attribute\r
+      a = @my_class().my_manifest().associations\r
+      replaced = if a.has_many[name]\r
+        # has many association\r
+        _.map attribute, (decoded) =>\r
+          child = if options.hold\r
+            @replace_and_hold_element(decoded)\r
+          else\r
+            @replace_element(decoded)\r
+          console.log child\r
+          child.attributes = child.replaced_attributes(options)\r
+          child\r
+      else if a.has_one[name]\r
+        # has one association\r
+        child = if options.hold\r
+          @replace_and_hold_element(attribute)\r
+        else\r
+          @replace_element(attribute)\r
+        child.attributes = child.replaced_attributes(options)\r
+        child\r
+      else\r
+        attribute\r
+      console.log replaced\r
+      attributes[name] = replaced\r
+    attributes\r
+  \r
+  replace_element: (decoded_child) ->\r
+    cached_child = Pettanr.cache.restore(decoded_child)\r
+    item = if cached_child\r
+      cached_child\r
+    else\r
+      model = decoded_child.my_class()\r
+      caching_item = new model()\r
+      Pettanr.cache.store(caching_item)\r
+    item\r
+  \r
+  cache_child: (model, attr) ->\r
+    attr_without_element = {}\r
+    _.each attr, (a, n) ->\r
+      if !_.isObject(a) and !_.isArray(a)\r
+        attr_without_element[n] = a\r
+    decoded_child = new model(attr_without_element)\r
+    cached_child = Pettanr.cache.restore(decoded_child)\r
+    holden_item = if cached_child\r
+      cached_child.hold()\r
+    else\r
+      Pettanr.cache.store(decoded_child).hold()\r
+    holden_item.attributes = attr\r
+    holden_item\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
+      a = @my_class().my_manifest().associations\r
+      if a.has_many[name]\r
+        # has many association\r
+        _.each elements, (element) =>\r
+          element.release()\r
+      else if a.has_one[name]\r
+        # has one association\r
+        elements.release()\r
+  \r
+  fix_elements: () ->\r
+    return \r
+    _.each @attributes, (elements, name) =>\r
+      a = @my_class().my_manifest().associations\r
+      if a.has_many[name]\r
+        # has many association\r
+        _.each elements, (element) =>\r
+          if @has('_destroy')\r
+            element.release()  # destroy element by editor\r
+          else\r
+            element.fix()\r
+      else if a.has_one[name]\r
+        # has one association\r
+        if @has('_destroy')\r
+          element.release()\r
+        else\r
+          element.fix()\r
   \r