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
41 success: (model, response, opt) =>
\r
42 @_load_elements(items, options)
\r
43 error: (item, response, opt) =>
\r
44 options.fail.call(options.context, response, opt)
\r
47 decoded_attributes: () ->
\r
49 _.each @attributes, (attribute, name) =>
\r
50 a = @my_class().my_manifest().associations
\r
51 decoded = if a.has_many[name]
\r
52 # has many association
\r
53 model = a.has_many[name].model()
\r
54 _.map attribute, (attr) =>
\r
55 child = @cache_child(model, attr)
\r
56 child.attributes = child.decoded_attributes()
\r
58 else if a.has_one[name]
\r
59 # has one association
\r
60 model = a.has_one[name].model()
\r
61 child = @cache_child(model, attribute)
\r
62 child.attributes = child.decoded_attributes()
\r
66 attributes[name] = decoded
\r
69 replaced_attributes: (options = {}) ->
\r
71 _.each @attributes, (attribute, name) =>
\r
73 console.log attribute
\r
74 a = @my_class().my_manifest().associations
\r
75 replaced = if a.has_many[name]
\r
76 # has many association
\r
77 _.map attribute, (decoded) =>
\r
78 child = if options.hold
\r
79 @replace_and_hold_element(decoded)
\r
81 @replace_element(decoded)
\r
83 child.attributes = child.replaced_attributes(options)
\r
85 else if a.has_one[name]
\r
86 # has one association
\r
87 child = if options.hold
\r
88 @replace_and_hold_element(attribute)
\r
90 @replace_element(attribute)
\r
91 child.attributes = child.replaced_attributes(options)
\r
95 console.log replaced
\r
96 attributes[name] = replaced
\r
99 replace_element: (decoded_child) ->
\r
100 cached_child = Pettanr.cache.restore(decoded_child)
\r
101 item = if cached_child
\r
104 model = decoded_child.my_class()
\r
105 caching_item = new model()
\r
106 Pettanr.cache.store(caching_item)
\r
109 cache_child: (model, attr) ->
\r
110 attr_without_element = {}
\r
111 _.each attr, (a, n) ->
\r
112 if !_.isObject(a) and !_.isArray(a)
\r
113 attr_without_element[n] = a
\r
114 decoded_child = new model(attr_without_element)
\r
115 cached_child = Pettanr.cache.restore(decoded_child)
\r
116 holden_item = if cached_child
\r
117 cached_child.hold()
\r
119 Pettanr.cache.store(decoded_child).hold()
\r
120 holden_item.attributes = attr
\r
132 @release_elements()
\r
134 release_elements: () ->
\r
135 _.each @attributes, (elements, name) =>
\r
136 a = @my_class().my_manifest().associations
\r
137 if a.has_many[name]
\r
138 # has many association
\r
139 _.each elements, (element) =>
\r
141 else if a.has_one[name]
\r
142 # has one association
\r
145 fix_elements: () ->
\r
147 _.each @attributes, (elements, name) =>
\r
148 a = @my_class().my_manifest().associations
\r
149 if a.has_many[name]
\r
150 # has many association
\r
151 _.each elements, (element) =>
\r
152 if @has('_destroy')
\r
153 element.release() # destroy element by editor
\r
156 else if a.has_one[name]
\r
157 # has one association
\r
158 if @has('_destroy')
\r