Only send changes back to client
This commit is contained in:
@@ -24,10 +24,10 @@ feature
|
||||
local
|
||||
panel: WSF_MULTI_CONTROL
|
||||
do
|
||||
create textbox1.make ("txtBox1", "1")
|
||||
create textbox2.make ("txtBox2", "2")
|
||||
create button1.make ("sample_button1", "SUM")
|
||||
create textbox_result.make ("txtBox3", "")
|
||||
create textbox1.make_text ("txtBox1", "1")
|
||||
create textbox2.make_text ("txtBox2", "2")
|
||||
create button1.make_button ("sample_button1", "SUM")
|
||||
create textbox_result.make_text ("txtBox3", "")
|
||||
button1.set_click_event (agent handle_click)
|
||||
create panel.make ("panel")
|
||||
panel.add_control (textbox1)
|
||||
|
||||
@@ -7,7 +7,6 @@ trigger_callback = (control_name,event)->
|
||||
cache: no
|
||||
.done (new_states)->
|
||||
#Update all classes
|
||||
window.states = new_states
|
||||
for name,state of new_states
|
||||
controls[name]?.update(state)
|
||||
return
|
||||
@@ -35,7 +34,9 @@ class WSF_BUTTON_CONTROL extends WSF_CONTROL
|
||||
trigger_callback(@control_name, 'click')
|
||||
|
||||
update: (state) ->
|
||||
@$el.text(state.text)
|
||||
if state.text?
|
||||
window.states[@control_name]['text'] = state.text
|
||||
@$el.text(state.text)
|
||||
|
||||
class WSF_TEXT_CONTROL extends WSF_CONTROL
|
||||
attach_events: ()->
|
||||
@@ -49,7 +50,9 @@ class WSF_TEXT_CONTROL extends WSF_CONTROL
|
||||
trigger_callback(@control_name, 'change')
|
||||
|
||||
update: (state) ->
|
||||
@$el.val(state.text)
|
||||
if state.text?
|
||||
window.states[@control_name]['text'] = state.text
|
||||
@$el.val(state.text)
|
||||
|
||||
#map class name to effectiv class
|
||||
typemap =
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
cache: false
|
||||
}).done(function(new_states) {
|
||||
var name, state, _ref;
|
||||
window.states = new_states;
|
||||
for (name in new_states) {
|
||||
state = new_states[name];
|
||||
if ((_ref = controls[name]) != null) {
|
||||
@@ -66,7 +65,10 @@
|
||||
};
|
||||
|
||||
WSF_BUTTON_CONTROL.prototype.update = function(state) {
|
||||
return this.$el.text(state.text);
|
||||
if (state.text != null) {
|
||||
window.states[this.control_name]['text'] = state.text;
|
||||
return this.$el.text(state.text);
|
||||
}
|
||||
};
|
||||
|
||||
return WSF_BUTTON_CONTROL;
|
||||
@@ -97,7 +99,10 @@
|
||||
};
|
||||
|
||||
WSF_TEXT_CONTROL.prototype.update = function(state) {
|
||||
return this.$el.val(state.text);
|
||||
if (state.text != null) {
|
||||
window.states[this.control_name]['text'] = state.text;
|
||||
return this.$el.val(state.text);
|
||||
}
|
||||
};
|
||||
|
||||
return WSF_TEXT_CONTROL;
|
||||
|
||||
@@ -12,20 +12,20 @@ inherit
|
||||
WSF_CONTROL
|
||||
|
||||
create
|
||||
make
|
||||
make_button
|
||||
|
||||
feature {NONE}
|
||||
|
||||
make (n: STRING; v: STRING)
|
||||
make_button (n: STRING; v: STRING)
|
||||
do
|
||||
control_name := n
|
||||
make (n)
|
||||
text := v
|
||||
end
|
||||
|
||||
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
|
||||
set_state (new_state: JSON_OBJECT)
|
||||
-- Restore text from json
|
||||
-- Restore text from json
|
||||
do
|
||||
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then
|
||||
text := new_text.unescaped_string_32
|
||||
@@ -33,7 +33,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
end
|
||||
|
||||
state: JSON_OBJECT
|
||||
-- Return state which contains the current text and if there is an event handle attached
|
||||
-- Return state which contains the current text and if there is an event handle attached
|
||||
do
|
||||
create Result.make
|
||||
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
|
||||
@@ -43,7 +43,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
feature --EVENT HANDLING
|
||||
|
||||
set_click_event (e: attached like click_event)
|
||||
-- Set button click event handle
|
||||
-- Set button click event handle
|
||||
do
|
||||
click_event := e
|
||||
end
|
||||
@@ -64,7 +64,10 @@ feature
|
||||
|
||||
set_text (t: STRING)
|
||||
do
|
||||
text := t
|
||||
if not t.is_equal (text) then
|
||||
text := t
|
||||
state_changes.replace (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
@@ -11,10 +11,20 @@ feature
|
||||
|
||||
control_name: STRING
|
||||
|
||||
feature {NONE}
|
||||
|
||||
make (n: STRING)
|
||||
do
|
||||
control_name := n
|
||||
create state_changes.make
|
||||
ensure
|
||||
attached state_changes
|
||||
end
|
||||
|
||||
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
|
||||
load_state (new_states: JSON_OBJECT)
|
||||
-- Select state stored with `control_name` as key
|
||||
-- Select state stored with `control_name` as key
|
||||
do
|
||||
if attached {JSON_OBJECT} new_states.item (create {JSON_STRING}.make_json (control_name)) as new_state_obj then
|
||||
set_state (new_state_obj)
|
||||
@@ -22,32 +32,42 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
end
|
||||
|
||||
set_state (new_state: JSON_OBJECT)
|
||||
-- Before we process the callback. We restore the state of control.
|
||||
-- Before we process the callback. We restore the state of control.
|
||||
deferred
|
||||
end
|
||||
|
||||
read_state (states: JSON_OBJECT)
|
||||
-- Add a new entry in the `states` JSON object with the `control_name` as key and the `state` as value
|
||||
-- Add a new entry in the `states` JSON object with the `control_name` as key and the `state` as value
|
||||
do
|
||||
states.put (state, create {JSON_STRING}.make_json (control_name))
|
||||
end
|
||||
|
||||
read_state_changes (states: JSON_OBJECT)
|
||||
-- Add a new entry in the `states_changes` JSON object with the `control_name` as key and the `state` as value
|
||||
do
|
||||
if state_changes.count > 0 then
|
||||
states.put (state_changes, create {JSON_STRING}.make_json (control_name))
|
||||
end
|
||||
end
|
||||
|
||||
state: JSON_OBJECT
|
||||
-- Returns the current state of the Control as JSON. This state will be transfered to the client.
|
||||
-- Returns the current state of the Control as JSON. This state will be transfered to the client.
|
||||
deferred
|
||||
end
|
||||
|
||||
state_changes: JSON_OBJECT
|
||||
|
||||
feature --EVENT HANDLING
|
||||
|
||||
handle_callback (cname: STRING; event: STRING)
|
||||
-- Method called if any callback recived. In this method you can route the callback to the event handler
|
||||
-- Method called if any callback recived. In this method you can route the callback to the event handler
|
||||
deferred
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
render: STRING
|
||||
-- Return html representaion of control
|
||||
-- Return html representaion of control
|
||||
deferred
|
||||
end
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ inherit
|
||||
|
||||
WSF_CONTROL
|
||||
redefine
|
||||
make,
|
||||
read_state,
|
||||
read_state_changes,
|
||||
load_state
|
||||
end
|
||||
|
||||
@@ -24,16 +26,16 @@ feature {NONE}
|
||||
|
||||
make (n: STRING)
|
||||
do
|
||||
control_name := n
|
||||
Precursor (n)
|
||||
controls := create {LINKED_LIST [WSF_CONTROL]}.make;
|
||||
end
|
||||
|
||||
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
|
||||
load_state (new_states: JSON_OBJECT)
|
||||
-- Pass new_states to subcontrols
|
||||
-- Pass new_states to subcontrols
|
||||
do
|
||||
Precursor(new_states)
|
||||
Precursor (new_states)
|
||||
across
|
||||
controls as c
|
||||
loop
|
||||
@@ -46,9 +48,9 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
end
|
||||
|
||||
read_state (states: JSON_OBJECT)
|
||||
-- Read states in subcontrols
|
||||
-- Read states in subcontrols
|
||||
do
|
||||
Precursor(states)
|
||||
Precursor (states)
|
||||
across
|
||||
controls as c
|
||||
loop
|
||||
@@ -56,8 +58,19 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
end
|
||||
end
|
||||
|
||||
read_state_changes (states: JSON_OBJECT)
|
||||
-- Read states_changes in subcontrols
|
||||
do
|
||||
Precursor (states)
|
||||
across
|
||||
controls as c
|
||||
loop
|
||||
c.item.read_state_changes (states)
|
||||
end
|
||||
end
|
||||
|
||||
state: JSON_OBJECT
|
||||
--Read state
|
||||
--Read state
|
||||
do
|
||||
create Result.make
|
||||
end
|
||||
@@ -65,7 +78,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
feature --EVENT HANDLING
|
||||
|
||||
handle_callback (event: STRING; cname: STRING)
|
||||
-- Pass callback to subcontrols
|
||||
-- Pass callback to subcontrols
|
||||
do
|
||||
if equal (cname, control_name) then
|
||||
else
|
||||
|
||||
@@ -25,27 +25,27 @@ feature -- Access
|
||||
feature
|
||||
|
||||
initialize_controls
|
||||
-- Initalize all the controls, all the event handles must be set in this function.
|
||||
-- Initalize all the controls, all the event handles must be set in this function.
|
||||
deferred
|
||||
ensure
|
||||
attached control
|
||||
end
|
||||
|
||||
process
|
||||
-- Function called on page load (not on callback)
|
||||
-- Function called on page load (not on callback)
|
||||
deferred
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
execute
|
||||
-- Entry Point: If request is a callback, restore control states and execute handle then return new state json.
|
||||
-- If request is not a callback. Run process and render the html page
|
||||
-- Entry Point: If request is a callback, restore control states and execute handle then return new state json.
|
||||
-- If request is not a callback. Run process and render the html page
|
||||
local
|
||||
event: detachable STRING
|
||||
control_name: detachable STRING
|
||||
states: detachable STRING
|
||||
new_states: JSON_OBJECT
|
||||
states_changes: JSON_OBJECT
|
||||
json_parser: JSON_PARSER
|
||||
do
|
||||
control_name := get_parameter ("control_name")
|
||||
@@ -57,10 +57,10 @@ feature
|
||||
control.load_state (sp)
|
||||
end
|
||||
control.handle_callback (control_name, event)
|
||||
create new_states.make
|
||||
control.read_state (new_states)
|
||||
create states_changes.make
|
||||
control.read_state_changes (states_changes)
|
||||
response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "application/json"]>>)
|
||||
response.put_string (new_states.representation)
|
||||
response.put_string (states_changes.representation)
|
||||
else
|
||||
process
|
||||
render
|
||||
@@ -68,7 +68,7 @@ feature
|
||||
end
|
||||
|
||||
render
|
||||
-- Render and send the HTML Page
|
||||
-- Render and send the HTML Page
|
||||
local
|
||||
data: STRING
|
||||
page: WSF_PAGE_RESPONSE
|
||||
@@ -92,13 +92,13 @@ feature
|
||||
end
|
||||
|
||||
get_parameter (key: STRING): detachable STRING
|
||||
-- Read query parameter as string
|
||||
-- Read query parameter as string
|
||||
local
|
||||
value: detachable WSF_VALUE
|
||||
do
|
||||
Result := VOID
|
||||
value := request.query_parameter (key)
|
||||
if attached value and then value.is_string then
|
||||
if attached value and then value.is_string then
|
||||
Result := value.as_string.value
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,20 +12,20 @@ inherit
|
||||
WSF_CONTROL
|
||||
|
||||
create
|
||||
make
|
||||
make_text
|
||||
|
||||
feature {NONE}
|
||||
|
||||
make (n: STRING; v: STRING)
|
||||
make_text (n: STRING; v: STRING)
|
||||
do
|
||||
control_name := n
|
||||
make (n)
|
||||
text := v
|
||||
end
|
||||
|
||||
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
|
||||
set_state (new_state: JSON_OBJECT)
|
||||
-- Restore text from json
|
||||
-- Restore text from json
|
||||
do
|
||||
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then
|
||||
text := new_text.unescaped_string_32
|
||||
@@ -33,7 +33,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
end
|
||||
|
||||
state: JSON_OBJECT
|
||||
-- Return state which contains the current text and if there is an event handle attached
|
||||
-- Return state which contains the current text and if there is an event handle attached
|
||||
do
|
||||
create Result.make
|
||||
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
|
||||
@@ -42,9 +42,8 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
|
||||
|
||||
feature --EVENT HANDLING
|
||||
|
||||
|
||||
set_change_event (e: attached like change_event)
|
||||
-- Set text change event handle
|
||||
set_change_event (e: attached like change_event)
|
||||
-- Set text change event handle
|
||||
do
|
||||
change_event := e
|
||||
end
|
||||
@@ -67,7 +66,10 @@ feature
|
||||
|
||||
set_text (t: STRING)
|
||||
do
|
||||
text := t
|
||||
if not t.is_equal (text) then
|
||||
text := t
|
||||
state_changes.replace (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
|
||||
end
|
||||
end
|
||||
|
||||
feature
|
||||
|
||||
Reference in New Issue
Block a user