From cb7f1f0ee397ddb1105cdb28e46b1d0d37608176 Mon Sep 17 00:00:00 2001 From: YNH Webdev Date: Wed, 28 Aug 2013 00:04:42 +0200 Subject: [PATCH] First working callback --- examples/widgetapp/application.e | 44 ++++++++-- examples/widgetapp/sample_page.e | 1 + examples/widgetapp/widget.coffee | 45 ++++++++++ examples/widgetapp/widget.js | 82 +++++++++++++++++++ .../wsf_html/webcontrol/wsf_button_control.e | 11 ++- .../server/wsf_html/webcontrol/wsf_control.e | 2 +- .../wsf_html/webcontrol/wsf_page_control.e | 23 ++++-- 7 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 examples/widgetapp/widget.coffee create mode 100644 examples/widgetapp/widget.js diff --git a/examples/widgetapp/application.e b/examples/widgetapp/application.e index 86eab8c4..9163a630 100644 --- a/examples/widgetapp/application.e +++ b/examples/widgetapp/application.e @@ -1,12 +1,15 @@ note - description : "simple application root class" - date : "$Date$" - revision : "$Revision$" + description: "simple application root class" + date: "$Date$" + revision: "$Revision$" class APPLICATION inherit + + WSF_ROUTED_SERVICE + WSF_DEFAULT_SERVICE redefine initialize @@ -20,19 +23,44 @@ feature {NONE} -- Initialization initialize -- Initialize current service. do + initialize_router set_service_option ("port", 9090) end -feature -- Basic operations +feature {NONE} -- Initialization - execute (req: WSF_REQUEST; res: WSF_RESPONSE) + setup_router + do + -- router.map (create {WSF_URI_MAPPING}.make ("/hello", create {WSF_AGENT_URI_HANDLER}.make (agent execute_hello))) + map_agent_uri ("/", agent execute_hello, Void) + map_agent_uri ("/widget.js", agent load_js, Void) + end + +feature -- Helper: mapping + + map_agent_uri (a_uri: READABLE_STRING_8; a_action: like {WSF_URI_AGENT_HANDLER}.action; rqst_methods: detachable WSF_REQUEST_METHODS) + do + router.map_with_request_methods (create {WSF_URI_MAPPING}.make (a_uri, create {WSF_URI_AGENT_HANDLER}.make (a_action)), rqst_methods) + end + +feature -- Execution + + execute_hello (req: WSF_REQUEST; res: WSF_RESPONSE) local page: SAMPLE_PAGE do - -- To send a response we need to setup, the status code and - -- the response headers. - create page.make(req, res) + -- To send a response we need to setup, the status code and + -- the response headers. + create page.make (req, res) page.execute end + load_js (req: WSF_REQUEST; res: WSF_RESPONSE) + local + f: WSF_FILE_RESPONSE + do + create f.make_html ("widget.js") + res.send (f) + end + end diff --git a/examples/widgetapp/sample_page.e b/examples/widgetapp/sample_page.e index dcfe8f29..9a289db1 100644 --- a/examples/widgetapp/sample_page.e +++ b/examples/widgetapp/sample_page.e @@ -23,6 +23,7 @@ feature initialize_controls do button := create {WSF_BUTTON_CONTROL}.make ("sample_button", "I'm a button") + button.set_click_event(agent handle_click) control := button end diff --git a/examples/widgetapp/widget.coffee b/examples/widgetapp/widget.coffee new file mode 100644 index 00000000..e6a6c93f --- /dev/null +++ b/examples/widgetapp/widget.coffee @@ -0,0 +1,45 @@ +trigger_callback = (control_name,event)-> + $.ajax + data: + control_name: control_name + event: event + cache: no + .done (new_states)-> + states = new_states + for name,state of states + controls[name].update(state) + return + +class WSF_CONTROL + constructor: (@control_name, @$el)-> + @attach_events() + return + + attach_events: ()-> + return + + update: (state)-> + return + +controls = {} + +class WSF_BUTTON_CONTROL extends WSF_CONTROL + attach_events: ()-> + self = @ + @$el.click ()-> + self.click() + click: ()-> + trigger_callback(@control_name, 'click') + + update: (state) -> + @$el.text(state.text) + +typemap = + "WSF_BUTTON_CONTROL":WSF_BUTTON_CONTROL + +for name,state of states + $el = $('[data-name='+name+']') + type = $el.data('type') + #bind widget + controls[name]=new typemap[type](name,$el) + diff --git a/examples/widgetapp/widget.js b/examples/widgetapp/widget.js new file mode 100644 index 00000000..55f30910 --- /dev/null +++ b/examples/widgetapp/widget.js @@ -0,0 +1,82 @@ +// Generated by CoffeeScript 1.6.1 +(function() { + var $el, WSF_BUTTON_CONTROL, WSF_CONTROL, controls, name, state, trigger_callback, type, typemap, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + trigger_callback = function(control_name, event) { + return $.ajax({ + data: { + control_name: control_name, + event: event + }, + cache: false + }).done(function(new_states) { + var name, state, states; + states = new_states; + for (name in states) { + state = states[name]; + controls[name].update(state); + } + }); + }; + + WSF_CONTROL = (function() { + + function WSF_CONTROL(control_name, $el) { + this.control_name = control_name; + this.$el = $el; + this.attach_events(); + return; + } + + WSF_CONTROL.prototype.attach_events = function() {}; + + WSF_CONTROL.prototype.update = function(state) {}; + + return WSF_CONTROL; + + })(); + + controls = {}; + + WSF_BUTTON_CONTROL = (function(_super) { + + __extends(WSF_BUTTON_CONTROL, _super); + + function WSF_BUTTON_CONTROL() { + return WSF_BUTTON_CONTROL.__super__.constructor.apply(this, arguments); + } + + WSF_BUTTON_CONTROL.prototype.attach_events = function() { + var self; + self = this; + return this.$el.click(function() { + return self.click(); + }); + }; + + WSF_BUTTON_CONTROL.prototype.click = function() { + return trigger_callback(this.control_name, 'click'); + }; + + WSF_BUTTON_CONTROL.prototype.update = function(state) { + return this.$el.text(state.text); + }; + + return WSF_BUTTON_CONTROL; + + })(WSF_CONTROL); + + typemap = { + "WSF_BUTTON_CONTROL": WSF_BUTTON_CONTROL + }; + + for (name in states) { + state = states[name]; + $el = $('[data-name=' + name + ']'); + type = $el.data('type'); + controls[name] = new typemap[type](name, $el); + } + +}).call(this); diff --git a/library/server/wsf_html/webcontrol/wsf_button_control.e b/library/server/wsf_html/webcontrol/wsf_button_control.e index 808be0af..10fe38d1 100644 --- a/library/server/wsf_html/webcontrol/wsf_button_control.e +++ b/library/server/wsf_html/webcontrol/wsf_button_control.e @@ -31,16 +31,21 @@ feature do end - handle_callback (event: STRING; cname: STRING; page: WSF_PAGE_CONTROL) + set_click_event (e: PROCEDURE [ANY, TUPLE [WSF_PAGE_CONTROL]]) do - if Current.control_name = cname and attached click_event then + click_event := e + end + + handle_callback (cname: STRING; event: STRING; page: WSF_PAGE_CONTROL) + do + if Current.control_name.is_equal (cname) and attached click_event then click_event.call ([page]) end end render: STRING do - Result := "" + Result := "" end state: JSON_OBJECT diff --git a/library/server/wsf_html/webcontrol/wsf_control.e b/library/server/wsf_html/webcontrol/wsf_control.e index 40e61b3a..9c039e77 100644 --- a/library/server/wsf_html/webcontrol/wsf_control.e +++ b/library/server/wsf_html/webcontrol/wsf_control.e @@ -13,7 +13,7 @@ feature feature {WSF_PAGE_CONTROL, WSF_CONTROL} - handle_callback (event: STRING; cname: STRING; page: WSF_PAGE_CONTROL) + handle_callback (cname: STRING; event: STRING; page: WSF_PAGE_CONTROL) deferred end diff --git a/library/server/wsf_html/webcontrol/wsf_page_control.e b/library/server/wsf_html/webcontrol/wsf_page_control.e index f9020ea1..b9545440 100644 --- a/library/server/wsf_html/webcontrol/wsf_page_control.e +++ b/library/server/wsf_html/webcontrol/wsf_page_control.e @@ -59,12 +59,25 @@ feature render local data: STRING + page: WSF_PAGE_RESPONSE + states: JSON_OBJECT do - data := control.render - response.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"]>>) - response.put_string ("") - response.put_string (data) - response.put_string ("") + create states.make + control.read_state (states) + + data := "" + data.append ("") + data.append (control.render) + data.append ("") + data.append ("") + data.append ("") + data.append ("") + create page.make + page.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"]>>) + page.set_body (data) + response.send (page) end get_parameter (key: STRING): detachable STRING