Class: UU::OS::VUC::ViewModel

Inherits:
Object
  • Object
show all
Defined in:
uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb

Overview

Class is used for building view response. Use ViewModel if you want get components, add message, change view template etc.

Examples:

def on_init(ctx)

  # get viewModel class from context
  view = ctx.view

  # get attribute
  format = view[:exportFormat] # String 'PDF'

  # set attribute
  view[:exportFormat] = 'PDF'

  # get component with code 'FORM_TEXT_AREA'
  view.component(:FORM_TEXT_AREA) # UU::OS::Form::Textarea

  # get all components, whose code ends at 'TEXT_AREA'
  view.find_components(".*TEXT_AREA$") # Array [UU::OS::Form::Textarea, UU::OS::Form::Textarea]

  info_msg = UU::OS::GVC::Message.new(
      {
          code: "CODE",
          severity: UU::OS::GVC::Message::MessageSeverity::INFO,
          text: "Info message."
      }
    )
  # add message to response
  view.add_message(info_msg)

  # get all messages
  view.messages # Array [UU::OS::GVC::Message, UU::OS::GVC::Message]

  # set focus on component with code 'FORM_TEXT_AREA'
  view.focus_component(:FORM_TEXT_AREA)

  # you must return this object as result so all changes will be proceed
  return view
end
def on_before_init(ctx)
  # get viewModel class from context
  view = ctx.view

  # set template uri to 'ues:UNI:VUC:EXTENDED_FORM'
  view.template_uri("ues:UNI:VUC:EXTENDED_FORM")

  return view
end
def on_value_change(ctx)
  # get viewModel class from context
  view = ctx.view

  # get component which trigger event
  component = view.event_source_component # UU::OS::Form::Textarea

end

Constant Summary

RESPONSE_VIEW_ATTRIBUTES =

Key used for storing view model attributes in JSON format.

:uuUcContextAttributes

Instance Method Summary (collapse)

Constructor Details

- (ViewModel) initialize(ctx)

Returns a new instance of ViewModel

Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 77

def initialize(ctx)
  @ctx = ctx
  @raw_attrs = UU::OS::Lang::UniformHash.new

  uuView = @template_uri = @ctx.parameters[:uuView]
  if uuView != nil
    @template_uri = uuView[:templateUri]
  end
  
  # restore parameters previously set to the ctx.view
  if ( @ctx.parameters[RESPONSE_VIEW_ATTRIBUTES] != nil )
    @ctx.parameters[RESPONSE_VIEW_ATTRIBUTES].each do |key,value|
      @raw_attrs[key]=value;
    end
    @ctx.parameters[RESPONSE_VIEW_ATTRIBUTES].clear; 
  end
end

Instance Method Details

- (Object) [](key)

Note:

Attribute’s key have some restrictions:

  • Key of the attribute must not start with ‘uu’ prefix (e.g. uuName, uusage), prefix ‘uu’ is reserved for internal usage.

  • Key of the attribute must not be same as code of some component. It may cause conflict and the component will not work as expected. E.g. you have component with code ‘NAME_INPUT’ so you can not use this code as attribute code.

Returns attribute's value.

Examples:

# get viewModel class from context
view = ctx.view

format = view[:exportFormat] # String 'PDF'

Parameters:

  • key (Symbol)

    Attribute key in symbol.

Returns:

  • (Object)

    Attribute value.



112
113
114
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 112

def [](key)
  @raw_attrs[key]
end

- (Object) []=(key, value)

Note:

Attribute’s key have some restrictions:

  • Key of the attribute must not start with ‘uu’ prefix (e.g. uuName, uusage), prefix ‘uu’ is reserved for internal usage.

  • Key of the attribute must not be same as code of some component. It may cause conflict and the component will not work as expected. For example you have component with code ‘NAME_INPUT’ so you can not use this code as attribute code.

Set attribute's value. Attributes has a Use Case scope. It's means that attribute will be available in all following actions on same Controller. E.g. If you set attribute in on_init(ctx) method, you can get this attribute in on_value_change(ctx) or on_submit(ctx) actions. Attributes are not forwarded to next Uca Case or between modal dialogs.

Examples:

# get viewModel class from context
view = ctx.view

view[:exportFormat] = 'PDF'

Parameters:

  • key (Symbol)

    Attribute key in symbol.

  • value (Object, nil)

    Attribute value or nil if attribute with key do not exists.



137
138
139
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 137

def []=(key, value)
  @raw_attrs[key] = value
end

- (Object) add_message(message)

