1 class Peta.ElementNestableContent extends Peta.Content
\r
2 element_items: (element_model) ->
\r
3 n = @my_class().my_manifest().associations.child_element_name(element_model)
\r
6 elements_items: () ->
\r
7 r = _.map @my_class().child_models(), (element_model) =>
\r
8 @element_items(element_model)
\r
9 _.compact(_.flatten(r))
\r
13 _.each @elements_items(), (item) =>
\r
14 item.boosts(level) if item
\r
18 @_dig_elements(items, @attributes)
\r
21 _dig_elements: (items, attributes) ->
\r
22 _.each attributes, (attribute, name) =>
\r
23 a = @my_class().my_manifest().associations
\r
25 _.each attribute, (attr) =>
\r
27 Array.prototype.push.apply(items, attr.dig_elements())
\r
29 items.push(attribute)
\r
30 Array.prototype.push.apply(items, attribute.dig_elements())
\r
32 load_elements: (options) ->
\r
33 @_load_elements(@dig_elements(), options)
\r
35 _load_elements: (items, options) ->
\r
37 options.success.call(options.context, this)
\r
39 item = items.shift()
\r
40 item.fetch().done =>
\r
41 @_load_elements(items, options)
\r
43 decoded_attributes: () ->
\r
45 _.each @attributes, (attribute, name) =>
\r
46 a = @my_class().my_manifest().associations
\r
47 decoded = if a.has_many[name]
\r
48 # has many association
\r
49 model = a.has_many[name].model()
\r
50 _.map attribute, (attr) =>
\r
51 child = @cache_child(model, attr)
\r
52 child.attributes = child.decoded_attributes()
\r
54 else if a.has_one[name]
\r
55 # has one association
\r
56 model = a.has_one[name].model()
\r
57 child = @cache_child(model, attribute)
\r
58 child.attributes = child.decoded_attributes()
\r
62 attributes[name] = decoded
\r
65 replaced_attributes: (options = {}) ->
\r
67 _.each @attributes, (attribute, name) =>
\r
69 console.log attribute
\r
70 a = @my_class().my_manifest().associations
\r
71 replaced = if a.has_many[name]
\r
72 # has many association
\r
73 _.map attribute, (decoded) =>
\r
74 child = if options.hold
\r
75 @replace_and_hold_element(decoded)
\r
77 @replace_element(decoded)
\r
79 child.attributes = child.replaced_attributes(options)
\r
81 else if a.has_one[name]
\r
82 # has one association
\r
83 child = if options.hold
\r
84 @replace_and_hold_element(attribute)
\r
86 @replace_element(attribute)
\r
87 child.attributes = child.replaced_attributes(options)
\r
91 console.log replaced
\r
92 attributes[name] = replaced
\r
95 replace_element: (decoded_child) ->
\r
96 cached_child = Pettanr.cache.restore(decoded_child)
\r
97 item = if cached_child
\r
100 model = decoded_child.my_class()
\r
101 caching_item = new model()
\r
102 Pettanr.cache.store(caching_item)
\r
105 cache_child: (model, attr) ->
\r
106 attr_without_element = {}
\r
107 _.each attr, (a, n) ->
\r
108 if !_.isObject(a) and !_.isArray(a)
\r
109 attr_without_element[n] = a
\r
110 decoded_child = new model(attr_without_element)
\r
111 cached_child = Pettanr.cache.restore(decoded_child)
\r
112 holden_item = if cached_child
\r
113 cached_child.hold()
\r
115 Pettanr.cache.store(decoded_child).hold()
\r
116 holden_item.attributes = attr
\r
128 @release_elements()
\r
130 release_elements: () ->
\r
131 _.each @attributes, (elements, name) =>
\r
132 a = @my_class().my_manifest().associations
\r
133 if a.has_many[name]
\r
134 # has many association
\r
135 _.each elements, (element) =>
\r
137 else if a.has_one[name]
\r
138 # has one association
\r
141 fix_elements: () ->
\r
143 _.each @attributes, (elements, name) =>
\r
144 a = @my_class().my_manifest().associations
\r
145 if a.has_many[name]
\r
146 # has many association
\r
147 _.each elements, (element) =>
\r
148 if @has('_destroy')
\r
149 element.release() # destroy element by editor
\r
152 else if a.has_one[name]
\r
153 # has one association
\r
154 if @has('_destroy')
\r