Only send changes back to client

This commit is contained in:
YNH Webdev
2013-08-29 18:31:57 +02:00
parent 275cc7aa21
commit bc6b4f90c3
8 changed files with 96 additions and 50 deletions

View File

@@ -24,10 +24,10 @@ feature
local local
panel: WSF_MULTI_CONTROL panel: WSF_MULTI_CONTROL
do do
create textbox1.make ("txtBox1", "1") create textbox1.make_text ("txtBox1", "1")
create textbox2.make ("txtBox2", "2") create textbox2.make_text ("txtBox2", "2")
create button1.make ("sample_button1", "SUM") create button1.make_button ("sample_button1", "SUM")
create textbox_result.make ("txtBox3", "") create textbox_result.make_text ("txtBox3", "")
button1.set_click_event (agent handle_click) button1.set_click_event (agent handle_click)
create panel.make ("panel") create panel.make ("panel")
panel.add_control (textbox1) panel.add_control (textbox1)

View File

@@ -7,7 +7,6 @@ trigger_callback = (control_name,event)->
cache: no cache: no
.done (new_states)-> .done (new_states)->
#Update all classes #Update all classes
window.states = new_states
for name,state of new_states for name,state of new_states
controls[name]?.update(state) controls[name]?.update(state)
return return
@@ -35,7 +34,9 @@ class WSF_BUTTON_CONTROL extends WSF_CONTROL
trigger_callback(@control_name, 'click') trigger_callback(@control_name, 'click')
update: (state) -> 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 class WSF_TEXT_CONTROL extends WSF_CONTROL
attach_events: ()-> attach_events: ()->
@@ -49,7 +50,9 @@ class WSF_TEXT_CONTROL extends WSF_CONTROL
trigger_callback(@control_name, 'change') trigger_callback(@control_name, 'change')
update: (state) -> 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 #map class name to effectiv class
typemap = typemap =

View File