Add user message, which will be displayed on form after this action.

Examples:


info_msg = UU::OS::GVC::Message.new(
    {
        code: "CODE",
        severity: UU::OS::GVC::Message::MessageSeverity::INFO,
        text: "Info message."
    })

# add message as UU::OS::GVC::Message object
view.add_message(info_msg)

msg_hash = {
    code: "CODE",
    severity: UU::OS::GVC::Message::MessageSeverity::INFO,
    text: "Info message."
}

# add message as Hash
view.add_message(msg_hash)

Parameters:

  • message (UU::OS::GVC::Message, Hash, String)

    Message to be added. Message can be UU::OS::GVC::Message, Hash or JSON String.



252
253
254
255
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 252

def add_message(message)
  container.add_message(message)
  return self
end

- (UU::OS::GVC::Component?) component(code)

Returns component with code passed as parameter or nil if component with this code does not exists.

Examples:

# get viewModel class from context
view = ctx.view

# get component with code 'FORM_TEXT_AREA'
text_area = view.component(:FORM_TEXT_AREA) # UU::OS::Form::Textarea

# get non existing component
view.component(:NON_EXISTS_CODE) # nil

Parameters:

  • code (Symbol)

    Component code as symbol.

Returns:

  • (UU::OS::GVC::Component, nil)

    Returns component by code or nil if component with this code does not exists.



156
157
158
159
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 156

def component(code)
  code = code.to_sym if code.kind_of? String
  container.component_by_code(code)
end

- (UU::OS::GVC::Component?) event_source_component

Returns component which trigger event. Result may be nil if event is not triggered by component. E.g. in UU::OS::VUC::VisualUseCaseController#on_init method.

Examples:

def on_value_change(ctx)
  # get viewModel class from context
  view = ctx.view

  # get component which trigger event
  component = view.event_source_component # UU::OS::Form::Textarea
  text = component.value

end

Returns:



369
370
371
372
373
374
375
376
377
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 369

def event_source_component
  uu_view = @ctx.parameters[:uuView]
  if (uu_view.nil?) || (uu_view[:sourceComponent].nil?)
    return nil
  end
  source_comp_code = uu_view[:sourceComponent]
  source_comp_code = source_comp_code.to_sym unless source_comp_code.nil?
  return container.component_by_code(source_comp_code)
end

- (Array) find_components(code_regex = nil) {|comp| ... }

Note:

It is forbidden to modify components in filtering block.

Search for component by regular expression or filtering block. If both expression and block are used, so block has bigger priority and expression is discarded. If no expression or block are available so all components are included in result. If expression or block match no component, empty array is returned.

will not be included in result. Otherwise component will be part of result.

Examples:

# get viewModel class from context
view = ctx.view

# get all components which code starts with 'LABEL', e.g. 'LABEL_NAME', 'LABEL_SURNAME' but not 'NAME_LABEL'
view.find_components("^LABEL") # Array [UU::OS::Form::Label, UU::OS::Form::Textarea]

# same as above but with Regexp object
view.find_components(/^LABEL/)

# get all components
view.find_components # Array [CompA, CompB, CompC]

# same as above
view.find_components(nil) # Array [CompA, CompB, CompC]

# if expression match no component, empty array is returned
view.find_components("NON_EXISTS") # Array []

# filter components using block, get all component with textarea type
view.find_components do |comp|
  comp.component_type == 'uu.os.form.textarea'
end

# get all read only textarea
view.find_components do |comp|
  (comp.component_type == 'uu.os.form.textarea') && (comp.read_only)
end

Parameters:

  • code_regex (String, Regexp) (defaults to: nil)

    Regular expression, all components which code match this expression will be returned in result.

Yields:

  • (comp)

    Block with one parameter that is actual component, if block returns false or nil so component

Yield Parameters:

Returns:

  • (Array)

    Returns array of filtered components.

See Also:

  • #Regexp


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 203

def find_components(code_regex = nil, &block)
  result = []

  if block_given?
    comps_list = container.components
    comps_list.each do |comp|
      include_comp = yield(comp)
      result << comp if include_comp
    end
  elsif !code_regex.nil?
    code_regex = code_regex.to_s
    comps_hash = container.get_components
    comps_hash.each do |key, comp|
      if key.to_s.match(code_regex)
        result << comp
      end
    end
  else
    result = container.components
  end

  return result
end

- (Object) focus_component(code)

Change focus of components.

Examples:

# set focus on component with code 'FORM_TEXT_AREA'
view.focus_component(:FORM_TEXT_AREA)

