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
16 replaced_attributes: (options = {}) ->
\r
18 _.each @attributes, (attribute, name) =>
\r
19 replaced = if _.isArray(attribute)
\r
20 # has many association
\r
21 model = @my_class().my_manifest().associations.has_many[name].model()
\r
22 _.map attribute, (attr) =>
\r
23 child = if options.hold
\r
24 @replace_and_hold_element(model, attr)
\r
26 @replace_element(model, attr)
\r
27 child.attributes = child.replaced_attributes(options)
\r
29 else if _.isObject(attribute)
\r
30 # has one association
\r
31 model = @my_class().my_manifest().associations.has_one[name].model()
\r
32 child = if options.hold
\r
33 @replace_and_hold_element(model, attribute)
\r
35 @replace_element(model, attribute)
\r
36 child.attributes = child.replaced_attributes(options)
\r
40 attributes[name] = replaced
\r
43 replace_element: (model, attr) ->
\r
44 empty_child = new model(attr)
\r
45 cached_child = Pettanr.cache.restore(empty_child)
\r
46 item = if cached_child
\r
49 Pettanr.cache.store(empty_child)
\r
50 item.attributes = attr
\r
53 replace_and_hold_element: (model, attr) ->
\r
54 empty_child = new model(attr)
\r
55 cached_child = Pettanr.cache.restore(empty_child)
\r
56 item = if cached_child
\r
59 Pettanr.cache.store(empty_child)
\r
60 holden = item.hold()
\r
61 holden.attributes = attr
\r
75 release_elements: () ->
\r
76 _.each @attributes, (elements, name) =>
\r
77 if _.isArray(elements)
\r
78 # has many association
\r
79 model = @my_class().my_manifest().associations.has_many[name].model()
\r
80 _.each elements, (element) =>
\r
82 else if _.isObject(elements)
\r
83 # has one association
\r
87 _.each @attributes, (elements, name) =>
\r
88 if _.isArray(elements)
\r
89 # has many association
\r
90 model = @my_class().my_manifest().associations.has_many[name].model()
\r
91 _.each elements, (element) =>
\r
93 else if _.isObject(elements)
\r
94 # has one association
\r