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,6 +34,8 @@ class WSF_BUTTON_CONTROL extends WSF_CONTROL
trigger_callback(@control_name, 'click') trigger_callback(@control_name, 'click')
update: (state) -> update: (state) ->
if state.text?
window.states[@control_name]['text'] = state.text
@$el.text(state.text) @$el.text(state.text)
class WSF_TEXT_CONTROL extends WSF_CONTROL class WSF_TEXT_CONTROL extends WSF_CONTROL
@@ -49,6 +50,8 @@ class WSF_TEXT_CONTROL extends WSF_CONTROL
trigger_callback(@control_name, 'change') trigger_callback(@control_name, 'change')
update: (state) -> update: (state) ->
if state.text?
window.states[@control_name]['text'] = state.text
@$el.val(state.text) @$el.val(state.text)
#map class name to effectiv class #map class name to effectiv class

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) {
if (state.text != null) {
window.states[this.control_name]['text'] = state.text;
return this.$el.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) {
if (state.text != null) {
window.states[this.control_name]['text'] = state.text;
return this.$el.val(state.text); return this.$el.val(state.text);
}
}; };
return WSF_TEXT_CONTROL; return WSF_TEXT_CONTROL;

View File

@@ -12,13 +12,13 @@ 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
@@ -64,7 +64,10 @@ feature
set_text (t: STRING) set_text (t: STRING)
do do
if not t.is_equal (text) then
text := t 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,6 +11,16 @@ 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)
@@ -32,11 +42,21 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
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)

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,7 +26,7 @@ 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
@@ -33,7 +35,7 @@ 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
@@ -48,7 +50,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
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,6 +58,17 @@ 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

View File

@@ -45,7 +45,7 @@ feature
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

View File

@@ -12,13 +12,13 @@ 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
@@ -42,7 +42,6 @@ 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
@@ -67,7 +66,10 @@ feature
set_text (t: STRING) set_text (t: STRING)
do do
if not t.is_equal (text) then
text := t text := t
state_changes.replace (create {JSON_STRING}.make_json (text), create {JSON_STRING}.make_json ("text"))
end
end end
feature feature