@@ -14,7 +14,6 @@
cache: false cache: false
}).done(function(new_states) { }).done(function(new_states) {
var name, state, _ref; var name, state, _ref;
window.states = new_states;
for (name in new_states) { for (name in new_states) {
state = new_states[name]; state = new_states[name];
if ((_ref = controls[name]) != null) { if ((_ref = controls[name]) != null) {
@@ -66,7 +65,10 @@
}; };
WSF_BUTTON_CONTROL.prototype.update = function(state) { 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; return WSF_BUTTON_CONTROL;
@@ -97,7 +99,10 @@
}; };
WSF_TEXT_CONTROL.prototype.update = function(state) { 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; return WSF_TEXT_CONTROL;

View File

@@ -12,20 +12,20 @@ inherit
WSF_CONTROL WSF_CONTROL
create create
make make_button
feature {NONE} feature {NONE}
make (n: STRING; v: STRING) make_button (n: STRING; v: STRING)
do do
control_name := n make (n)
text := v text := v
end end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
set_state (new_state: JSON_OBJECT) set_state (new_state: JSON_OBJECT)
-- Restore text from json -- Restore text from json
do do
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then
text := new_text.unescaped_string_32 text := new_text.unescaped_string_32
@@ -33,7 +33,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
end end
state: JSON_OBJECT 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 do
create Result.make create Result.make
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text")) 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 feature --EVENT HANDLING
set_click_event (e: attached like click_event) set_click_event (e: attached like click_event)
-- Set button click event handle -- Set button click event handle
do do
click_event := e click_event := e
end end
@@ -64,7 +64,10 @@ feature
set_text (t: STRING) set_text (t: STRING)
do 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 end
feature feature

View File

@@ -11,10 +11,20 @@ feature
control_name: STRING 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 feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
load_state (new_states: JSON_OBJECT) load_state (new_states: JSON_OBJECT)
-- Select state stored with `control_name` as key -- Select state stored with `control_name` as key
do do
if attached {JSON_OBJECT} new_states.item (create {JSON_STRING}.make_json (control_name)) as new_state_obj then if attached {JSON_OBJECT} new_states.item (create {JSON_STRING}.make_json (control_name)) as new_state_obj then
set_state (new_state_obj) set_state (new_state_obj)
@@ -22,32 +32,42 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
end end
set_state (new_state: JSON_OBJECT) 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 deferred
end end
read_state (states: JSON_OBJECT) 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 do
states.put (state, create {JSON_STRING}.make_json (control_name)) states.put (state, create {JSON_STRING}.make_json (control_name))
end 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 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 deferred
end end
state_changes: JSON_OBJECT
feature --EVENT HANDLING feature --EVENT HANDLING
handle_callback (cname: STRING; event: STRING) 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 deferred
end end
feature feature
render: STRING render: STRING
-- Return html representaion of control -- Return html representaion of control
deferred deferred
end end

View File

@@ -11,7 +11,9 @@ inherit
WSF_CONTROL WSF_CONTROL
redefine redefine
make,
read_state, read_state,
read_state_changes,
load_state load_state
end end
@@ -24,16 +26,16 @@ feature {NONE}
make (n: STRING) make (n: STRING)
do do
control_name := n Precursor (n)
controls := create {LINKED_LIST [WSF_CONTROL]}.make; controls := create {LINKED_LIST [WSF_CONTROL]}.make;
end end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
load_state (new_states: JSON_OBJECT) load_state (new_states: JSON_OBJECT)
-- Pass new_states to subcontrols -- Pass new_states to subcontrols
do do
Precursor(new_states) Precursor (new_states)
across across
controls as c controls as c
loop loop
@@ -46,9 +48,9 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
end end
read_state (states: JSON_OBJECT) read_state (states: JSON_OBJECT)
-- Read states in subcontrols -- Read states in subcontrols
do do
Precursor(states) Precursor (states)
across across
controls as c controls as c
loop loop
@@ -56,8 +58,19 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
end end
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 state: JSON_OBJECT
--Read state --Read state
do do
create Result.make create Result.make
end end
@@ -65,7 +78,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
feature --EVENT HANDLING feature --EVENT HANDLING
handle_callback (event: STRING; cname: STRING) handle_callback (event: STRING; cname: STRING)
-- Pass callback to subcontrols -- Pass callback to subcontrols
do do
if equal (cname, control_name) then if equal (cname, control_name) then
else else

View File

@@ -25,27 +25,27 @@ feature -- Access
feature feature
initialize_controls 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 deferred
ensure ensure
attached control attached control
end end
process process
-- Function called on page load (not on callback) -- Function called on page load (not on callback)
deferred deferred
end end
feature feature
execute execute
-- Entry Point: If request is a callback, restore control states and execute handle then return new state json. -- 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 -- If request is not a callback. Run process and render the html page
local local
event: detachable STRING event: detachable STRING
control_name: detachable STRING control_name: detachable STRING
states: detachable STRING states: detachable STRING
new_states: JSON_OBJECT states_changes: JSON_OBJECT
json_parser: JSON_PARSER json_parser: JSON_PARSER
do do
control_name := get_parameter ("control_name") control_name := get_parameter ("control_name")
@@ -57,10 +57,10 @@ feature
control.load_state (sp) control.load_state (sp)
end end
control.handle_callback (control_name, event) control.handle_callback (control_name, event)
create new_states.make create states_changes.make
control.read_state (new_states) control.read_state_changes (states_changes)
response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "application/json"]>>) response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "application/json"]>>)
response.put_string (new_states.representation) response.put_string (states_changes.representation)
else else
process process
render render
@@ -68,7 +68,7 @@ feature
end end
render render
-- Render and send the HTML Page -- Render and send the HTML Page
local local
data: STRING data: STRING
page: WSF_PAGE_RESPONSE page: WSF_PAGE_RESPONSE
@@ -92,13 +92,13 @@ feature
end end
get_parameter (key: STRING): detachable STRING get_parameter (key: STRING): detachable STRING
-- Read query parameter as string -- Read query parameter as string
local local
value: detachable WSF_VALUE value: detachable WSF_VALUE
do do
Result := VOID Result := VOID
value := request.query_parameter (key) 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 Result := value.as_string.value
end end
end end

View File

@@ -12,20 +12,20 @@ inherit
WSF_CONTROL WSF_CONTROL
create create
make make_text
feature {NONE} feature {NONE}
make (n: STRING; v: STRING) make_text (n: STRING; v: STRING)
do do
control_name := n make (n)
text := v text := v
end end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
set_state (new_state: JSON_OBJECT) set_state (new_state: JSON_OBJECT)
-- Restore text from json -- Restore text from json
do do
if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then if attached {JSON_STRING} new_state.item (create {JSON_STRING}.make_json ("text")) as new_text then
text := new_text.unescaped_string_32 text := new_text.unescaped_string_32
@@ -33,7 +33,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
end end
state: JSON_OBJECT 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 do
create Result.make create Result.make
Result.put (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text")) 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 feature --EVENT HANDLING
set_change_event (e: attached like change_event)
set_change_event (e: attached like change_event) -- Set text change event handle
-- Set text change event handle
do do
change_event := e change_event := e
end end
@@ -67,7 +66,10 @@ feature
set_text (t: STRING) set_text (t: STRING)
do 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 end
feature feature