Parameters:

  • code (Symbol)

    Component code, which will be focused.



275
276
277
278
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 275

def focus_component(code)
  container.focus_by_code(code)
  return self
end

- (Object) height=(height)

Sets height of the VUC view. View size may be used by a page layout to set specific size of the VUC visual container. For example dialog window, in which the VUC view is showed, may used view size as the dialog size.

Examples:

# set dialog height
view.height = 300

Parameters:

  • height (Number)

    Dialog height.



301
302
303
304
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 301

def height=(height)
  container.height = height      
  return self
end

- (Array) messages

Returns all messages added by #add_message method.

Examples:

# get all messages
view.messages # Array [UU::OS::GVC::Message, UU::OS::GVC::Message]

Returns:

  • (Array)

    Array of UU::OS::GVC::Message.



264
265
266
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 264

def messages
  container.messages.clone
end

- (Object) template_uri(*uu_uri)

Note:

This parameters can be set only in on_before_init method, if you set template_uri in another

Because of historical reasons (due to backward compatibility), this method acts as getter (if no parameter is provided) or setter (the method is called with a template_uri parameter).

If this method is used as setter, it sets new view template. This template will be rendered as form. Default value is the main sheet of registered VUC. You can use sheets from this VUC only, it's means that only sheets related to actual VUC artifact are allowed, otherwise form execution fails with error. If you set non-exists uri than form execution will fail as well.

method, it will have no effect.

Examples:


# uri of default VUC
UCR_URI = "ues:UNI:CDS.GUESTBOOK/COMMENT/CREATE"

# code of sheet with extended form
EXTENDED_FORM_CODE = "EXTENDED_FORM"

def on_before_init(ctx)

  # get ViewModel instance from context
  view = ctx.view

  use_extended_form = ... # some logic to find out if user can see extended form

  if use_extended_form
    # set new sheet with extended form
    new_template = UU::OS::UESURIBuilder.parse_uesuri(UCR_URI).set_object_code(EXTENDED_FORM_CODE).to_uesuri
    view.template_uri(new_template)
  end

  # if you do not set template_uri on view so default is used

  return view
end

Parameters:

  • uu_uri (UU::OS::UESURI, String)

    UESURI of sheet that will be used as form template.



345
346
347
348
349
350
351
352
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 345

def template_uri(*uu_uri)
  if uu_uri.size == 0
    return @template_uri
  else
    @template_uri = uu_uri[0]        
  end
  return self
end

- (Object) to_json(opt = {})

Serialize object to JSON in format corresponding to schema ues:SYSTEM:UU.OS.VUC/RESPONSE_MESSAGE-SCHEMA-V3

Parameters:

  • opt (Hash) (defaults to: {})

    Optional parameters.

See Also:

  • #JSON


384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 384

def to_json(opt = {})
  response = {}

  # add components
  comp_list = container.get_components
  comp_list.each do |k, v|
    response[k] = v.attributes
  end
  
  # add raw attributes
  if (@raw_attrs != nil and @raw_attrs.size > 0)
    uuUcContextAttributes = {};
    @raw_attrs.each do |key, param|
      uuUcContextAttributes[key] = param
    end
    response[RESPONSE_VIEW_ATTRIBUTES] = uuUcContextAttributes;
  end
  
  # add messages
  msgs = container.messages
  if (!msgs.nil? && !msgs.empty?)
    view = create_view(response)
    view[:messages] = []
    msgs.each do |message|
      view[:messages] << message.raw_attrs
    end
  end

  # add focus if any
  focus = container.get_focus
  if focus
    view = create_view(response)
    view[:focus] = focus
  end
  
  # add view width
  width = container.width
  if width
    view = create_view(response)
    view[:width] = width
  end
  
  # add view height
  height = container.height
  if height
    view = create_view(response)
    view[:height] = height
  end

  # add view template
  unless @template_uri.nil?
    view = create_view(response)
    view[:templateUri] = @template_uri
  end
  response[:schemaUri] = RESPONSE_JSON_SCHEMA_URI
  return response.to_json(opt)
end

- (Object) width=(width)

Sets width of the VUC view. View size may be used by a page layout to set specific size of the VUC visual container. For example dialog window, in which the VUC view is showed, may used view size as the dialog size.

Examples:

# set dialog width
view.width = 500

Parameters:

  • width (Number)

    Dialog width.



288
289
290
291
# File 'uu_os_vuc-server-3.6.2/lib/uu/os/vuc/view_model.rb', line 288

def width=(width)
  container.width = width     
  return self
end