First working callback

This commit is contained in:
YNH Webdev
2013-08-28 00:04:42 +02:00
parent ef34217a6d
commit cb7f1f0ee3
7 changed files with 191 additions and 17 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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);

View File

@@ -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 := "<button>" + text + "</button>"
Result := "<button data-name=%"" + control_name + "%" data-type=%"WSF_BUTTON_CONTROL%">" + text + "</button>"
end
state: JSON_OBJECT

View File

@@ -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

View File

@@ -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 ("<html><head></head><body>")
response.put_string (data)
response.put_string ("</body></html>")
create states.make
control.read_state (states)
data := "<html><head>"
data.append ("</head><body>")
data.append (control.render)
data.append ("<script type=%"text/javascript%">var states=")
data.append (states.representation)
data.append (";</script>")
data.append ("<script src=%"//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js%"></script>")
data.append ("<script src=%"/widget.js%"></script>")
data.append ("</body></html>")
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